Docker Compose Tutorial: Manage Multi-Container Applications. In this post, we introduce Docker Compose, its major advantages then show you how to manage multi container applications using Docker Compose.
Basically, Docker Compose is software using which creates and executes multi-container Docker applications. In the environments of production, staging, testing, or development, it may allow users to manage several containers at once. So, if you are looking for a tool that makes it easier for you to manage the entire software development lifecycle, use Docker Compose.
An application may comprise multiple containers each executing a different service. In such a case, manually starting and managing containers is time consuming. Hence, to make things simpler, Docker developed Docker Compose – a helpful tool to streamline the procedure.
Applying the rules set out in a docker-compose.yaml file is how Docker Compose operates. The YAML configuration file specifies the rules for how the application’s services should operate. Using Compose, you easily, create, destroy, and rebuild the services with a single command and define them in a YAML file. In addition, you run one-off commands, view log outputs, and examine a service’s status.
Use it for automated testing environments, spin up new isolated development environments, or single host deployments. Let’s have a look at some of the main benefits that will make it easier to understand the software.
When dealing with containerized apps, developers and DevOps teams gain a lot by using Docker Compose in the following ways:
1. Easy Deployment – Swiftly deploy your whole application stack with Docker Compose with just one command. This simplifies the deployment procedure and even guarantees uniformity across many environments.
2. Scalable – Another benefit of using Docker Composer is you individually scale each service. In fact, in case you need to manage increased traffic or workload, you may easily replicate services, giving your application more scalability.
3. CI/CD Support – Developers simply access and share the whole setup because all of the services are well defined inside of the docker-compose file. The environment can be launched in a couple of minutes by downloading the YAML file and source code. In fact, an effective CI/CD pipeline is built up and enabled as a result.
4. Quick Development Workflow – By offering a simple approach to define, deploy, and manage applications, Docker Compose improves the development workflow. Developers are free to concentrate on developing code without being distracted by the difficulties of setting up the full environment.
5. Efficient Resource Usage – Host numerous isolated environments on a single host using Docker Compose. You conserve a significant amount of resources by running everything on one piece of hardware. Resource efficiency is also improved by its capability to reuse previously used containers and cache configurations.
6. Secure Internal Communication – All the services share a network that Compose generates. Since the services cannot be accessible from the outside, this gives the app an additional layer of protection.
7. Version Control Integration – A YAML file used to contain the application settings allows it to be version controlled with programs like Git. This promotes improved teamwork and makes it easier to monitor changes over time.
8. Easy to Configure – The supported environment variables and YAML scripts make it simple to configure or alter application services.
9. Fast Testing and Debugging – Local development environments can be quickly built up with all the necessary services using Docker Compose. Developers can test, troubleshoot, and debug their applications fast in a reproducible setting.
By default, the latest version of Docker and Docker Compose are not available in the Ubuntu default repository. So you should install the Docker from the Docker’s repository.
Let’s add the Docker GPG key and repository the following command.
Next, install both Docker and Docker Compose with the following command.
apt install docker-ce docker-compose -y
Verify the Docker version using the following command.
docker version
You see the Docker information on the following screen.
Create a Directory Structure for Multi Container App
In this tutorial, we deploy LEMP stack using the Docker Compose. So you need to create a directory structure for the LEMP project. First, create a project directory with the following command.
mkdir project
Then, navigate to the project directory and create other required directories using the following command.
Next, create an Nginx virtual host configuration file inside the nginx directory. Docker Compose will copy this configuration file to the container during the building process.
At this point, all files are ready to deploy the LEMP stack in the Docker container. Now, its time to run the Docker Compose. You can run it with the following command.
docker-compose up -d
This command will download each image defined in the docker-compsoe.yml file and create a container for them. After the successful deployment, you will see the following screen.
Verify all downloaded Docker images using the following command.
docker images
This displays all the Docker images in the following screen.
Now, verify the active status of each running container using the following command.
docker-compose ps
You will see the following screen.
To verify the logs of LEMP deployment, run the following command.
At this point, the LEMP stack is deployed using the Docker Compose. You can now verify the listening ports of all applications using the following command.
ss -antpl
You will see the listening ports on the following screen.
To access the phpMyAdmin, use the URL http://your-server-ip:8080/ in your web browser. You will see the phpMyAdmin login screen.
Provide your MySQL root username, password and click on Login. You see the phpMyAdmin dashboard on the following screen.
To access the Nginx web server page, use the URL http://your-server-ip. You will see the Nginx page in the following screen.
You can also access the PHP page using the URL http://your-server-ip/info.php.
In this guide, we have created a Docker Compose file and defined all LEMP components, ports, and services. Then, we run the Docker Compose to deploy the LEMP stack inside the Docker container. Finally, we have verified the LEMP stack via a web browser.
Overall, Docker Compose is an effective solution that boosts development teams’ efficiency and encourages consistency between environments. Environment variables can be added to the docker-compose file to configure containers for various environments or users. Because the variable values are not hardcoded in the setup, you have more flexibility when looking forward to setting up containers with Compose. It is even capable of streamlining the process of managing and deploying containerized apps.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.