I Wanted Orange (The machine would not make a mistake.)

16Nov/16Off

Refreshing my LAMP stack with Docker

Over the years my home desktop has acquired many layers of development tools, to the extent where I keep things around "just in case" but in reality the project or experiment is definitely over. (Who needs disk-images of a Slackware server from 2001?) I've decided to do some spring-cleaning and install new versions.

Part of that means (re)installing Docker on Windows 7, and I've decided to journal this process mainly so I can refer to what I did (or didn't) do later.  I'm going to assume that if you're still reading this, you have a vague idea what Docker is and where it stands relative to Vagrant, Virtualbox, and the development benefits of virtual-machinery in general.

Step one: Install Docker Toolbox (and VirtualBox)

For Windows 7 you'll want Docker Toolbox, since it seems the flagship Docker Platform is designed around features in Windows 10. (And for various reasons, I myself do not want to upgrade.)

When you install Docker Toolbox (in my case, v1.12.2) it will automatically include VirtualBox (5.1.6) as a workhorse behind the scenes. Effectively it has to hose a teeny tiny VM which, in turn, can host the core components of Docker. In the process I was prompted to install some device drivers from Oracle for things like USB support and network drivers.

When it's done, you should have Start Menu entries for VirtualBox (which can be used on its own without Docker) and a shortcut called "Docker Quickstart Terminal" should exist on your desktop. Run it.

You may be prompted to allow VirtualBox to make some changes -- it's probably setting up all the network-adapter magic that lets your virtual machines talk to one-another and reach out to download things from the internet. (Often necessary, when installing.)

Once it's done, you should see an ASCII-art whale (with ominous growths on its back) and a prompt. So let's run the its standard "Hello World" test image in a new container:

User@Desktop MINGW64 ~/
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Step two: A simple docker-compose

Create a new folder somewhere for the project, and create a new file called docker-compose.yml  , which will define which host(s) we need for a full installation of, well, whatever.

Initially, let's choose one PHP web node, running a pre-release copy of PHP-7.1, and let's structure it so that we can customize that image a little bit.

version: '2'
services:   
   webserver:
     build:
      context: ./image_web
     ports:
       - "8000:80"
     restart: always
     volumes:  
       - ./src/:/var/www

This sample defines a new host called webserver, and in order to create it docker-compose will go try to build ./image_web/Dockerfile , and when it runs the data in ./src/ will be mounted.

So go ahead and create the folder image_web, and inside place a Dockerfile that just says:

FROM php:7.1-rc-apache

At the moment this is a pointless level of indirection, but later it'll help us layer on customizations.

Next, create the folders and file for src/html/index.php , the "html" portion is due to the server setup defined by the existing php:7.1-rc-apache image. To test that PHP is running right, try:

Hello dockerized world!
<?php
phpinfo();

Step three: Bringing everything up and testing

Back to the command-line, let's check that we have nothing currently running:
User@Desktop MINGW64 ~/docker-demo
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED
STATUS              PORTS               NAMES

So let's ask docker-compose to build everything and start up our tiny world of one webserver. You'll probably see much much more output than this as your computer downloads what it needs for the php:7.1-rc-apache image.

User@Desktop MINGW64 ~/docker-demo
$ docker-compose up -d
Building webserver
Step 1 : FROM php:7.1-rc-apache
 ---> dd39d6dff6fd
Successfully built dd39d6dff6fd
WARNING: Image for service webserver was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
dockerphp_webserver_1 is up-to-date

To figure out what URL to use in your browser, first we should find the IP address that the docker virtual-machine (one-layer out from the containers) is using. You can do that with:

User@Desktop MINGW64 ~/docker-demo
$ docker-machine.exe ip
192.168.99.100

As for the port number, remember the docker-compose.yml  file? It contained a line saying that the container's port-80 should be mapper to port-8000 on the outside, so that means our combined URL is http://192.168.99.100:8000/

Visiting that, you ought to see "Hello dockerized world!" and a PHPInfo output.

Now, we used the -d argument to docker-compose which means the webserver will keep running. To turn it off, simply go:

User@Desktop MINGW64 ~/docker-demo
$ docker-compose stop
Stopping dockerphp_webserver_1 ... done

Epilogue: Cleaning up

What if you want to get rid of this stuff and try some things over? First, you can get a list of the images docker knows about like so:

User@Desktop MINGW64 ~/docker-demo/image_web
$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
dockerphp_webserver   latest              dd39d6dff6fd        2 days ago          406.3 MB
php                   7.1-rc-apache       dd39d6dff6fd        2 days ago          406.3 MB
hello-world           latest              c54a2cc56cbb        4 months ago        1.848 kB

Let's try removing the hello-world stuff from before:

User@Desktop MINGW64 ~/docker-demo
$ docker rmi hello-world:latest
Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container 560e895d696c is using its referenced i
mage c54a2cc56cbb

Uh oh, what happened? Well, the hello-world container we created is still around, even if it isn't actively running. We can see it here:

User@Desktop MINGW64 ~/docker-demo
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
cf5dbd9936a7        dd39d6dff6fd        "apache2-foreground"   22 minutes ago      Exited (0) 6 minutes ago                       dockerphp_webserver_1
560e895d696c        hello-world         "/hello"               46 hours ago        Exited (0) 46 hours ago                        small_pasteur

And remove it with:

User@Desktop MINGW64 ~/docker-demo
$ docker rm 560e895d696c
560e895d696c

Now when we remove the image, things are smoother:

User@Desktop MINGW64 ~/docker-demo
$ docker rmi hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Deleted: sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc
Deleted: sha256:a02596fdd012f22b03af6ad7d11fa590c57507558357b079c3e8cebceb4262d7
Tagged as: , Comments Off