How to Build Docker Container to Run Nginx Webserver in Microsoft Windows

Posted on


This is an article where the main focus is just to build a docker container. Specifically, building a docker container which is using a docker image. It is a docker image exist from a public docker image repository. It is using the docker image of a specific Linux operating system instead of Nginx Webserver’s docker image.  So, this article is showing how to run Nginx Webserver in a docker container. But it will use a Linux operating system docker image. In the previous part, there is already another article which is showing how to run docker container directly using Nginx Webserver docker image.

That specific subject exist in the previous article of ‘How to Run Nginx using Docker Container in Microsoft Windows‘. Instead of directly specify the Nginx Webserver docker image in the docker command to run the docker container, it is using another method. It will use a method by building a docker image with a specific modification which is installing Nginx Webserver in it. So, basically it is using a Linux operating system docker image but with the Nginx Webserver available inside of it. So, it is building a new image of Linux operating system which exist in the public docker image repository as a base image.

How to Build Nginx Docker Container in Microsoft Windows

So, this part will show how to build a docker container. After building the docker container which utilize the Nginx webserver docker image, it will continue on the process by also running that docker container :

  1. As for the first step, just choose a folder for preparing the necessary file to simulate the build process. In this context, it will use two files within that folder. For an example it exist in the following directory structure :

    C:\docker\script>tree /F .
    Folder PATH listing for volume Windows-SSD
    Volume serial number is CA30-19A4
    No subfolders exist
  2. In the above directory structure which is the ‘script’ folder, it contains a file with the name of ‘Dockerfile’. It is a description file for building a new docker image. If the file does not exist, just create a new file with the name of ‘Dockerfile’. After that, fill the ‘Dockerfile’ file with the following content :

    FROM debian:latest
    MAINTAINER Myself ""
    RUN apt-get update && apt-get upgrade -y && apt-get install -y nginx
    RUN echo "daemon off;" >> /etc/nginx/nginx.conf
    COPY welcome.html /var/www/html/welcome.html
    EXPOSE 80
    CMD ["/usr/sbin/nginx"]

    In the above Dockerfile, it is mainly retrieving Linux debian image for building the docker image. Inside of it, there is an additional command for installing Nginx webserver. So, it does not use directly the Nginx Webserver docker image, but it is using a Linux debian operating system image. After that, it will copy an HTML file for a demo. Following after, it will expose port 80 for Nginx Webserver’s access. Last but not least, it will run the Nginx Webserver.

  3. Do not forget to create the HTML file too. Continue on after, just run the script for building the docker image by executing the following command :

    C:\docker\script>docker build -t testnginx:latest .
    [+] Building 102.7s (9/9) FINISHED
    => [internal] load build definition from Dockerfile 0.0s
    => => transferring dockerfile: 293B 0.0s
    => [internal] load .dockerignore 0.1s
    => => transferring context: 2B 0.0s
    => [internal] load metadata for 0.9s
    => [internal] load build context 0.1s
    => => transferring context: 331B 0.0s
    => [1/4] FROM 59.2s
    => => resolve 0.1s
    => => sha256:a288aa7ad0e4d443e86843972c25a02f99e9ad6ee589dd764895b2c3f5a8340b 1.85kB / 1.85kB 0.0s
    => => sha256:a4b14cf13428c71a5fce6d9380eb2ac1452a7ae4253e594dc90c8a884fdf3db2 529B / 529B 0.0s
    => => sha256:291bf168077c554abde831d52178c76aa8d7dad0e55a2e37a68c2b55a5d92e64 1.46kB / 1.46kB 0.0s
    => => sha256:f2f58072e9ed1aa1b0143341c5ee83815c00ce47548309fa240155067ab0e698 55.04MB / 55.04MB 55.8s
    => => extracting sha256:f2f58072e9ed1aa1b0143341c5ee83815c00ce47548309fa240155067ab0e698 3.1s
    => [2/4] RUN apt-get update && apt-get upgrade -y && apt-get install -y nginx 40.7s
    => [3/4] RUN echo "daemon off;" >> /etc/nginx/nginx.conf 0.7s
    => [4/4] COPY welcome.html /var/www/html/welcome.html 0.1s
    => exporting to image 0.8s
    => => exporting layers 0.7s
    => => writing image sha256:2f381efd4421c834f29bb2ac9bd4764282cb2dc2b236c22af77a916431e7dfe7 0.0s
    => => naming to 0.0s
    Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
  4. Since the process for building docker image is a success, check whether the docker image is already exist by running ‘docker ps’. After that, just run the docker image as in the previous article in ‘How to Run Nginx using Docker Container in Microsoft Windows‘. Below is the execution of the currently build docker image :

    C:\docker\script>docker run -d -p 8383:80 testnginx:latest
  5. Furthermore, check the status of the currently running docker container as it appear below :

    C:\Users\Personal>docker ps
    CONTAINER ID IMAGE             COMMAND                CREATED    STATUS            PORTS                  NAMES
    2fa1231804ca testnginx:latest  "/usr/sbin/nginx"      2 days ago Up About a minute>80/tcp   magical_bell
    d8fc06dd81a5 nginx:latest      "/docker-entrypoint.…" 2 days ago Up 2 hours>80/tcp   web1
  6. Apparently, a new docker container with the previously build docker image is finally running. So, last but not least, just access the Nginx Webserver by typing the URL address of the Nginx Webserver ‘localhost:8383’ in the web browser. The web brower will render following page in it :

    How to Build Nginx Docker Container in Microsoft Windows
    How to Build Nginx Docker Container in Microsoft Windows

Leave a Reply