, ,

More docker = more power? – Part 2: Setting up Nginx and Docker

Moritz Lottermann

This is Part 2 of a series of posts. You can find Part 1 here: https://blog.mi.hdm-stuttgart.de/index.php/2016/01/03/more-docker-more-power-part-1-setting-up-virtualbox/

In the first part of this series we have set up two VirtualBox machines. One functions as the load balancer and the other will house our services. As the next step we want to install docker on the service VM. To do that enter the following commands in the bash:

$ wget -qO- https://get.docker.com/ | sh
$ sudo gpasswd -a <username> docker
$ newgrp docker

This downloads and installs Docker, adds your user to the docker user group and logs you into this new group to allow you to create and run containers.

Since we want to host our test website using nginx we will create a representation of the needed file system in our home directory. We create a directorystructure which looks like a normal Unix filesystem. This filesystem will be copied to our Docker container.
For example if we create a file called “etc/foobar” this file will be copied to “/etc” in our Container. This way we are able to configure everything or add new files, just by putting them in our fake-filesystem.
First create a workspace directory, where you put your Dockerfiles and all docker related stuff. In this folder you can create the fake-fs.

$ mkdir /fs
$ cd fs
$ mkdir -p etc/nginx
$ mkdir -p var/www/html

In our file system we created the folders “/etc/nginx” where we’ll put our customized nginx configuration, and “/var/www/html” where we’ll put the test website.

You can create this website directly in your VM  or upload it with your favorite tool, like SCP.  Simply ensure you put the website in the var/www/html directory of your fake-filesystem.

Next, we want to add a nginx.conf config to fs/etc/nginx/. You can use our Configfile we provied via Pastebin (http://pastebin.com/ZUq6YyRr) Or you can also use the default config of nginx. But keep in mind you have to use the daemon off; parameter to keep nginx running in the foreground.

After we moved our testwebsite to the fake-filesystem we are ready to deploy it and serve it in a container. Therefore, we create a Dockerfile

$ vi Dockerfile

and enter the following commands:

FROM ubuntu:latest
RUN apt-get -y update && apt-get install -y nginx
COPY fs /
RUN chmod -R 755 /var/www/
RUN chown -R www-data:www-data /var/www/
CMD nginx

This creates a new Docker image using Ubuntu as its base, installs nginx, copies all of our files from fs into its root directory, set read/write rights and starts nginx. Keep in mind the Dockerfile needs to be next to your fake-fs. Your directorystructure should look like the this:

+-- Dockerfile
+-- fs
    +-- etc
        +-- nginx
            +-- nginx.conf
    +-- var
        +-- www
            +-- html
                +-- index.html

To create a container from the newly created Dockerfile and run a container from it you can use this two commands:

docker build -t testwebsite:1 .
docker run -d -p 80:80 testwebsite:1

The first one creates an Dockerimage from the Dockerfile in the current directory and calls it testwebsite with the versionnumber 1. The second command creates a container from the given image and runs it in background. The port 80 of the container is mapped to port 80 of your VM.

If you now open your browser and enter the IP of your VM you should see your wonderful testwebsite.

The next part will explain how to create a loadbalancer and automate the creation of multiple containers.


Moritz Lottermann


Leave a Reply