
Docker Compose is a multi-container orchestration tool for a single host. It uses the Docker engine to manage and orchestrate the Docker containers. It simplifies setting up interconnected containers for local development. In CI/CD, it automates test environments by providing temporary isolation for testing.
This article explains how to use Docker Compose to create and manage containers. It covers the basics of the docker compose command, how to create a Docker Compose file, and how Docker Compose profiles run containers for different environments.
Below are some basic Docker Compose commands that operate on a compose.yaml or docker-compose.yaml file.
This section covers creating a sample Docker Compose file compose.yaml for a sample Flask based application.
Download the sample application.
Navigate to the flask-redis-postgresql project directory.
The directory contains a file compose.yaml with the following contents.
You'll see the following Docker Compose manifest:
In this file,
name: Sets the project name to myproject.services: Defines the individual containers that make up your application.redis service with these details:image: Creates the service from the redis:alpine image.restart: always: Sets a policy to always restart this container if it stops.ports: Maps port 6379 on the host to port 6379 in the container.volumes: Mounts the named volume redis to the /data directory inside the container for persistent data.postgresql service with the following:image: Creates the service from the postgres:16-alpine image.ports: Maps port 5432 on the host to port 5432 in the container.volumes: Mounts the named volume pgdata to the /var/lib/postgresql/data directory for persistent data.environment: Sets environment variables inside the container. The values for ${DATABASE_USER}, ${DATABASE_PASSWORD}, and ${DATABASE_NAME} are substituted from your shell's environment (often loaded from an .env file).flask with the following:build: .: Builds a Docker image in the current directory from the Dockerfile.restart: always: Sets a policy to always restart this container if it stops.env_file: Loads environment variables for the service directly from a .env file.depends_on: Controls the startup order. The flask service will start only after the redis and postgresql services have been started.ports: Maps port 8000 on the host to port 5000 inside the container.volumes: Mounts the current project directory on the host to the /code directory inside the container.volumes: Defines two named volumes, redis and pgdata, using the local driver. These are managed by Docker and stored on the host machine.Create a file .env.
Add the following environment variables for PostgreSQL credentials. You can replace the following details with your credentials.
Save and exit the file.
Build and start your services.
The option -d allows you to run containers in the background.
Run the command below to check the running containers or services.
Output:
Output explanation:
flask service is running on port 8000 on the host machine, which is based on the Docker image myproject_flask.postgresql service running on port 5432 based on the Docker image postgres:16-alpine.redis service is running on port 6379 based on the Docker image redis:alpine.Check the list of images that are used by your services.
Output:
Open a web browser and visit http://<SERVER-IP>:8000/ to verify that your application is running.
The following table describes the common docker compose commands for managing your application stack.
Profiles enable selective service execution based on the environment or use cases, such as development and production.
Edit the compose.yml file.
Add the following configuration under the flask service.
Save and close the file.
In this configuration:
adminer: Defines a service named adminer that uses the adminer Docker image.restart: always: Sets a policy to always restart the container if it stops.depends_on: Ensures the adminer service starts only after the postgresql service starts.profiles: Assigns the dev profile to the service.ports: Maps port 8080 on the host to port 8080 in the container.redis-insight: Defines a service named redis-insight that uses the redis/redisinsight:latest Docker image.restart: always: Sets a policy to always restart the container if it stops.depends_on: Ensures the redis-insight service starts only after the redis service starts.profiles: Assigns the dev profile to the service.ports: Maps port 8001 on the host to port 8001 in the container.Start the adminer service.
Use the --profile dev option to activate and start services assigned to the dev profile. This tells Docker Compose to include the adminer service, which it ignores by default.
Check the running container with the following command.
You'll see the adminer service is running.
To stop a service, for example adminer, use the command below.
Remove the stopped adminer service.
Destroy all services (including the dev profile).
Docker Compose makes it easy to update services to newer container images. The exact update process depends on whether your services use the latest tag or a hard-coded version tag in the docker-compose.yml file.
If your service uses the latest tag in its image, such as image: traefik:latest:
Pull the newest version.
Docker downloads the updated image. Only services with updated images are re-pulled.
Recreate your services.
If your service uses a version-specific tag in its image, such as image: gitea/gitea:1.25.1:
Edit the Docker Compose file to specify the new version.
Pull the newly specified image.
Recreate and restart the updated containers.
docker compose up -d stops the old containers, starts new ones with the updated image version, and preserves all data stored in volumes.
In this article, you've explored how to use Docker Compose to manage container stacks. You have learned how to start, stop, restart, and destroy containers using the docker compose command, how to create the compose.yaml file for your application stack, and the basic usage of Docker Compose Profiles for selectively running containers depending on your environment. For more information, check the Docker Compose documentation.
0 Comments
Be the first to comment and share your perspective with the community.