Docker Nginx Web Server - file sharing/copying & Dockerfile
This will be a very brief tutorial on Docker: we'll take a "nginx" docker image and build a simple web server on Docker container. In that process, we can get a quick taste of how Docker is working.
- How to get an official image.
- How to use the files on host machine from our container.
- How to copy the files from our host to the container.
- How to make our own image - in this tutorial, we'll make a Dockerfile with a page for render.
To create an instance of Nginx in a Docker container, we need to search for and pull the Nginx official image from Docker Hub. Use the following command to launch an instance of Nginx running in a container and using the default configuration:
$ docker run --name my-nginx-1 -P -d nginx fe693e1455bf1923abe416b636fc2d0250c0d7332dc25a76a08714e12961f022
The command creates a container named my-nginx-1 based on the Nginx image and runs it in "detached" mode, meaning the container is started and stays running until stopped but does not listen to the command line. We will talk about this later how to interact with the container.
The Nginx image exposes ports 80 and 443 in the container and the -P option tells Docker to map those ports to ports on the Docker host that are randomly selected from the range between 49153 and 65535.
We do this because if we create multiple Nginx containers on the same Docker host, we may induce conflicts on ports 80 and 443. The port mappings are dynamic and are set each time the container is started or restarted.
If we want the port mappings to be static, set them manually with the -p option. The long form of the "Container Id" will be returned.
We can run docker ps to verify that the container was created and is running, and to see the port mappings:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fe693e1455bf nginx "nginx -g 'daemon off" 5 minutes ago Up 5 minutes 0.0.0.0:32771->80/tcp, 0.0.0.0:32770->443/tcp my-nginx-1
We can verify that Nginx is running by making an HTTP request to port 32771 (reported in the output from the preceding command as the port on the Docker host that is mapped to port 80 in the container), the default Nginx welcome page appears:
$ curl http://localhost:32771 <!DOCTYPE html> <html> <head> ... </html>
Now that we have a working Nginx Docker container, how do we manage the content and the configuration? What about logging?
It is common to have SSH access to Nginx instances, but Docker containers are generally intended to be for a single purpose (in this case running Nginx) so the Nginx image does not have OpenSSH installed and for normal operations there is no need to get shell access directly to the Nginx container. We will use other methods supported by Docker rather than using SSH.
When the container is created, we can tell Docker to mount a local directory on the Docker host to a directory in the container.
The Nginx image uses the default Nginx configuration, so the root directory for the container is /usr/share/nginx/html. If the content on the Docker host is in the local directory /var/www, we run the command:
$ docker run --name my-nginx-2 \ -v /var/www:/usr/share/nginx/html:ro -P -d nginx $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 35a4c74ff073 nginx "nginx -g 'daemon off" 7 seconds ago Up 2 seconds 0.0.0.0:32785->80/tcp, 0.0.0.0:32784->443/tcp my-nginx-2
Now any change made to the files in the local directories /var/www on the Docker host are reflected in the directories /usr/share/nginx/html in the container. The :ro option causes these directors to be read only inside the container.
Rather than using the files that kept in the host machine, another option is to have Docker copy the content and configuration files from a local directory on the Docker host when a container is created.
Once a container is created, the files are maintained by creating a new container when the files change or by modifying the files in the container.
A simple way to copy the files is to create a Dockerfile to generate a new Docker image, based on the Nginx image from Docker Hub. When copying files in the Dockerfile, the path to the local directory is relative to the build context where the Dockerfile is located. For this example, the content is in the content directory under the same directory as the Dockerfile.
Here is the Dockerfile:
FROM nginx COPY content /usr/share/nginx/html
We can then create our own Nginx image by running the following command from the directory where the Dockerfile is located:
$ docker build -t my-nginx-image-1 . Sending build context to Docker daemon 4.096 kB Step 1 : FROM nginx ---> cc1b61406712 Step 2 : COPY content /usr/share/nginx/html ---> c64adc1cdc0e Removing intermediate container 3e56e2ebe70b Successfully built c64adc1cdc0e
Note the period (".") at the end of the command. This tells Docker that the build context is the current directory. The build context contains the Dockerfile and the directories to be copied.
Now we can create a container using the image by running the command:
$ docker run --name my-nginx-3 -P -d my-nginx-image-1 38fdde3e7b953661b50d4a06b22bd86430cb2d9bcb19a47a1e7a1e65f45a1211 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 38fdde3e7b95 my-nginx-image-1 "nginx -g 'daemon off" 7 seconds ago Up 3 seconds 0.0.0.0:32787->80/tcp, 0.0.0.0:32786->443/tcp my-nginx-3
As we know, we are not able to get SSH access to the Nginx container, so if we want to edit the container files directly we can use a helper container that has shell access.
In order for the helper container to have access to the files, we must create a new image that has the proper volumes specified for the image.
Assuming we want to copy the files as in the example above, while also specifying volumes for the content and configuration files, we use the following Dockerfile:
FROM nginx COPY content /usr/share/nginx/html VOLUME /usr/share/nginx/html
We then create the new Nginx image (my-nginx-image-2) by running the following command:
$ docker build -t my-nginx-image-2 .
Now we create an Nginx container (my-nginx-4) using the image by running the command:
$ docker run --name my-nginx-4 -P -d my-nginx-image-2
We then start a helper container with a shell and access the content and configuration directories of the Nginx container we created in the previous example by running the command:
$ docker run -i -t --volumes-from my-nginx-4 --name my-nginx-4-files debian /bin/bash root@9820031675b5:/#
This creates an interactive container named my-nginx-4-files that runs in the foreground with a persistent standard input (-i) and a tty (-t) and mounts all the volumes defined in the container my-nginx-4 as local directories in the my-nginx-4-files container.
After the container is created, it runs the bash shell, which presents a shell prompt for the container that we can use to modify the files as needed.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization