Docker – the DockerFile

In the last post Link, I talked about Docker very very briefly and set up a series of containers to play around with it. In this post we will take a look at the DockerFile, Docker Hub and set up a RabbitMQ (AMQP Message broker) with a DockerFile and push it to a public repository for the world’s use.

Theory

Docker can act as a builder and read instructions from a plain text file called Dockerfile to automate the steps you would otherwise take manually to create an image.  Basically what we will do is Create a DockerFile and execute docker build to create an image from which we will create a container afterwards. The docker file will look like this:

# USE UBUNTU
FROM ubuntu:trusty

MAINTAINER Marc Lopez Rubio <marc5.12@outlook.com>

# INSTALL RABBITMQ
RUN apt-get -qq update && apt-get install -qq -y rabbitmq-server

# ENABLE MANAGEMENT INTERFACE
RUN rabbitmq-plugins enable rabbitmq_management

#COPY CONFIG FILE
ADD rabbitmq.config /etc/rabbitmq/rabbitmq.config

# EXPOSE RABBITMQ PORT AND MANAGEMENT UI PORT
EXPOSE 5672 15672

# SET THE CONTAINER ENTRYPOINT TO THE RABBITMQ EXECUTABLE
ENTRYPOINT /usr/sbin/rabbitmq-server

I want to highlight that it’s usage is very very similar to what you would execute on the shell with a INSTRUCTION in front of the command, there are various INSTRUCTION we can use in the Dockerfile:

  • FROM: Used to tell docker which base image to use, in this case we used the Ubuntu trusty image (14.04) (IMAGE:TAG format)
  • MAINTAINER: Used to set the author of the Docker file
  • RUN: Used to run commands with the Linux shell (/bin/sh -c)
  • ADD: used to add a file from a URL or Path to the specified path
  • EXPOSE: Used to expose a listening container port to the host
  • ENTRYPOINT: Used to use that container as an executable, there can only be one ENTRYPOINT on a Docker file, if there’s more than one the last one will be used.
  • ENV: Used to set Environment variables.
  • USER: Used to set the user from which to run the RUN instructions and executing the ENTRYPOINT, etc.
  • WORKDIR: Used to set the workdir for the RUN, ENTRYPOINT and CMD instructions
  • #: To indicate that it’s a comment just as in Bash scripts.

Hands on

First you’ll have to create a Docker Hub account (if you haven’t already). Then we will create a folder on which we will set up our Docker projects in my case it is /home/marc/docker/rabbitmq, and create a file called “Dockerfile”, then we’ll create the RabbitMQ file

RABBITDOCKER=/home/marc/docker/rabbitmq
mkdir -p $RABBITDOCKER && cd $RABBITDOCKER
vi Dockerfile
vi rabbitmq.config

The Dockerfile will contain:

# USE UBUNTU
FROM ubuntu:trusty

MAINTAINER Marc Lopez Rubio <marc5.12@outlook.com>

# INSTALL RABBITMQ
RUN apt-get -qq update && apt-get install -qq -y rabbitmq-server

# ENABLE MANAGEMENT INTERFACE
RUN rabbitmq-plugins enable rabbitmq_management

#COPY CONFIG FILE
ADD rabbitmq.config /etc/rabbitmq/rabbitmq.config

# EXPOSE RABBITMQ PORT AND MANAGEMENT UI PORT
EXPOSE 5672 15672

# SET THE CONTAINER ENTRYPOINT TO THE RABBITMQ EXECUTABLE
ENTRYPOINT /usr/sbin/rabbitmq-server

The rabbitmq.config will contain:

[
{rabbit, [
{default_user, <<“admin”>>},
{default_pass, <<“adminpass”>>},
{tcp_listeners,[{“0.0.0.0”,5672}]}
]}
].

Now we have our Working directory with all the necessary elements for us to work with. The above Dockerfile will:

  • Use the Ubuntu 14.04 LTS
  • Install the rabbitmq server
  • Enable the management UI
  • Copy our RabbitMQ Configuration
  • Expose the 5672 and 15672 Ports to the host IP
  • Set the container ENTRYPOINT to the rabbitmq-server executable which will start the RabbitMQ Server.

Now we build the image, run it and test it🙂

marc@ubuntu-docker:~/docker/rabbitmq$ docker build -t marclop/rabbitmq .
Sending build context to Docker daemon 3.584 kB
Sending build context to Docker daemon


