Docker tutorials 6: installing Apache2 on docker (on a Mac) – part 1

We need to

  • install apache2 on the container
  • start it
  • access its port (80)

Now things get a bit complex: remember that on a Mac we need to run a VirtualBox to have a Linux VM onto which docker acts. So we need the container to expose the port to its host (VirtualBox), and VirtualBox has to expose it to the Mac. To find out the IP address of VirtualBox, we execute

docker-machine ls
NAME     ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER   ERRORS
default   *       virtualbox   Running   tcp://           v1.10.2

and we discover that the Mac sees the VirtualBox as so this is the address we’ll need to use to access our Apache once that’s installed.

As a first step, when we activate the container onto which we want to install apache2, we need to make the port 80 visibile from the host machine

docker run --name myLinux2 -p -it ubuntu:latest bash

The switch -p maps the port 80 in the container to port 8000 exposed by the VirtualBox on the host machine: so now we have the full info about where to find Apache once that’s installed: it will be on

NOTE: it is really important that we use a Mac. If we had a Linux PC instead, we’d use, and to see the page from the browser we’d use localhost:8000.

The rest is easy. On our running “myLinux2” container we just need install Apache, so (after the usual to execute apt-get update and apt-get –y upgrade that we found in Tutorial 1) we execute

apt-get install -y apache2

Once it is done, we need to start it.

We say

apache2ctl start

then we run a browser on our Mac pointing at, et voilà, we get the standard home page.

WATCH OUT: it is apache2ctl, NOT apachectl (since we’ve installed the most recent Apache versione: 2.4).

We’ll need to do some customization. Surely we want to customize the directory where we serve from, and most likely also the Apache configuration.

That is simply done, by using the file system sharing we introduced in Tutorial 5.

As we had to plan in advance that we wanted to share a port, and we declared it when starting the container, we’ll have to plan in advance the file system sharing.

So we’ll need to add to the docker run command the following switch:

-v /full/path/to/mySharedDirectory:/var/www/html

if we keep the default, that the web home directory is /var/www/html.

If we want to change the something in the configuration, we can do it inside the container (editing the files in /etc/apache2/) , or outside, by preparing a mapping of the files we want to change onto our host machine file system.

All this was done by hand. Of course it is much more practical to do it via Dockerfile, and this is what we are going to discuss in next tutorial.



Tagged with: ,
Posted in Docker

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: