In this article, we will look at how to install Nginx using Docker Compose on a host server and configure it so that we can view the index.html file in the browser on port 9999.
Installing Docker and Docker Compose
Before you start, make sure you have Docker and Docker Compose installed on your host server. If they are not already installed, you can install them by following the Docker instructions for Centos 7 and for Ubuntu 20.04 and Docker Compose.
Creating an index.html file on the host server
The first step is to create an index.html file on the host server. We will use it to demo Nginx when we run it in a Docker container. You can create this file anywhere on the host server where you have write access. In this example, we will create it in the /var/www/html directory.
Run the following command to create a directory and change into it:
mkdir -p /var/www/html && cd /var/www/html
Create an index.html file
Paste the following code into it:
<!DOCTYPE html> <html> <head> <title>Hello, Nginx!</title> </head> <body> <h1>Hello, Nginx!</h1> <p>This is a test page served by Nginx in a Docker container.</p> </body> </html>
Save the file in any place convenient for you on the host server. In this example, we will save the file in the /var/www/html directory.
Creating a docker-compose.yaml file
We need to create a docker-compose.yaml file that defines the Nginx container and its configuration. This file can be created anywhere on the host server where you have write access. In this example, we will create it in the /opt/nginx directory.
Run the following command to create the directory and file:
mkdir -p /opt/nginx && cd /opt/nginx vim /opt/nginx/docker-compose.yaml
Add the following content to the docker-compose.yaml file:
version: '3' services: nginx: image: nginx:latest ports: - "9999:80" volumes: - /var/www/html:/usr/share/nginx/html restart: always
services - defines the list of services we want to run with Docker Compose.
nginx is the name of the service we want to start.
image - defines the Docker image we want to use. In this case, we are using the official Nginx image, which is tagged latest.
ports - defines the port mapping between the host machine and the container. In this case, we are mapping port 9999 on the host machine to port 80 on the container.
volumes - defines the volume we want to use to store data inside the container. In this case, we are mounting the /var/www/html directory on the host machine to the /usr/share/nginx/html directory in the container.
restart - determines whether Docker should automatically restart the container if it has stopped. In this case, we specify that the container should always be restarted.
Running Docker Compose
We are now ready to run the Nginx container using Docker Compose. To do this, run the following command from the directory where the docker-compose.yaml file is located:
docker-compose up -d
This command will start the Nginx container in the background and print its ID.
You can check if this container is running with the command:
Checking Nginx is working
We can now test Nginx by opening a web browser and navigating to http://<host server address>:9999. In our example, this will be http://IP:9999.
If everything is set up correctly, you should see a "Hello, Nginx!" page.
This is all! You can now use Docker Compose to manage Nginx containers and other applications on your hosted server.