Packaging software in docker images

Posted: November 3, 2016 in Linux

By default,when we create docker,it comes without any software installed.
Wouldn’t be nice if we can create predefined docker images (remember,dockers are derived from images),with predefined software.

Let’s start with “empty” docker:

docker run --name web --hostname web -it ubuntu bash

Update docker container and install some software:

root@web:/# apt-get update && apt-get install nano -y && apt-get install -y  net-tools

Now we need to create image from a modified container.It’s not a bad idea to use -a switch that “signs” an image with author string and -m which set a commit message

root@ubuntu:~# docker commit -a "created by me :)" -m "with net-tools" web ubuntu_modified
root@ubuntu:~#  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu_modified     latest              0575f08e5562        4 minutes ago       167.6 MB
registry            2                   c9bd19d022f6        2 weeks ago         33.3 MB
ubuntu              latest              f753707788c5        2 weeks ago         127.2 MB
hello-world         latest              c54a2cc56cbb        4 months ago        1.848 kB
nginx               1.9                 c8c29d842c09        5 months ago        182.8 MB

Now we need to test it:
We’ll remove web container (with net-tools installed)

docker rm -v web

Create new container from modified image:

docker run --name web1 --hostname web1 -it ubuntu_modified bash
root@web1:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:04
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::42:acff:fe11:4/64 Scope:Link
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 B)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

As we can see,ifconfig is available “out of the box”

Exporting and imorting containers

docker export --output web1_export.tar.gz web1

Docker container is exported to tar file

Copying container to another system (with docker installed):

root@ubuntu:~# scp web1_export.tar.gz ja@

Importing and running it in new system

docker import web1 web1_export.tar.gz
docker run -it web1 /bin/bash


Automating docker deployement

First,create a docker file

FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 && apt-get install -y nano && apt-get install net-tools
RUN adduser --disabled-password --gecos '' example
RUN usermod -G $(groups example| sed -e 's/.*: *//' -e 's/ */,/g'),example example
VOLUME /myvolume
COPY /usr/local/
COPY test /myvolume/test
RUN mkdir /in_docker
RUN chown example:example /in_docker
CMD bash -C '/usr/local/';'bash'

FROM:Tells the docker wich image to use

MAINTAINER:who created the image

RUN:commands to execute

VOLUME:create volume in docker container

COPY:copy files/folders from host to docker container

EXPOSE:open port on docker container

CMD:run script on startup on docker container

To summarize:

New docker container will be created,container will be updated,apache,nano and net-tools will be installed,new user and group (example) will be created and user will be added to group.Volume /myvolume will be created,script to start apache service will be copied from host to docker container,and folder test will be also copied to docker container,also /in_docker folder will be created in docker container.

Compose container:

docker build --tag ubuntu-apache .

New image,ubuntu-apache will be created based on docker file in current folder .

Step 1 : FROM ubuntu:latest
latest: Pulling from library/ubuntu
Digest: sha256:2d44ae143feeb36f4c898d32ed2ab2dffeb3a573d2d8928646dfc9cb7deb1315
Status: Downloaded newer image for ubuntu:latest
—> f753707788c5
—> Running in c6a72a1259f8
—> dc08f9805cf6
Removing intermediate container c6a72a1259f8
Step 3 : RUN apt-get update && apt-get install -y apache2 && apt-get install -y nano && apt-get install net-tools
Step 4 : RUN adduser –disabled-password –gecos ” example
—> Running in ec1a36f56ae7
Adding user `example’ …
Adding new group `example’ (1000) …
Adding new user `example’ (1000) with group `example’ …
Creating home directory `/home/example’ …
Copying files from `/etc/skel’ …
—> 1f987549253e
Removing intermediate container ec1a36f56ae7
Step 5 : RUN usermod -G $(groups example| sed -e ‘s/.*: *//’ -e ‘s/ */,/g’),example example
—> Running in 70a33b1a09ff
—> f0875f632bad
Removing intermediate container 70a33b1a09ff
Step 6 : VOLUME /myvolume
—> Running in f794f854779e
—> 9412bd436bb6
Removing intermediate container f794f854779e
Step 7 : COPY /usr/local/
—> ae1b7dccece3
Removing intermediate container 65e578b6a262
Step 8 : COPY test /myvolume/test
—> 15172e733a3b
Removing intermediate container b525ad49fb94
Step 9 : RUN mkdir /in_docker
—> Running in 30954343fbe7
—> 5f6b51d7d249
Removing intermediate container 30954343fbe7
Step 10 : RUN chown example:example /in_docker
—> Running in 3da2b61afe17
—> ec1d1ebdc353
Removing intermediate container 3da2b61afe17
Step 11 : EXPOSE 80
—> Running in 7684950ddb84
—> dd10f2b22e64
Removing intermediate container 7684950ddb84
Step 12 : CMD bash -C ‘/usr/local/’;’bash’
—> Running in 373d8c0e3d2a
—> 056203232f3a
Removing intermediate container 373d8c0e3d2a
Successfully built 056203232f3a

All steps defined in docker file are executed

Now run container based on new image:

docker run --name web -it ubuntu-apache
root@ubuntu:~# docker run --name web -it ubuntu-apache
 * Starting Apache httpd web server apache2                                                                    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using Set the 'ServerName' directive globally to suppress this message

Container is created and script copied from host ( is started (service apache2 start).Also folder copied from host (test) is shown,as well as created folder (in_docker)

root@97b57ef34be9:/# ls
bin   dev  home       lib    media  myvolume  proc  run   srv  tmp  var
boot  etc  in_docker  lib64  mnt    opt       root  sbin  sys  usr

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s