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.
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
Now, verify the Docker Compose installation using the following command:
docker-compose --version
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 project/src
Next, create an index.html page inside the src directory:
nano project/src/index.html
Add the following contents:
Nginx with Docker Compose
Install Nginx using Docker Compose.
This content is being served by an Nginx Docker container.
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:
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.
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 MB
project_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/tcp
project_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8080->80/tcp,:::8080->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_1
php_1 | [10-Sep-2021 10:38:30] NOTICE: fpm is running, pid 1
php_1 | [10-Sep-2021 10:38:30] NOTICE: ready to handle connections
web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
web_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.sh
web_1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
web_1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
web_1 | 2021/09/10 10:38:31 [notice] 1#1: using the "epoll" event method
web_1 | 2021/09/10 10:38:31 [notice] 1#1: nginx/1.21.3
web_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-generic
web_1 | 2021/09/10 10:38:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
web_1 | 2021/09/10 10:38:31 [notice] 1#1: start worker processes
web_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:
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.
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.
15votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.