Nginx Inside Docker – Website Root Configuration

Check out ways of setting up website root outside the docker container.

“What the mind can conceive and believe, and the heart desire, you can achieve.” ― Norman Vincent Peale

1. Introduction

In this article here, we have described how to use Nginx with SSL enabled inside a docker container. Not discussed in that article is the aspect of the website root. Once the web server is installed inside the docker container, it has looks for the web site root within the container OS file system. This is controlled by this Nginx clause in the file /etc/nginx/sites-available/default.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/aurora/web;


So once the docker container is running, how can we update the website root directory? Well, there are a couple of options.

2. Copy Files from Host to Container

One option is to update the file system inside the container by copying files from the host to the container.

First look up the id of the running container into which you want to copy the files.

docker ps -f ancestor=mynginx
# prints
CONTAINER ID        IMAGE               COMMAND
06d5ad47eba0        mynginx:latest      "/usr/sbin/nginx -..."

Now copy files from the host to the container. Assuming the website root is the directory website in the current directory:

docker cp website 06d5ad47eba0:/home/aurora/web

Now you can check that your web site is operating correctly.

3. Mount Host Directory

A second option is to directly mount a host directory onto the container. Using this option means changes on the host are transparently visible to the container, and vice versa. In some cases, this situation offers more flexibility than copy files into the container.

You need a change to the Dockerfile to use this method. You have to declare the directory in the Dockerfile as a volume.

For full details of the Dockerfile, refer to this article.

COPY nginx/snippets /etc/nginx/snippets
COPY nginx/sites-available /etc/nginx/sites-available
VOLUME /home/aurora/web
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]

Once the image is built, you need to inform Docker of the host directory you want to mount at the point specified by VOLUME. This is done using the run command.

docker run -p 80:80 -p 443:443  -v /home/aurora/server/docker/nginx/website/:/home/aurora/web mynginx:latest

Now, you will find that whatever changes are made under the host directory is visible in the container, and vice versa.

If you like, you can run another container on a different port, and possibly different web server root (may be for beta testing).

docker run -p 8080:80 -p 8443:443  -v /var/www/html:/home/aurora/web mynginx:latest


In this article, we learned how to connect a host directory to a docker container and have the Nginx web server in the container serve these files. Once you have prepared an image (such as what we described here and in the previous article), you can tailor the docker run command to securely serve out files on demand from a specific directory. The flexibility that this solution offers is immense, in that you do not have to “contaminate” the host machine with software required for running a web server. You can also run multiple instances of the web site on different ports (say, for beta testing).

If you are not yet using Docker in your software development and deployment, you should seriously consider doing so. Please let us know your thoughts in the comments section below.

Leave a Reply

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