You've set up your Docker container. Say, for example's sake, you have the tried-and-true Nginx running as a container and it's running well. That's great. You have a basic, containerized web server up and running. Now what do you do? You can always gain access to the container (see my post, How to commit changes to a docker image) and then start building your site within the container; but that adds a layer of complexity to the setup. What if, on the other hand, you could run that container in such a way as to link it to the host's file system? With this type of setup, you could effectively treat the local directory as the Nginx document root and work with a bit more efficiency.
How do you do this? Believe it or not, it's very simple. Let me show you how. I will assume you already have Docker up and running, the Nginx image pulled, the container not running, and you have sudo access to your system. I will be demonstrating on Ubuntu Server 16.10, but this process works, in similar fashion, on any system that runs Docker.
Creating the local directory
Before we run the Nginx container, the local directory must first be created. For this, we'll add the directory /docker-nginx/html with the command sudo mkdir -p /docker-nginx/html. Your local directory is created, however the current set of permissions will not allow the container to read the directory. To fix this, we're going to hand that directory over to the www-data user/group with the command:
sudo chown -R www-data.www-data /docker-nginx/html
The directory is now prepared for the container.
Running the container
It's time to run the Nginx container in such a way that it will be linked to the local file system. This is done with a single command. The command looks like this:
docker run --name docker-nginx -p 80:80 -d -v /docker-nginx/html:/usr/share/nginx/html nginx
The important part of the above command comes after the -v flag. The option can be broken down like this:
LOCAL DIRECTORY:CONTAINER DIRECTORY
In other words, on the left side of the : is our newly-created local directory and on the right side is the container directory (in this instance, the containerized Nginx document root) that will be linked to the local file system.
At this point, you can move all of your web site data into the /docker-nginx/html local directory and build your Docker container-powered Nginx site.
Making Docker even more flexible
Docker is an incredibly powerful tool that can enable your business to do and offer so much more. With a few simple tricks, you can make that Docker system considerably more flexible. Give this process a try and see if it doesn't make your containerized web sites easier to develop.
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.