Docker Compose
Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience.
Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single, comprehensible YAML configuration file. Then, with a single command, you create and start all the services from your configuration file.
Compose works in all environments; production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:
Start, stop, and rebuild services
View the status of running services
Stream the log output of running services
Run a one-off command on a service
Why use Docker Compose?
Simplified control: Docker Compose allows you to define and manage multi-container applications in a single YAML file. This simplifies the complex task of orchestrating and coordinating various services, making it easier to manage and replicate your application environment.
Efficient collaboration: Docker Compose configuration files are easy to share, facilitating collaboration among developers, operations teams, and other stakeholders. This collaborative approach leads to smoother workflows, faster issue resolution, and increased overall efficiency.
Rapid application development: Compose caches the configuration used to create a container. When you restart a service that has not changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to your environment very quickly.
Portability across environments: Compose supports variables in the Compose file. You can use these variables to customize your composition for different environments, or different users.
Extensive community and support: Docker Compose benefits from a vibrant and active community, which means abundant resources, tutorials, and support. This community-driven ecosystem contributes to the continuous improvement of Docker Compose and helps users troubleshoot issues effectively.
How Docker Compose works?
With Docker Compose you use a YAML configuration file, known as the Compose file, to configure your application’s services, and then you create and start all the services from your configuration with the Compose CLI.
The Compose file, or
compose.yaml
file, follows the rules provided by the Compose Specification in how to define multi-container applications.
Install Docker compose in Linux
linux$ sudo apt install docker-compose
We can also install docker compose in version, e.g. v1, v2, v3 ...
The Docker compose file
The default path for a Compose file is
compose.yaml
(preferred) orcompose.yml
that is placed in the working directory.Compose also supports
docker-compose.yaml
anddocker-compose.yml
for backwards compatibility of earlier versions. If both files exist, Compose prefers the canonicalcompose.yaml
.
CLI
The Docker CLI lets you to interact with your Docker Compose applications through the
docker compose
command, and its subcommands.Using the CLI, you can manage the lifecycle of your multi-container applications defined in the
compose.yaml
file.The CLI commands enable you to start, stop, and configure your applications effortlessly.
To start all the services defined in your docker-compose.yml
file:
linux$ docker compose up
To stop and remove the running services:
linux$ docker compose down
If you want to monitor the output of your running containers and debug issues, you can view the logs with:
linux$ docker compose logs
To lists all the services along with their current status:
linux$ docker compose ps
Task
Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint: Use the
usermod
command to give the user permission to Docker). Make sure you reboot the instance after giving permission to the user.Inspect the container's running processes and exposed ports using the
docker inspect
command.Use the
docker logs
command to view the container's log output.Use the
docker stop
anddocker start
commands to stop and start the container.Use the
docker rm
command to remove the container when you're done.
Login to DockerHub
linux$ docker login
username:
password:
Pull nginx image
linux$ docker pull nginx
Create container of nginx
linux$ docker run -d --name nginx -p 80:80 nginx:latest
Access it on browser
Put, <your_ip>:80 on browser and it suppose to give nginx web-page.
- Inspect the container's running processes and exposed ports using the
docker inspect
command.
linux$ docker inspect <container_id or container_name>
- Use the
docker logs
command to view the container's log output.
linux$ docker logs <container_id or container_name>
- Use the
docker stop
anddocker start
commands to stop and start the container.
linux$ docker stop <container_id or container_name>
linux$ docker rm <container_id or container_name>
Connect with Me
Follow me for more amazing content :)