Docker Compose is a tool for defining and running multi-container docker applications in Linux. With Compose, we use a YAML file to configure our application’s services. And then we create and start all the services from the configuration with a single command. Here is a simple graphical illustration that shows how Docker compose works.
Docker Compose is basically a three-step process:
- First, we need to define application environment with a Dockerfile so it can be reuse again.
- Secondly, we define the services that make up the app in docker-compose.yml so they can be run together in an isolated environment.
- Finally, we run docker-compose up command and Compose will start and run your entire application.
Getting Started With Docker Compose In Linux
Docker Compose can be installed in two ways. You can find the installation instructions under "Install Docker Compose" section in the following links.
After installation, check the version of Docker compose using commands:
# docker-compose -version
# docker-compose -v
# docker-compose --version
To get help, simply run:
# docker-compose -help
This command will list out the commands which docker compose can perform.
Now let us take a simple docker compose file as an example. Here is the contents of my compose file.
Above docker compose file is a bare minimum file to understand the basic content inside the compose file.
We can check the validity of the file by using command:
# docker-compose config
If you give a wrong version inside the docker compose file, then it will give you an error message.
Now let us run the compose file using command:
# docker-compose up -d
It will start all services with single command.
To list out the running containers created by compose file, run:
# docker-compose ps
We can bring down the application at any time with the following command:
# docker-compose down
We can also assign a different port to nginx, for example 8181.
To do so, just define the port in compose file as shown in the following image.
Now the nginx is exposed to port 8181. Let us start the container and check if Nginx is running on 8181 port.
Open the browser and verify if it is running on 8181 port.
If you want to scale the service, you can do it using command:
# docker-compose up -d --scale database=3
To display running services, run:
# docker-compose top
To stop, start. restart the whole service at once, the commands would be:
# docker-compose stop
# docker-compose start
# docker-compose restart
We can view logs of the services using command:
# docker-compose logs
Networking in docker-compose
Docker Compose sets up a single network for your app by default. Each container joins the default network and containers will able to communicate on that default network.
You can however create a new network using compose if you don't want the default network.
For the purpose of this guide, I use the following three files:
Here is the contents of Dockerfile:
Contents of docker-compose.yml:
Contents of server.py:
Now build the image using command:
# docker-compose build
Once the build is complete, you will see the following output:
As you can see the image is successfully built. You can verify it using command:
# docker images
As you see in the above output, a Docker image named image1 is created.
Now run the compose file:
# docker-compose up -d
Check if the new network is created or not with command:
# docker network ls
From the above screenshot, we can confirm a new network named dc_network_my-network has been created.
Let us check whether container is running or not by using "docker ps" command:
# docker ps
Yes, the container is running!
Finally run the application code file (server.py) using curl command:
# curl localhost:15001
# curl 10.26.35.244:15001
We are getting output - Hello World from node 1! which is written in the server.py file. Meaning it works!
- Explaining Docker Networking Concepts
- Explaining Docker Volumes With Examples
- How To Automatically Update Running Docker Containers
- ctop – A Commandline Monitoring Tool For Linux Containers
- Portainer – An Easiest Way To Manage Docker
- PiCluster – A Simple, Web-based Docker Management Application
- Dockly – Manage Docker Containers From Terminal
About the Author:
Dhruv Tiwari is a DevOps Engineer who loves automating things, working with Linux at scale and dream of the day when systems are smart enough to never need to login to a Linux box. Journey Of CI/CD from source code to code deployment to production.