Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can define the entire application stack, including services, networks, and volumes, in a single YAML file. This simplifies the process of deploying and managing complex applications with multiple interconnected components. In this tutorial, we'll cover the basics of Docker Compose, including creating a docker-compose.yml
file and using the docker-compose
command.
Docker Compose is not included in the default Docker installation. To install Docker Compose, follow the instructions in the official documentation: https://docs.docker.com/compose/install/
docker-compose.yml
file:To define your application stack, create a docker-compose.yml
file in the root directory of your project. The file should include the following sections:
version
: The version of the Docker Compose file format.services
: A list of services that make up your application.networks
: (Optional) A list of custom networks to be created for your application.volumes
: (Optional) A list of named volumes to be created and used by your services.Here's a basic example of a docker-compose.yml
file:
version: "3.9" services: web: build: . ports: - "80:80" db: image: "postgres:latest" environment: - POSTGRES_USER=myuser - POSTGRES_PASSWORD=mypassword networks: default: external: name: my-network volumes: db-data:
In this example, we define two services: web
and db
. The web
service is built from the current directory, while the db
service uses the official postgres
image. We also define a custom network and a named volume.
Some common Docker Compose commands include:
docker-compose up
: Build, (re)create, and start all services and containers defined in the docker-compose.yml
file. Use the -d
option to run containers in the background (detached mode).
docker-compose up -d
docker-compose down
: Stop and remove all services, containers, networks, and volumes defined in the docker-compose.yml
file.
docker-compose down
docker-compose ps
: List the containers managed by Docker Compose.
docker-compose ps
docker-compose logs
: View the logs of all services and containers managed by Docker Compose. Use the -f
option to follow the log output.
docker-compose logs -f
docker-compose exec
: Execute a command in a running container.
docker-compose exec SERVICE_NAME COMMAND
Replace SERVICE_NAME
with the name of the service, and COMMAND
with the command you want to execute.
To update a service, modify its definition in the docker-compose.yml
file and run docker-compose up -d
. Docker Compose will recreate the affected containers and apply the changes.
To scale a service, use the --scale
option followed by the service name and the desired number of replicas:
docker-compose up -d --scale SERVICE_NAME=NUM_REPLICAS
Replace SERVICE_NAME
with the name of the service, and NUM_REPLICAS
with the desired number of replicas.
This tutorial provides a basic introduction to Docker Compose, including creating a docker-compose.yml
file and using the docker-compose
command.
Defining Services in Docker Compose:
docker-compose.yml
file.docker-compose.yml
file:version: '3' services: web: image: nginx:latest db: image: postgres:latest
Docker Compose Environment Variables:
docker-compose.yml
file to pass configurations to services.version: '3' services: web: image: nginx:latest environment: - MY_ENV_VARIABLE=value
Docker Compose Volumes:
version: '3' services: web: image: nginx:latest volumes: - ./app:/app
Docker Compose Scaling Services:
docker-compose up --scale
command to scale services horizontally.docker-compose up --scale web=3
Docker Compose Build and Up Commands:
docker-compose build
to build images defined in the docker-compose.yml
file, and docker-compose up
to start the services.docker-compose build docker-compose up -d
Docker Compose Down and Stop Commands:
docker-compose down
to stop and remove containers, networks, and volumes defined in the docker-compose.yml
file. docker-compose stop
only stops services.docker-compose down