Docker Compose Tutorial: Manage Multi-Container Applications

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.

What is 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.

Further, it allows you to specify the setup of each service in your application, including the environment variables to use, the exposed ports to utilize, and more. Additionally, Docker Compose makes it easier to scale your application, simple to replicate services, and offers handy commands for starting and examining your containers and services.

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.

Benefits of Using Docker Composer

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.

Docker Compose Tutorial: How to Manage Multi-Container Applications

In this section, we show you how to deploy and manage multi container applications using Docker Compose.

Getting Started

It is necessary to update all your system packages to the latest version using the following command.

				
					apt update -y
apt upgrade -y
				
			

Then, install additional required packages to your server.

				
					apt install gnupg2  apt-transport-https ca-certificates curl software-properties-common -y
				
			

Once you are done, please proceed to install Docker and Docker Compose.

Install Docker and Docker Compose

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.

				
					curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
				
			

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.

				
					cd project
mkdir -p {logs,nginx,public,db-data}
				
			

Next, create an Nginx log file.

				
					touch logs/{error,access}.log
				
			

Now verify your directory structure using the following command.

				
					tree
				
			

This shows you the structure of your project.

Create an Nginx Configuration File

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.

				
					nano nginx/app.conf
				
			

Add the following configuration.

				
					upstream php {
        server phpfpm:9000;
}
 
server {
 
        server_name your-domain.com;
 
        error_log "https://net.cloudinfrastructureservices.co.uk/opt/bitnami/nginx/logs/myapp-error.log";
        access_log  "https://net.cloudinfrastructureservices.co.uk/opt/bitnami/nginx/logs/myapp-access.log";
 
        root /myapps;
        index index.php index.html;
 
        location / {
 
                try_files $uri $uri/ /index.php?$args;
        }
 
        location ~ \.php$ {
 
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }
 
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

				
			

Save and close the file when you are done.

Next, create an index.html and info.php file. Access both files after deploying the LEMP stack in a multi container environment.

				
					echo '<h1>LEMP with Docker-Compose</h1>' > public/index.html
echo '<?php phpinfo(); ?>' > public/info.php
				
			

Create a Docker Compose File

In this section, we will create a docker-compose.yml file to define the services for the LEMP stack and base images for each container.

				
					nano docker-compose.yml
				
			

Add the following configuration.

				
					nginx:
    image: 'bitnami/nginx'
    ports:
        - '80:80'
    links:
        - phpfpm
    volumes:
        - ./logs/access.log:/opt/bitnami/nginx/logs/myapp-access.log
        - ./logs/error.log:/opt/bitnami/nginx/logs/myapp-error.log
        - ./nginx/app.conf:/bitnami/nginx/conf/server_blocks/app.conf
        - ./public:/myapps

phpfpm:
    image: 'bitnami/php-fpm'
    ports:
        - '9000:9000'
    volumes:
        - ./public:/myapps


mysql:
    image: 'mariadb'
    ports:
        - '3306:3306'
    volumes:
        - ./db-data:/var/lib/mysql
    environment:
        - MYSQL_ROOT_PASSWORD=securepassword


phpmyadmin:
    image: 'phpmyadmin/phpmyadmin'
    restart: always
    ports:
       - '8080:80'
    links:
        - mysql:mysql
    environment:
        MYSQL_USERNAME: root
        MYSQL_ROOT_PASSWORD: securepassword
        PMA_HOST: mysql

				
			

Save and close the file after you finish.

Run Docker Compose to Deploy LEMP Stack

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.

				
					docker-compose logs
				
			

Verify LEMP Deployment

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.

Docker Compose Tutorial: Manage Multi-Container Applications Conclusion

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.

Avatar for Hitesh Jethva
Hitesh Jethva

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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x