Vous êtes ici

First steps with Docker - 2

dockerPrevious article

Creating an Apache image

My first target will be to create an Apache image that I can run on the Mac, and then transfer to the server on the Internet, and run there. So, let's go!

First, I pull an Ubuntu 14.04 image:

docker pull ubuntu:14.04

Resulting download is more than 200 MB large.

Then:

  • run the image: docker run -t -i ubuntu:14.04 /bin/bash
  • update the Ubuntu distribution and install a small text editor:
apt-get update
apt-get upgrade
apt-get install nano
  • install Apache 2:
apt-get install apache2
apt-get install php5 libapache2-mod-php5
apt-get install php5-mysql

An error message is displayed by the first two commands:

invoke-rc.d: policy-rc.d denied execution of start.

This page gives a solution: edit contents of /usr/sbin/policy-rc.d, to replace exit 101 by exit 0.

  • start Apache: service apache2 restart
  • exit the container: exit
  • commit a copy of this container to a new image:
docker commit -m="Added Apache2" -a="<author>" \
<imageId> <userid>/apache2:v1

<imageId> is the id that was displayed by terminal prompt after the docker run -t -i ubuntu:14.04 /bin/bash command had been issued. <userid> can be the user id you registered on Docker Hub, if you did it, or another string.

Running the Apache image on the development machine

Now, one way to test our containerized Apache server on the Mac is to use the docker run command first, to start a container based on our image:

docker run -t -p 80:80 -i <userid>/apache2:v1 /bin/bash

The -p option allows to map a machine port to the port used by the Apache server.

Then, from inside the container:

service apache2 start

Get the IP address to be used to reach the web server:

boot2docker ip

And then, using your preferred web browser, visit this IP address: you get the default Apache web page.

Running the Apache image on the server

Now, let's go one step further. On the brand new server, where I just installed Ubuntu Server 14.04, I install Docker, as described in Docker documentation.

Then:

  • pull the Ubuntu:14.04 image
  • perform an update
  • on the development machine, save the docker image:
docker save -o apache2 <userid>/apache2:v1
  • copy the resulting apache2 tar file to the server
  • on the server, import the image:
docker load -i apache2
  • then run it, and start Apache service:
docker run -t -p 80:80 -i <userid>/apache2:v1 /bin/bash
service apache2 start

That's it!

Next article