Docker – An apache ‘hello world’ example

Here we’re going to build a simple docker container that is running apache web server and then access it from your laptop’s web browser.

We will be building our container using the official ubuntu docker image.

First off, pull down the ubuntu image, by running:

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
ae79f2514705: Pull complete
5ad56d5fc149: Pull complete
170e558760e8: Pull complete
395460e233f5: Pull complete
6f01dc62e444: Pull complete
Digest: sha256:506e2d5852de1d7c90d538c5332bd3cc33b9cbd26f6ca653875899c505c82687
Status: Downloaded newer image for ubuntu:latest

Then confirm that you have this image now:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              747cb2d60bbe        4 days ago          122MB

Next we need to create a new container which runs the bash terminal in interactive mode. We need to expose port 80 for this container, then we need to map one of our host’s port (e.g. 8080) to the new exposed container port 80.

$ docker run -p 8080:80 -it ubuntu /bin/bash

Note: I’m using a ubuntu image for this example. I normally prefer to use centos, but I ran into a weird bug when using the centos image, so used ubuntu.

I’m now inside this container as indicated above. If you want to exit the container, you simply do:

root@f5842a8d0c33:/# exit

However that ends up stopping your container altogether:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

To start up this particular container, we first find it’s id:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
f5842a8d0c33        ubuntu              "/bin/bash"         3 minutes ago       Exited (0) 2 minutes ago                       naughty_shockley

Then start it by running:

$ docker start f5842a8d0c33

Now confirm that your container is running:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
f5842a8d0c33        ubuntu              "/bin/bash"         11 minutes ago      Up 4 minutes>80/tcp   naughty_shockley

Notice here, that the portwording is also defined.

Now let’s log into our docker container:

$ docker exec -it f5842a8d0c33 /bin/bash

Next we do an apt-get update:

root@f5842a8d0c33:/# apt-get update
Get:1 xenial-security InRelease [102 kB]
Get:2 xenial InRelease [247 kB]
Get:3 xenial-security/universe Sources [50.1 kB]
Get:4 xenial-updates InRelease [102 kB]
Get:5 xenial-backports InRelease [102 kB]
Get:6 xenial/universe Sources [9802 kB]
Get:7 xenial-security/main amd64 Packages [471 kB]
Get:8 xenial-security/restricted amd64 Packages [12.8 kB]
Get:9 xenial-security/universe amd64 Packages [219 kB]
Get:10 xenial-security/multiverse amd64 Packages [2930 B]
Get:11 xenial/main amd64 Packages [1558 kB]
Get:12 xenial/restricted amd64 Packages [14.1 kB]
Get:13 xenial/universe amd64 Packages [9827 kB]
Get:14 xenial/multiverse amd64 Packages [176 kB]
Get:15 xenial-updates/universe Sources [221 kB]
Get:16 xenial-updates/main amd64 Packages [831 kB]
Get:17 xenial-updates/restricted amd64 Packages [13.5 kB]
Get:18 xenial-updates/universe amd64 Packages [691 kB]
Get:19 xenial-updates/multiverse amd64 Packages [17.5 kB]
Get:20 xenial-backports/main amd64 Packages [5176 B]
Get:21 xenial-backports/universe amd64 Packages [6354 B]
Fetched 24.5 MB in 17s (1408 kB/s)
Reading package lists... Done

Now install apache2:

root@f5842a8d0c33:/# apt-get install apache2 -y

Now start apache:

$ root@f5842a8d0c33:/# apachectl start
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using Set the 'ServerName' directive globally to suppress this message

Also enable the service:

root@f5842a8d0c33:/# systemctl enable apache2
apache2.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install enable apache2

Next go to your laptop’s web browser and open http://localhost:8080/.

You should see your an ubuntu/apache welcome page.

This is a quick and dirty way to get off the ground with this work.