How to use Nginx with Docker Compose

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 | sh

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

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

$ sudo systemctl enable docker

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

$ sudo systemctl status docker

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

$ docker run hello-world

How to install Docker Compose on CentOS

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

$ sudo curl -L "$(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:

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

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

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:

$ sudo mkdir ~/nginx

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

$ cd ~/nginx

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

$ sudo nano docker-compose.yml

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

  image: nginx

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

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

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

$ docker-compose up -d

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

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

$ docker ps

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

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

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:

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

root@aa825b49a317:/# exit


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

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.