Docker Compose is a tool that is utilized for defining and running several containers as a single service. It is used in staging, development, and testing environments. Docker Compose works by defining a stack in its “.yml” file and then executing it by utilizing docker-compose command. Docker Compose permits you to link multiple containers together and deploy an application with only one command. It also assists in maintaining the continuity of the Integration workflow.

Using Docker Compose, you can create multiple containers and add local or official images such as “Nginx”. Inside a Docker Container, you can install packages, add or remove various settings of that specific container. In this post, we will talk about how to use Nginx with Docker Compose.

To use Nginx with Docker Compose, you need to install and enable Docker and Docker Compose on your system. Follow the post to proceed with the installation procedure.

How to install Docker on CentOS

Firstly, press “CTRL ALT T” to open the terminal. After that, execute the below-given command for adding the official Docker repository and downloading its latest version:

$ curl -fsSL https://get.docker.com/ | sh

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-1.png" data-lazy- height="480" src="data:image/svg xml,” width=”759″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-2.png" data-lazy- height="485" src="data:image/svg xml,” width=”762″>

The above-given error-free output signifies that Docker is successfully installed on your system. Now start the Docker service using the “systemctl” command:

$ sudo systemctl start docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-3.png" data-lazy- height="284" src="data:image/svg xml,” width=”757″>

To ensure that the Docker service runs at every server reboot, utilize this command in your terminal:

$ sudo systemctl enable docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-4.png" data-lazy- height="295" src="data:image/svg xml,” width=”759″>

Now, verify if the Docker is running on your system or not:

$ sudo systemctl status docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-5.png" data-lazy- height="490" src="data:image/svg xml,” width=”759″>

Or you can also pull an image such as “hello-world” from the Docker Hub globally:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-6.png" data-lazy- height="562" src="data:image/svg xml,” width=”758″>

How to install Docker Compose on CentOS

You can install Docker Compose on our system, by executing the below-given command:

$ sudo curl -L “https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)$(uname -m) -o /usr/local/bin/docker-compose

This command will download the latest version of Docker Compose and store it in the “/usr/local/bin/docker-compose” directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-7.png" data-lazy- height="309" src="data:image/svg xml,” width=”757″>

Now, we will set the permission of the downloaded docker-compose with the help of the “chmod” command. The execution of the below-given will make the docker-compose file binary executable:

$ sudo chmod x /usr/local/bin/docker-compose

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-8.png" data-lazy- height="282" src="data:image/svg xml,” width=”757″>

Check out the version of the installed Docker Compose. This action will also verify the existence of Docker Compose on your CentOS system:

$ docker-compose –version

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-9.png" data-lazy- height="296" src="data:image/svg xml,” width=”758″>

How to use Nginx with Docker Compose

In this section, we will demonstrate how you can use Nginx with Docker Compose. For this purpose, firstly, we will create a “nginx” directory using the “mkdir” command. The “mkdir” command is utilized for creating single or multiple directories in a Linux-based system such as CentOS. Execute the below-mentioned command for creating a “nginx” directory in your system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-10.png" data-lazy- height="295" src="data:image/svg xml,” width=”755″>

Now, move into the “Nginx” Directory with the help of the “cd” command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-11.png" data-lazy- height="277" src="data:image/svg xml,” width=”754″>

Utilize your nano editor to create a “docker-compose.yml” file:

$ sudo nano docker-compose.yml

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-12.png" data-lazy- height="291" src="data:image/svg xml,” width=”755″>

Now, add the following lines of code in the opened “docker-compose.yml” file:

These lines will add the “Nginx” image to the Docker Container:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-13.png" data-lazy- height="569" src="data:image/svg xml,” width=”760″>

Next, press “CTRL O” to save the content in the docker-compose.yml file:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-14.png" data-lazy- height="565" src="data:image/svg xml,” width=”757″>

Now, execute the “docker-compose up” to start Nginx as a background process in our system:

Here, the “-d” option is added to activate the detached mode. This mode will execute the containers in the background:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-15.png" data-lazy- height="297" src="data:image/svg xml,” width=”760″>

To verify if Nginx is running as a background process, list the Docker images by executing the “docker ps” command:

From the output, note the CONTAINER ID of the Nginx image on your system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-16.png" data-lazy- height="226" src="data:image/svg xml,” width=”1056″>

Now, verify if the “Nginx” is running on your system by browsing “http://localhost” in your browser:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-17.png" data-lazy- height="603" src="data:image/svg xml,” width=”1365″>

If you want to add, modify or remove something inside your Nginx container, then utilize its CONTAINER ID that can be retrieved from the above-given “docker ps” command.

In our case, the CONTAINER ID is “aa825b49a317“. Now, we will execute the “docker exec” command for starting a shell inside of your Nginx container:

$ docker exec -it aa825b49a317 /bin/bash

Here, the “-t” option is added for opening a terminal and the “-i” option for making it interactive. Whereas “/bin/bash” will open a bash shell in the running Nginx container:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-18.png" data-lazy- height="278" src="data:image/svg xml,” width=”800″>

To exit the current Nginx Container shell, type exit in the terminal:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Nginx-with-Docker-Compose-19.png" data-lazy- height="218" src="data:image/svg xml,” width=”801″>

Conclusion

Docker-compose is a command-line utility that permits its user to deploy multiple Docker containers and applications with a single command. You can link multiple containers together and deploy an application in Docker. Docker Compose utilizes a “.yml” file for defining a stack. In this post, we have shown you how to install Docker and Docker Compose on CentOS. Moreover, we have also demonstrated to you how to use Nginx with Docker Compose.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/c73b74cd8044d2e9b300610f5af77d0b?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.