Removing intermediate container ea2fee4f0701
Successfully built aa7ad24e132c
marc@ubuntu-docker:~/docker/rabbitmq$ docker images
REPOSITORY               TAG                 IMAGE ID                 CREATED                 VIRTUAL SIZE
marclop/rabbitmq    latest              aa7ad24e132c        57 seconds ago      318.3 MB

marc@ubuntu-docker:~/docker/rabbitmq$ docker run -d -p 5672:5672 -p 15672:15672 marclop/rabbitmq
b947c82ac58f19a49bb5ada12e8ec17701070d8d66b0657d04f0f9d3e1ec6dc9
CONTAINER ID      IMAGE                                         COMMAND                     CREATED                STATUS                  PORTS                                                                                                      NAMES
b947c82ac58f        marclop/rabbitmq:latest   /bin/sh -c /usr/sbin   2 minutes ago       Up 2 minutes        0.0.0.0:5672->5672/tcp, 0.0.0.0:15672->15672/tcp   compassionate_goodall

We used the -d flag with the Docker RUN command to tell Docker that this container will be run in the background (dettached). We will check the logs to see if everything went well.

marc@ubuntu-docker:~/docker/rabbitmq$ docker logs b947c82ac58f

RabbitMQ 3.2.4. Copyright (C) 2007-2013 GoPivotal, Inc.
##  ##                  Licensed under the MPL.  See http://www.rabbitmq.com/
##  ##
##########  Logs: /var/log/rabbitmq/rabbit@b947c82ac58f.log
######      ##              /var/log/rabbitmq/rabbit@b947c82ac58f-sasl.log

##########
Starting broker… completed with 6 plugins.

Seems to be working all right, we will point our browser to the WebUI to see if it is in fact running.

RabbitMQ Web

 

That did work well. Now let’s publish our shiny RabbitMQ Ubuntu image to the Docker Hub for everyone’s use.

Publishing the image

First we’ll have to log in to our previously created Docker Hub account with the docker login command and then push the image to our repo.

marc@ubuntu-docker:~/docker/rabbitmq$ docker login
[sudo] password for marc:
Username (marclop):
Login Succeeded
marc@ubuntu-docker:~/docker/rabbitmq$ docker push marclop/rabbitmq
The push refers to a repository [marclop/rabbitmq] (len: 1)
Sending image list
Pushing repository marclop/rabbitmq (1 tags)
..
Pushing tag for rev [aa7ad24e132c] on {https://registry-1.docker.io/v1/repositories/marclop/rabbitmq/tags/latest}

That’s it! We have our image in a public repository available for the entire world, you can also create a description for the repository, link an existing Github repository to a Docker one, and create automatic triggers to auto-build a Docker image when a commit is made to the Github repository which is much much cooler.

Some more bits and pieces

I wanted to include some nice stuff about docker. Here’s the output of the Process hirearchy:

 marc@ubuntu-docker:~$ ps fax
PID TTY      STAT   TIME COMMAND

12047 ?        Ssl    2:37 /usr/bin/docker -d
16003 ?        Ss     0:00  \_ /bin/sh -c /usr/sbin/rabbitmq-server
16019 ?        S      0:00      \_ /bin/sh /usr/sbin/rabbitmq-server
16027 ?        S      0:00      |   \_ su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
16028 ?        Ss     0:00     |       \_ sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
16029 ?        Sl     0:05      |           \_ /usr/lib/erlang/erts-5.10.4/bin/beam -W w -K true -A30 -P 1048576 — -root /usr/lib/erlang -progname erl — -home /var/lib/rabbitmq — -pa /usr/lib/rabbitmq/lib/rab
16101 ?        Ss     0:00      |               \_ inet_gethost 4
16113 ?        S       0:00      |                   \_ inet_gethost 4
16045 ?        S      0:00      \_ /usr/lib/erlang/erts-5.10.4/bin/epmd -daemon

Also, because I am very curious I wanted to see how long it took to spin up our new container

marc@ubuntu-docker:~$ time docker run -d -p 5672:5672 -p 15672:15672 marclop/rabbitmq
fe3d28fe5213333e2e7a8efad1223936ee743569c7d765180d9390a7034ae5a5

real 0.063s

It took just 0.06s!!!!! 0.06s… no way you can match that with a VM🙂 Docker is blazing fast.

 

I hope you liked this post and keep reading our blog, it is much appreciated.

 

Docker Series Index:

  1. Docker Introduction
  2. Docker – the DockerFile

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s