Why use docker?

We use docker to run different websites in nginx because:

  1. Docker makes Nginx run in its own container, isolated from the host OS.
  2. No risk of messing up your main server packages.
  3. With Docker, you can move your full Nginx setup between servers easily.
  4. Just move the Dockerfile and config → run again anywhere.
  5. You can version-control your Nginx config files inside Docker setups.(CI/CD)
  6. If something breaks, you can rebuild Nginx + config from scratch instantly. (CI/CD)
  7. If you have Node.js apps in Docker, Django apps in Docker, etc., (using docker-compose)
  8. It makes sense to also run Nginx in Docker to manage everything together easily. (using docker-compose)

Docker commands

  1. docker run

    Example: docker run --publish 443:443 nginx

    where:

    • 443 - local port
    • 443 - container port
    • nginx -container image name
  2. docker build

    Example: docker build --tag myimage .

    where:

    • myimage - image name
    • . - the build path

Using Dockerfile to build images

DockerFile - A text document with instructions for building a container image. It contains:

  • Base Image
  • Commands and Configuration
  • Add content
#Dockerfile format
INSTRUCTION arguments
...
INSTRUCTION arguments
Common Instructions
FROM specifies the base image
RUN Runs the command inside the container
COPY Copies file from the local system into the container

Example:

#Dockerfile with NGINX
FROM nginx:latest
COPY ./index.html /usr/share/nginx/html/index.html

 

Now let's run and try the docker

apt  install docker.io
root@ip-172-31-2-79:~/docker-files# ls
Dockerfile  README.md  config  scripts  website
root@ip-172-31-2-79:~/docker-files# docker build --tag az_ielts .

Here is the sample config of the Dockerfile

# Because the base image uses the Ubuntu operating system,
# `apt` commands can be used to install applications.  But
# the package references need to be updated first.
RUN apt update

# Install Python3
RUN apt install -y python3

# Install the Apache2 Utilities
RUN apt install -y apache2-utils

# Use htpasswd to create a password file with credentials
# for a user named "robots" with the password "123456"
RUN htpasswd -b -c /etc/nginx/passwords robots 123456

# Copy the app server script into the container image
COPY ./scripts/start_app_servers.py /usr/local/bin/start_app_servers.py

# Copy a wrapper script into the entrypoint location for the NGINX image.
# The wrapper script starts the app servers in a way that allows the
# NGINX process to start properly
COPY ./scripts/wrapper.sh /docker-entrypoint.d/wrapper.sh

# Copy the az_ielts.conf NGINX configuration into the container as
# the default configuration.
COPY ./config/az_ielts.conf /etc/nginx/conf.d/default.conf

# If you have problems using the SSL enabled configuraion, use the
# following configuration instead.
# Note that you will have to use `docker run --publish 80:80 az_ielts` as well.
# COPY ./config/az_ielts.conf-no-ssl.conf /etc/nginx/conf.d/default.conf

# Copy the AZ_ielts website contents into the default root directory
COPY ./website/ /usr/share/nginx/html
  • First, it will update packages ( in preparation to install python3 and other utilities)
  • Install the python inside the image
  • install the apache2-utils in the image to run apache2-utils
  • Create a password using htpasswd
  • Copy the python app script in the /usr/loca/bin to be able to run it
  • We have wrapper.sh to run the application. The docker-entrypoint.d is used to run the application at the start of the docker. The config was:
#!/bin/bash

/usr/bin/env python3 /usr/local/bin/start_app_servers.py &

where it will locate first the python3 the /usr/bin/env and then run the application  /usr/local/bin/start_app_servers.py.

  • Copy the az_ielts.conf file to the /etc/nginx/conf.d/default.conf of the docker image

Here is the configuration of az_ielts.conf

upstream app_server_7001 {
    server 127.0.0.1:7001;
}

upstream roundrobin {
    # default is round robin
    server 127.0.0.1:7001;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}

upstream leastconn {
    # The server with the fewest connections will get traffic
    least_conn;
    server 127.0.0.1:7001;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}

upstream iphash {
    # Connections will stick to the same server
    ip_hash;
    server 127.0.0.1:7001;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}

upstream weighted {
    # More connections will be sent to the weighted server
    server 127.0.0.1:7001 weight=2;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}

server {
    listen 443 ssl default_server;

    ssl_certificate /etc/ssl/certs/nginx.crt;
    ssl_certificate_key /etc/ssl/private/nginx.key;
    root /usr/share/nginx/html;
    #root /var/www/az_ielts;
    access_log /var/log/nginx/az_ielts.local.access.log;
    error_log /var/log/nginx/az_ielts.local.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
    location /images {
        autoindex on;
        access_log /var/log/nginx/az_ielts.local.images.access.log;
        error_log /var/log/nginx/az_ielts.local.images.error.log;
    }

    error_page 404 /404.html;
    location = /404.html {
        internal;
        }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        internal;
    }

    location /500 {
        fastcgi_pass unix:/this/will/fail;
    }

    location /proxy {
        # Trailing slash is key!
        proxy_pass http://app_server_7001/;

        # Logging for access to the proxy
        access_log /var/log/nginx/az_ielts.local.proxy.access.log;
        error_log /var/log/nginx/az_ielts.local.proxy.error.log;
    }

    location /roundrobin {
        # Trailing slash is key!
        proxy_pass http://roundrobin/;
    }

   location /leastconn {
        # Trailing slash is key!
        proxy_pass http://leastconn/;
    }

    location /iphash {
        # Trailing slash is key!
        proxy_pass http://iphash/;
    }

    location /weighted {
        # Trailing slash is key!
        proxy_pass http://weighted/;
    }
    error_page 401 403 /403.html;
    
    location /private {
            #ADD your IP address after allow
            allow 142.114.94.144;
            deny all;
    }
}
       
  • Lastly, we copy all the website configuration (html, scripts, etc,index.html)  to /usr/share/nginx/html

Now we will first build our docker using dockerfile. Here are the steps

  1. Build the image using docker build --tag az_ielts .

    Here are the images created from the command

    root@ip-172-31-2-79:~/docker-files# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
    az_ielts     latest    67bf40c96312   21 minutes ago   260MB
    nginx        latest    4cad75abc83d   2 months ago     192MB
  2. Now after the image was created. Let's run the docker using the image created above
root@ip-172-31-2-79:~/docker-files# docker run -d --name az_ielts \
  -p 80:80 -p 443:443 \
  -v /etc/letsencrypt/live/piconeuron.com/fullchain.pem:/etc/letsencrypt/live/piconeuron.com/fullchain.pem:ro \
  -v /etc/letsencrypt/live/piconeuron.com/privkey.pem:/etc/letsencrypt/live/piconeuron.com/privkey.pem:ro \
  -v /etc/nginx/conf.d/az_ielts.conf:/etc/nginx/conf.d/default.conf:ro \
  -v /var/www/az_ielts:/var/www/az_ielts:ro \
  az_ielts

After that, you will notice that the docker was created

root@ip-172-31-2-79:~/docker-files# docker ps -a
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                                                      NAMES
3bd554bce176   az_ielts   "/docker-entrypoint.…"   23 minutes ago   Up 23 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   az_ielts_server

 

Now you can run you site.