what is docker compose ?
Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you can define the services that make up your application in a single file called a docker-compose.yml
file, and then start and stop all of the services with a single command.
Setup Docker compose
Once you have Docker Engine installed, you can install Docker Compose by following these steps:
- Download the Docker Compose binary from the Docker website. You can choose the version that’s appropriate for your system, such as Linux, macOS, or Windows.
- Move the binary to a directory that’s in your system’s PATH, such as
/usr/local/bin
on Linux orC:\Program Files\Docker\Compose
on Windows. - Make the binary executable by running the following command:
1 2 |
chmod +x /usr/local/bin/docker-compose |
- Verify the installation by running the following command:
1 2 |
docker-compose --version |
This should display the version of Docker Compose that’s installed on your system.
With Docker Compose installed, you’re ready to start using it to define and run multi-container applications. To get started, create a docker-compose.yml
file, and run the docker-compose up
command to start the services defined in the file. You can use the docker-compose down
command to stop the services and remove the containers, networks, and volumes created by Docker Compose.
Example:
Here’s an example of what a docker-compose.yml
file might look like for a simple web application:
1 2 3 4 5 6 7 8 9 |
version: '3' services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine" |
In this example, we define two services: “web” and “redis”. The “web” service is built from the current directory, and maps port 5000 on the host to port 5000 in the container. The “redis” service uses the official Redis image from Docker Hub.
To start the services defined in a docker-compose.yml
file, you can run the following command:
1 2 |
docker-compose up |
This will start all of the services defined in the file, and bring them up in the background as daemon processes. You can then use docker ps
to see the list of running containers, and docker logs
to see the logs for each container.
To stop the services, you can run the following command:
1 2 |
docker-compose down |
This will stop all of the services and remove the containers, networks, and volumes defined in the docker-compose.yml
file.
Docker Compose is a powerful tool that can simplify the process of managing multi-container applications. By defining all of the services in a single file, you can easily start and stop your application, as well as share it with others.