How to Install Nginx with Docker Compose (Step by Step) (2024)

If you are using Docker then you can build and run one container at a time. Then, you will have to manually connect each container together. Also, you must be careful with dependencies and startup order. This is where Docker Compose comes into the picture.

Docker Compose is a tool used for running multi-container Docker applications. It uses a YAML file to define all services in a single file to build one or more containers. It uses service definitions to build fully customizable environments with multiple containers that can share networks and data volumes. You can deploy your entire app by just running a single command. Docker Compose works in all environments like production, staging, development, and testing.

How to Install Nginx with Docker Compose (Step by Step) (1)

Features of Docker Compose

  • Preserve volume data when containers are created
  • Single host deployment
  • Quick and easy configuration
  • Only recreate containers that have changed
  • High productivity
  • Security

In this post, we will show you how to install Nginx with Docker Compose

Also Read

Apache vs Nginx – Whats the Difference ? (Pros and Cons)

Step 1 - Getting Started

First, it is a good idea to update your systems package cache to the latest version. You can update them using the following command:

apt-get update -y

After updating the system package cache, install other required dependencies using the following command:

apt-get install apt-transport-https software-properties-common ca-certificates curl gnupg lsb-release -y

Also Read

How to setup NGINX on Ubuntu in Azure/AWS or GCP

Step 2 - Install Docker

Before starting, Docker CE must be installed on your server.

For Debian and Ubuntu operating systems, follow the below steps to install Docker CE:

First, you will need to add the Docker CE repository to APT. You can add it using 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"

Once the repository is added, update the repository cache and install Docker CE with the following command:

apt-get update -yapt-get install docker-ce docker-ce-cli -y

Once the installation has been finished, verify the Docker version using the following command:

You should see the following output:

Client: Docker Engine - CommunityVersion: 20.10.8API version: 1.41Go version: go1.16.6Git commit: 3967b7dBuilt: Fri Jul 30 19:54:27 2021OS/Arch: linux/amd64Context: defaultExperimental: trueServer: Docker Engine - CommunityEngine:Version: 20.10.8API version: 1.41 (minimum version 1.12)Go version: go1.16.6Git commit: 75249d8Built: Fri Jul 30 19:52:33 2021OS/Arch: linux/amd64Experimental: falsecontainerd:Version: 1.4.9GitCommit: e25210fe30a0a703442421b0f60afac609f950a3runc:Version: 1.0.1GitCommit: v1.0.1-0-g4144b63docker-init:Version: 0.19.0GitCommit: de40ad0

For CentOS, RHEL, and Fedora operating systems, follow the below steps to install Docker CE:

First, add the Docker CE repo with the following command:

dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Next, install the Docker CE using the following command:

dnf install docker-ce --nobest -y

Once the installation is completed, start the Docker service and enable it to start at system reboot:

systemctl start dockersystemctl enable docker

Also Read

How to Run Docker Compose in the Cloud

Step 3 - Install Docker Compose

By default, Docker Compose is not included in any of the Linux distributions. So you will need to download its binary from the GitHub page:

First, download the Docker Compose binary with the following command:

curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url | grep docker-compose-Linux-x86_64 | cut -d '"' -f 4 | wget -qi -

Once the download is completed, set the execution permission to the downloaded binary:

chmod +x docker-compose-Linux-x86_64

Next, move the downloaded binary to the system path:

mv docker-compose-Linux-x86_64 /usr/bin/docker-compose

Now, verify the Docker Compose installation using the following command:

You should see the following output:

docker-compose version 1.29.2, build 5becea4c

Step 4 - Create a Web Page to Serve on Nginx Container

In this section, we will create a sample index.html page for the Nginx website. Let’s create a new directory to hold the website content using the following command:

mkdir -p <a class="als" href="https://bizraw.com" title="project" target="_blank" rel="noopener">project</a>/src

Next, create an index.html page inside the src directory:

nano project/src/index.html

Add the following contents:

<p>Nginx with Docker Compose</p><h2 id="toc-5">Install Nginx using Docker Compose.</h2><p>This content is being served by an Nginx Docker container.</p>

Save and close the file when you are finished.

We will use this sample page to replace the default Nginx landing page in the Nginx container.

Step 5 -Setup docker-compose.yaml File

To demonstrate how to work with Docker Compose, you will need to create a docker-compose.yaml file. Let’s create a docker-compose.yaml file inside the project directory:

