Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
A Developer's Essential Guide to Docker Compose

You're reading from   A Developer's Essential Guide to Docker Compose Simplify the development and orchestration of multi-container applications

Arrow left icon
Product type Paperback
Published in Oct 2022
Publisher Packt
ISBN-13 9781803234366
Length 264 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Emmanouil Gkatziouras Emmanouil Gkatziouras
Author Profile Icon Emmanouil Gkatziouras
Emmanouil Gkatziouras
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Part 1: Docker Compose 101
2. Chapter 1: Introduction to Docker Compose FREE CHAPTER 3. Chapter 2: Running the First Application Using Compose 4. Chapter 3: Network and Volumes Fundamentals 5. Chapter 4: Executing Docker Compose Commands 6. Part 2: Daily Development with Docker Compose
7. Chapter 5: Connecting Microservices 8. Chapter 6: Monitoring Services with Prometheus 9. Chapter 7: Combining Compose Files 10. Chapter 8: Simulating Production Locally 11. Chapter 9: Creating Advanced CI/CD Tasks 12. Part 3: Deployment with Docker Compose
13. Chapter 10: Deploying Docker Compose Using Remote Hosts 14. Chapter 11: Deploying Docker Compose to AWS 15. Chapter 12: Deploying Docker Compose to Azure 16. Chapter 13: Migrating to Kubernetes Configuration Using Compose 17. Index 18. Other Books You May Enjoy

Understanding how Docker Compose works

Since we have Docker and Docker Compose installed onto our system, let’s take some time and understand what Compose is and how it works behind the scenes.

On GitHub, we can find a project (https://github.com/docker/compose) where the Docker Compose source code is being hosted. By navigating to the source code, we can see and understand more about Compose, as follows:

  • Compose integrates with the Docker CLI as a plugin.
  • Compose interacts with Docker Engine through the API.
  • Compose provides a CLI and its actions translate into Docker Engine API calls.
  • Compose will read the Compose YAML file and generate resources accordingly.
  • Compose provides a layer for converting docker-compose commands into CLI-compliant ones.
  • Compose will interact with Docker objects and distinguish between them using labels.

The Docker CLI provides an API to create and load plugins. Once a plugin has been created and loaded on its invocation, the CLI command will be passed to it:

func pluginMain() {
    plugin.Run(func(dockerCli command.Cli) *cobra.Command {
      …
      }
}
func main() {
    if commands.RunningAsStandalone() {
            os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
    }
    pluginMain()
}

The CLI is based on Cobra (https://github.com/spf13/cobra), which is a popular Go library for CLI applications.

Compose, being a plugin of the Docker CLI, will use a Docker Engine API client provided by the Docker CLI:

lazyInit.WithService(compose.NewComposeService(dockerCli.Client(), dockerCli.ConfigFile()))

Each command passed to the Docker Compose plugin will lead to an interaction with the Docker Engine API on our host. For example, the internals of the ls command:

func (s *composeService) List(ctx context.Context, opts api.ListOptions) ([]api.Stack, error) {
    list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
        Filters: filters.NewArgs(hasProjectLabelFilter()),
        All:     opts.All,
    })
    if err != nil {
        return nil, err
    }
    return containersToStacks(list)
}

We now have a good understanding of how Compose works and interacts with Docker Engine. You can also refer to the source code for more information. Next, we’re going to run our first Docker Compose application.

You have been reading a chapter from
A Developer's Essential Guide to Docker Compose
Published in: Oct 2022
Publisher: Packt
ISBN-13: 9781803234366
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime