Skip to content
tezvyn:

Docker Compose Services: Defining Your App's Components

Source: docs.docker.comEasyHow cards are made

A service in Docker Compose is a blueprint for a running container. You define its image, ports, and environment to describe one piece of your application, like a web server or database. The footgun is using build and image together for one service.

Why it exists

To manage multi-container applications declaratively. Instead of manually starting, networking, and configuring individual containers for a web server, database, and cache, you define them as services in a single file. This makes complex local development setups reproducible and easy to manage.

The mental model

Think of your docker-compose.yml file as a cast list for a play. Each service is a character. The image or build key is the actor's script (the code they run). ports are the stage doors they can be reached through. volumes are their props that persist between scenes. environment variables are director's notes that change their performance. docker-compose up is the "action!" call that brings them all to life on stage.

How it works

Under the services key in a docker-compose.yml file, you define one or more named services. For each service, you specify configuration. The most important is image (to pull from a registry) or build (to build from a local Dockerfile). Other common keys include ports to map container ports to host ports, volumes for persistent data, environment to set environment variables, and depends_on to control startup order. When you run docker-compose up, Docker reads this file, creates a network, and starts containers for each service according to your definitions.

When to use it

Use services for any project involving more than one container. It is the standard for local development environments for web applications, microservices architectures, and data processing pipelines. It lets you spin up a complete, interconnected environment (e.g., a WordPress site with its MySQL database) with a single command.

When not to use it

While excellent for development, Docker Compose is not a full production orchestration tool like Kubernetes. It lacks features for auto-scaling, high-availability, and rolling updates across a cluster of machines. For single-container apps, a simple docker run command may be easier.

One canonical example

A simple web app might have two services: web and db. The web service would be built from a local Dockerfile and depend on the db service. The db service would use the official postgres image, use a named volume to persist its data, and not expose any ports to the host. The web service connects to the database using the hostname db, as Compose provides service discovery on its internal network.

Interview question

What is the primary benefit of defining services in a Docker Compose file for an application?

  • a.It enables automatic scaling and high availability for production deployments.
  • b.It eliminates the need for Dockerfiles by allowing direct container creation from source code.
  • c.It provides a declarative way to manage multiple interconnected containers as a single application.Correct
  • d.It offers a web-based dashboard for monitoring and controlling individual container processes.
Why?

The card states that Docker Compose's purpose is to "manage multi-container applications declaratively" and allows you to "spin up a complete, interconnected environment with a single command." Option A describes features of production orchestration tools like Kubernetes, which the card explicitly states Compose is not.

Just read this? Test yourself on what you have been reading.

Read the original → docs.docker.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.

See open roles