nano project/docker-compose.yaml

Add the following contents:

version: '3'services:web:image: nginx:latestports:- "8080:80"volumes:- ./src:/usr/share/nginx/htmllinks:- phpphp:image: php:7-fpm

Save and close the file when you are finished.

A brief explanation of each service in the above file is shown below:

  • version – It defines the configuration version.
  • services – It holds all service definitions.
  • web – It downloads Nginx’s latest image from the Docker Hub and sets up a port redirection with the ports directive. It will redirect all requests on port 8080 on the host machine to the web container on port 80.
  • volumes – It will mount the local src folder to the /usr/share/nginx/html folder inside the container.
  • php – It will download php-fpm image, create a container and link it with the Nginx container.

Also Read

How to Setup Nginx as Reverse Proxy On Ubuntu Server

Step 6 - Run Docker Compose

At this, point the docker-compose.yaml file is ready to host an Nginx server. You can now use docker-compose up command to bring your environment up.

Navigate to the project directory and run Docker Compose with the following command:

cd projectdocker-compose up -d

You should get the following output:

Creating network "project_default" with the default driverPulling php (php:7-fpm)...7-fpm: Pulling from library/phpf8416d8bac72: Pull complete2259392b425a: Pull completecfb39fc3daf5: Pull complete5c501de24ca4: Pull completee8930b753eab: Pull complete0e3476f1c018: Pull complete93bffa7fd21b: Pull completeaed0342a8936: Pull complete70c98c90e58c: Pull complete71099d83f07c: Pull completeDigest: sha256:799979e1e6404f71fc6f80e0f37b4da3eb2acc669210797e99c7f7a71c79305dStatus: Downloaded newer image for php:7-fpmPulling web (nginx:latest)...latest: Pulling from library/nginxa330b6cecb98: Pull completee0ad2c0621bc: Pull complete9e56c3e0e6b7: Pull complete09f31c94adc6: Pull complete32b26e9cdb83: Pull complete20ab512bbb07: Pull completeDigest: sha256:853b221d3341add7aaadf5f81dd088ea943ab9c918766e295321294b035f3f3eStatus: Downloaded newer image for nginx:latestCreating project_php_1 ... doneCreating project_web_1 ... done

You can verify all images downloaded by Docker Compose using the following command:

docker-compose images

Sample output:

Container Repository Tag Image Id Size-------------------------------------------------------------project_php_1 php 7-fpm a879f4b3639f 462.4 MBproject_web_1 nginx latest ad4c705f24d3 133.3 MB

To check all running containers, run the following command:

docker-compose ps

Sample output:

Name Command State Ports---------------------------------------------------------------------------------------------project_php_1 docker-php-entrypoint php-fpm Up 9000/tcpproject_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8080-&gt;80/tcp,:::8080-&gt;80/tcp

You can also check the logs of running container using the following command:

docker-compose logs

Sample output:

Attaching to project_web_1, project_php_1php_1 | [10-Sep-2021 10:38:30] NOTICE: fpm is running, pid 1php_1 | [10-Sep-2021 10:38:30] NOTICE: ready to handle connectionsweb_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configurationweb_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.shweb_1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.confweb_1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.confweb_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.shweb_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.shweb_1 | /docker-entrypoint.sh: Configuration complete; ready for start upweb_1 | 2021/09/10 10:38:31 [notice] 1#1: using the "epoll" event methodweb_1 | 2021/09/10 10:38:31 [notice] 1#1: nginx/1.21.3web_1 | 2021/09/10 10:38:31 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)web_1 | 2021/09/10 10:38:31 [notice] 1#1: OS: Linux 5.4.0-29-genericweb_1 | 2021/09/10 10:38:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576web_1 | 2021/09/10 10:38:31 [notice] 1#1: start worker processesweb_1 | 2021/09/10 10:38:31 [notice] 1#1: start worker process 31

You can now access your application by visiting the URL http://your-server-ip:8080 in your web browser. You should see your sample index.html page on the following screen:

How to Install Nginx with Docker Compose (Step by Step) (2)

Conclusion

That’s it for now. You have successfully installed Nginx with Docker Compose on Linux. I hope You can now easily create a docker-compose.yaml file to host a multi-container applications. For more information about Docker Compose, check the official documentation page.

How to Install Nginx with Docker Compose (Step by Step) (2024)
Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 5773

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.