Using ngix docker to host static files
Minbin Jiang Lv4

Induction

As needs to work in a new role for realse management. Recently, I’ve been trying to know about some main CI/CD tools in the market, and container technology as well.
When talking about the container, invetably we have to mention the Docker. Docker in some way it’s almost equals to container technology. Acutally, couple of years ago, was curious about what Docker would help to my daily work, and was trying to setup docker environment multiple times, however, everytime gave up finally due to different kinds of reasons, For example, initiall there was no windows version docker, it was a bit tricky to download the sources when configuing a linux docker, especially i was in China back then, after windows version relased i once got stuck at how to share/mount windows directory to linux docker container. Anyway, I was prevented succesfully utilising docker for years.

This time i’d like to have a try again, a simple case comes to my mind to explore the docker functionalities that is using nginx docker image to host static files locate in my host machine with window OS.

Basic idea is to mount a windows folder with static file into a nginx linux container. Pretty simple though.

This post is finished based on environment as:

Docker desktop community version: 2.1.0.3(38240)
Windows 10

Detailed steps

  • create a working folder

D:\dockerProjects\nginx

WithIn the nginx, create a folder named content assuming all the static files in this folder will be hosted.

  • Put a static file within content diretory

greeting.txt(“Hello world”) with only one line text created.

  • mount content folder and start the container

docker run –name test_nginx -d -p 4444:80 -v D:\dockerProjects\nginx\content:/usr/share/nginx/html:ro -d nginx

start_the_container.PNG

About commandline doing things:

  1. mount the host machine location (D:\dockerProjects\nginx\content) to linux container location(/usr/share/nginx/html)
  2. map host port(4444) to linux container port(80)
  3. create a container with name test_nginx
  4. start the container

Hello world is expected to display if everything goes fine.

Creating a container, there is another way by using Dockerfile, What if we’d like to use the Dockerfile to create the container?

  • Prepare a content file

In working directory(D:\dockerProjects\nginx) create a content folder content2 and put greetings2.txt within the folder.

  • Create a dockerfile

    Create a text file, rename the file name to Dockerfile without any extension in working directory.

  • Edit the dockerfile
    Put below lines into the file and save

    FROM nginx
    COPY content2 /usr/share/nginx/html
  • Run the command to build a image based on the Dockerfile

docker build -t nginx5 .

In this step, the Dockerfile will be validated line by line top-down.

docker_build.PNG

  • Start the image with a container

docker run -p 4445:80 -d nginx5

Now ideally, we should able to access the http://localhost:4445/greeting2.txt.

Quite possible we’d like to enter into nginx linux container, e.g, checking whether or not the static files successfully mounted to the specified location (/usr/share/nginx/html).

docker exec -it container_name bash

As needs to specify the container name would like to enter in, we need to find out the container name that image nginx5 sits in with command line:

docker ps

docker_ps.PNG

Replacing the container_name and run the command line

docker exec -it dazzling_bartik bash

Now a linux bash mode started.

More Reading

nginx docker image help