Cookie Consent by TermsFeed

Docker-ize your Services - Part 2 - Web Server

Docker-ize your Services - Part 2 - Web Server

Now we have a proxy server that is capable of serving pages both via http and https. We just need to create something to be served.

We can use docker both for static and dynamic websites. We have a wide choice of images - and we can even create our own!

Let’s start with a simple example: a static website served by a nginx image. We’ll use the stock nginx container. Let’s create a simple “hello world” html page:

Let’s put a file called “index.html” into the directory “/tmp/helloworld” of the host, containing:

<div align=”center”>Hello World!</div>

Save and exit. Now we’re ready to launch the container. As you already have the proxy and letsencrypt containers running, we’ll just need to create a new container for this content and pass the environment variables to the proxy:

docker run -d --restart=unless-stopped --name helloworld -e 'LETSENCRYPT_EMAIL=me@mymail'  \
-e '' \
-e '' \
-v '/tmp/helloworld:/usr/share/nginx/html:ro' nginx

So we’ll be mounting the host directory “/tmp/helloworld” as “/usr/share/nginx/html” (web root) of the new nginx container and, after launching it, our helloworld page will be reachable, connecting to the domain we’ve just set (of course, a correct dns mapping must be present, otherwise it will fail to generate the certificate).

In case you don’t want to map a domain and just want to see this website on a specific port, you can just create the container this way (one line):

docker run -d --restart=unless-stopped --name helloworld -p8080:80
-v '/tmp/helloworld:/usr/share/nginx/html:ro' nginx

This kind of setup will not need the proxy and the companion containers and will just launch a nginx container (remapping from host port 8080 to container port 80). It may be useful if you just want to perform some tests.

Now we have static web sites, we can perform some tests with dynamic websites.

Let’s remove the index.html we created before and create a simple index.php in the same directory.


Now, let’s launch the official php container with apache on local port 8081 (one line):

docker run -d --restart=unless-stopped --name php -p8081:80
-v '/tmp/helloworld:/var/www/html/:ro' php:7.4-apache

Pointing your web browser to port 8081 you will see the correctly rendered index.php page.

Well done, you have both static and dynamic setup ready to be deployed. Feel free to publish as many websites as you want - every website will be in its container and with its specific process, php engine, etc. You can also mix and match, so have some static websites, some dynamic, some with php 7.4, some with php 7.2, all in the same server.

See also