Introduction
Docker Compose is a tool that allows you to define and run multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes that make up your application. With Docker Compose, you can manage your entire application stack in a single command, making it easier to test and deploy your applications.
Prerequisites
Before you start, you should have the following installed:
- Docker: You can download it from the official Docker website.
- Docker Compose: You can install it by following the instructions on the official Docker Compose website.
- Basic knowledge of Docker: You should have a basic understanding of Docker concepts such as containers, images, and Dockerfiles.
Getting Started
To get started with Docker Compose, you will need to create a YAML file called docker-compose.yml in the root directory of your project. This file will contain the configuration for your application.
The first step is to define the services that make up your application. A service is a container that runs a specific process. For example, if your application has a web server and a database, you would define two services in your YAML file.
Here’s an example docker-compose.yml file:
1 2 3 4 5 6 7 8 | version: '3' services: web: build: . ports: - "5000:5000" db: image: postgres |
This file defines two services: web and db. The web service is built from the Dockerfile in the current directory (.), and it maps port 5000 on the host to port 5000 in the container. The db service uses the postgres image from the Docker Hub registry.
Now that you have defined your services, you can start your application by running the following command:
1 | docker-compose up |
This command will create and start the containers for your services. You should see the output from each container in your terminal.
Managing Services
Once your application is running, you can manage the services using the docker-compose command.
To stop your application, press Ctrl+C in your terminal or run the following command:
1 | docker-compose down |
This command will stop and remove the containers for your services.
To start your application again, run the following command:
1 | docker-compose up |
This command will recreate and start the containers for your services.
To run your application in the background, run the following command:
1 | docker-compose up -d |
This command will start the containers in the background, and you can view the logs using the following command:
1 | docker-compose logs -f |
This command will display the logs for all the containers in your application.
Defining Networks
You can define networks to allow your containers to communicate with each other. To define a network, add the networks section to your docker-compose.yml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | version: '3' services: web: build: . ports: - "5000:5000" networks: - mynetwork db: image: postgres networks: - mynetwork networks: mynetwork: |
This file defines a network called mynetwork, and it adds the web and db services to the network.
Now, your containers can communicate with each other using the container names as hostnames. For example, the web service can connect to the db service using the hostname db.
Defining Volumes
You can define volumes to persist data between container runs. To define a volume, add the `volumes’ section to your docker-compose.yml file:
1 2 3 4 5 6 7 8 9 10 11 12 | version: '3' services: web: build: . ports: - "5000:5000" volumes: - ./app:/app db: image: postgres volumes: app: |
This file defines a volume called app, and it mounts the ./app directory on the host to the /app directory in the web service container.
Now, any changes made to files in the ./app directory on the host will be reflected in the /app directory in the container.
You can also use named volumes or bind mounts for more complex data persistence scenarios. Please refer to the Docker documentation for more information.
Environment Variables
You can define environment variables for your services using the environment section in your docker-compose.yml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | version: '3' services: web: build: . ports: - "5000:5000" environment: - FLASK_ENV=development db: image: postgres environment: - POSTGRES_USER=myuser - POSTGRES_PASSWORD=mypassword - POSTGRES_DB=mydb |
This file defines environment variables for both the web and db services.
You can also use environment files to load environment variables from a file. Please refer to the Docker documentation for more information.