Let’s run a basic web server using the official NGINX image. Run the following command to start the container.
$ docker run -it --rm -d -p 8080:80 --name web nginx
With the above command, you started running the container as a daemon (-d) and published port 8080 on the host network. You also named the container web using the --name option.
Open your favorite browser and navigate to http://localhost:8080 You should see the following NGINX welcome page.
This is great but the purpose of running a web server is to serve our own custom html files and not the default NGINX welcome page.
Let’s stop the container and take a look at serving our own HTML files.
$ docker stop web
Adding Custom HTML
By default, Nginx looks in the /usr/share/nginx/html directory inside of the container for files to serve. We need to get our html files into this directory. A fairly simple way to do this is use a mounted volume. With mounted volumes, we are able to link a directory on our local machine and map that directory into our running container.
Let’s create a custom html page and then serve that using the nginx image.
Create a directory named site-content. In this directory add an index.html file and add the following html to it:
Hello from Nginx container
Now run the following command, which is the same command as above, but now we’ve added the -v flag to create a bind mount volume. This will mount our local directory ~/site-content locally into the running container at: /usr/share/nginx/html
$ docker run -it --rm -d -p 8080:80 --name web -v ~/site-content:/usr/share/nginx/html nginx
Open your favorite browser and navigate to http://localhost:8080 and you should see the above html rendered in your browser window.