How Do You Deploy Several Docker Containers with Nginx and Have Each of Them Work Simultaneously on a Single RedHat Server?

Problem scenario
You want to create several Docker containers each supporting Nginx.  You want to be able to browse to the different instances of Nginx from a web browser.  How do you have multiple Docker containers support Nginx on one RedHat Enterprise Linux host server?

Solution
These directions are geared toward a RHEL host server instance in AWS.

#1 Install Docker. For directions, see this link or this link.

#2 Configure a user defined network in Docker.  From the Linux server, run this command:

ip addr show # or "ip addr show | grep eth0"

Look at the results for eth0.  Find its "inet" IP address and subnet mask (e.g., 172.31.38.151/20). This is the internal IP address of the Docker server and its subnet mask in shorthand notation.  To determine the IP address of this Docker network you will create, mentally increment the last octet's integer value by 1 and keep the subnet mask, unless it is 32, for the construction of a command below.  If the last octet in the IP address is 255 and mathematically adding 1 to it will put the number above 255, find an addressable IP number not being used and within the subnet mask for the server's IP address.  (For example, you may have to increment the third octet by 1 and have the final octet be 0 or 1.)  If the subnet mask is /32, use "24" as the subnet mask for the command below (docker network create...).  If you are using Google Cloud Platform, the IP address will likely start with a 10 (the first octet will not be a 172). 

Run a command like the following, however you may want to substitute "isolated_nw1" with the name of your choice for this user-defined network.  You must substitute x.x.x.x in the draft of the command below with the IP address this network will have (possibly as simple as one greater than the last octet of the server's internal IP address).  You will likely substitute 20 with the subnet mask short hand notation that the server's internal IP address has.  

docker network create --driver bridge isolated_nw1 --subnet x.x.x.x/20

The resulting command was you substitute properly may look like this:

docker network create --driver bridge isolated_nw1 --subnet 172.31.38.152/20

Take a note of this IP address and mentally increment the last octet one more time for future usage.  You'll use that IP address later for a Docker container.

#3 Make sure the Security Group allows the Docker server to reach the internet.  If you aren't using AWS, make sure the firewall allows you to reach the internet.

#4 Run a command like this but with a different IP address:

docker run --name docker-nginx1 -p 8123:80 --network=isolated_nw1 --ip 172.31.58.153 nginx

# Substitute 172.31.58.153 with the IP address referred to in the final two sentences of step #2.  
# The IP address you choose should be visible to the subnet that you created in step #2 given the network's IP address subnet mask.  
# This IP address should also be unique to the Linux server itself.
# Change the 8123 to the TCP port of your choice. It must be unique among the containers you create with the above commands.
# Change "docker-nginx1" to the name of the container you want; it must be unique to the server itself.
# Change "isolated_nw1" to the name of the subnet mask you created earlier.
# If you prefer to not use the public nginx image, replace the "nginx" at the end with the repository name and tag of the image you want to use.  To find the repository name and tag, run a "docker images" command.  The results will have several columns; the two left-most columns will have the repository name and tag to use for any given image.

# If the command hangs after you press enter, use ctrl-c.  In our experience, the Docker container is successfully created despite not gracefully returning to the command prompt as you would normally expect.  To read about other people's experiences of the command hanging, see this link or this Docker link.

#5  Run this command to find the Docker ID: docker ps -a

#6  Start this new Docker container.  docker start <dockerID from command above>

#7 This step assumes that your Security Group has outbound connections open for the port you configured in step #4 (the left of the colon one) to the IP address of your desktop.  Open a web browser from your desktop computer.  Type in the external IP address of the Docker server with a ":8123" at the end (where 8123 is the port you used to the left of the colon in step #4's -p flag).  The URL may look like this in Chrome or Firefox:  172.31.38.151:8123

You should see "Welcome to Nginx!" in the web browser display.  You can create many different Docker containers with Nginx each working on one host server as long as the containers have these three things:
    1) names that are unique to the host server, as assigned by the --name flag in step #4.
    2) ports that are unique to the host server, as assigned left of the colon after the -p flag in step #4.
    3) a IP address that is unique to the host server, as assigned with the --ip flag in step #4.

Port 80 and the user-defined network name are things that can be be reused as you create permutations of step #4's command.  If you want confirmation you are reaching a given Docker container, modify the "Welcome to Nginx" file to something unique.  Then open a web browser and test it out.

If you don't know how to enter a Docker container, see this link. If you want to know where to find the file index.html in the container, see this link as it will be default.conf or nginx.conf. If you need to know how to get a text editor installed in a Docker container, see this link.

Leave a comment

Your email address will not be published. Required fields are marked *