Manage Linux Containers with Docker on Ubuntu - Page 2

4.2 Building images with a Dockerfile

The Dockerfile uses a basic DSL with instructions for building Docker images. We then use the docker build command to build a new image from the  instructions in the Dockerfile. The team at Docker, Inc., have also published a Dockerfile tutorial to help you learn how to build Dockerfiles here.

Now  I will create a simple docker file which can be further enhanced as per the user's requirement. First make a directory suppose as dir=kishore:

mkdir kishore
cd kishore

Now create a Dockerfile as:

vi Dockerfile

& enter the content as follows:

FROM ubuntu:12.04
MAINTAINER Srijan Kishore <[email protected]>
RUN apt-get -qq update
RUN apt-get -qqy install apache2

Now to open the customized container use:

sudo docker build -t="srijan/custom1" .

It will yield as follows:

[email protected]:~/kishore$ sudo docker build -t="srijan/custom1" .
[sudo] password for srijan: 
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:12.04
Pulling repository ubuntu
ea7d6801c538: Download complete 
511136ea3c5a: Download complete 
65b7e9ccb809: Download complete 
f8dd6bd14f58: Download complete 
a343823119db: Download complete 
 ---> ea7d6801c538
Step 1 : MAINTAINER Srijan Kishore <[email protected]>
 ---> Running in a6135f405eb4
 ---> be97c22efa82
Removing intermediate container a6135f405eb4
Step 2 : RUN apt-get -qq update
 ---> Running in b5681cd85ba8
 ---> cbc3a95de894
Removing intermediate container b5681cd85ba8
Step 3 : RUN apt-get -qqy install apache2
 ---> Running in 5765c09b530f
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libsqlite3-0.
(Reading database ... 7551 files and directories currently installed.)
Unpacking libsqlite3-0 (from .../libsqlite3-0_3.7.9-2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libroken18-heimdal.
Unpacking libroken18-heimdal (from .../libroken18-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libasn1-8-heimdal.
Unpacking libasn1-8-heimdal (from .../libasn1-8-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libgpg-error0.
Unpacking libgpg-error0 (from .../libgpg-error0_1.10-2ubuntu1_amd64.deb) ...
Selecting previously unselected package libgcrypt11.
Unpacking libgcrypt11 (from .../libgcrypt11_1.5.0-3ubuntu0.2_amd64.deb) ...
Selecting previously unselected package libgdbm3.
Unpacking libgdbm3 (from .../libgdbm3_1.8.3-10_amd64.deb) ...
Selecting previously unselected package libp11-kit0.
Unpacking libp11-kit0 (from .../libp11-kit0_0.12-2ubuntu1_amd64.deb) ...
Selecting previously unselected package libtasn1-3.
Unpacking libtasn1-3 (from .../libtasn1-3_2.10-1ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libgnutls26.
Unpacking libgnutls26 (from .../libgnutls26_2.12.14-5ubuntu3.8_amd64.deb) ...
Selecting previously unselected package libhcrypto4-heimdal.
Unpacking libhcrypto4-heimdal (from .../libhcrypto4-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libheimbase1-heimdal.
Unpacking libheimbase1-heimdal (from .../libheimbase1-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libwind0-heimdal.
Unpacking libwind0-heimdal (from .../libwind0-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libhx509-5-heimdal.
Unpacking libhx509-5-heimdal (from .../libhx509-5-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libkrb5-26-heimdal.
Unpacking libkrb5-26-heimdal (from .../libkrb5-26-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libheimntlm0-heimdal.
Unpacking libheimntlm0-heimdal (from .../libheimntlm0-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libgssapi3-heimdal.
Unpacking libgssapi3-heimdal (from .../libgssapi3-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libsasl2-2.
Unpacking libsasl2-2 (from .../libsasl2-2_2.1.25.dfsg1-3ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libldap-2.4-2.
Unpacking libldap-2.4-2 (from .../libldap-2.4-2_2.4.28-1.1ubuntu4.4_amd64.deb) ...
Selecting previously unselected package libcap2.
Unpacking libcap2 (from .../libcap2_1%3a2.22-1ubuntu3_amd64.deb) ...
Selecting previously unselected package libexpat1.
Unpacking libexpat1 (from .../libexpat1_2.0.1-7.2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libmagic1.
Unpacking libmagic1 (from .../libmagic1_5.09-2ubuntu0.3_amd64.deb) ...
Selecting previously unselected package file.
Unpacking file (from .../file_5.09-2ubuntu0.3_amd64.deb) ...
Selecting previously unselected package mime-support.
Unpacking mime-support (from .../mime-support_3.51-1ubuntu1_all.deb) ...
Selecting previously unselected package netbase.
Unpacking netbase (from .../netbase_4.47ubuntu1_all.deb) ...
Selecting previously unselected package libsasl2-modules.
Unpacking libsasl2-modules (from .../libsasl2-modules_2.1.25.dfsg1-3ubuntu0.1_amd64.deb) ...
Selecting previously unselected package openssl.
Unpacking openssl (from .../openssl_1.0.1-4ubuntu5.16_amd64.deb) ...
Selecting previously unselected package libapr1.
Unpacking libapr1 (from .../libapr1_1.4.6-1_amd64.deb) ...
Selecting previously unselected package libaprutil1.
Unpacking libaprutil1 (from .../libaprutil1_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3.
Unpacking libaprutil1-dbd-sqlite3 (from .../libaprutil1-dbd-sqlite3_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package libaprutil1-ldap.
Unpacking libaprutil1-ldap (from .../libaprutil1-ldap_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package apache2.2-bin.
Unpacking apache2.2-bin (from .../apache2.2-bin_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2-utils.
Unpacking apache2-utils (from .../apache2-utils_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package libswitch-perl.
Unpacking libswitch-perl (from .../libswitch-perl_2.16-2_all.deb) ...
Selecting previously unselected package libclass-isa-perl.
Unpacking libclass-isa-perl (from .../libclass-isa-perl_0.36-3_all.deb) ...
Selecting previously unselected package perl-modules.
Unpacking perl-modules (from .../perl-modules_5.14.2-6ubuntu2.4_all.deb) ...
Selecting previously unselected package perl.
Unpacking perl (from .../perl_5.14.2-6ubuntu2.4_amd64.deb) ...
Selecting previously unselected package apache2.2-common.
Unpacking apache2.2-common (from .../apache2.2-common_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2-mpm-worker.
Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2.
Unpacking apache2 (from .../apache2_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package ssl-cert.
Unpacking ssl-cert (from .../ssl-cert_1.0.28ubuntu0.1_all.deb) ...
Setting up libsqlite3-0 (3.7.9-2ubuntu1.1) ...
Setting up libroken18-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libasn1-8-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libgpg-error0 (1.10-2ubuntu1) ...
Setting up libgcrypt11 (1.5.0-3ubuntu0.2) ...
Setting up libgdbm3 (1.8.3-10) ...
Setting up libp11-kit0 (0.12-2ubuntu1) ...
Setting up libtasn1-3 (2.10-1ubuntu1.1) ...
Setting up libgnutls26 (2.12.14-5ubuntu3.8) ...
Setting up libhcrypto4-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libheimbase1-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libwind0-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libhx509-5-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libkrb5-26-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libheimntlm0-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libgssapi3-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libsasl2-2 (2.1.25.dfsg1-3ubuntu0.1) ...
Setting up libldap-2.4-2 (2.4.28-1.1ubuntu4.4) ...
Setting up libcap2 (1:2.22-1ubuntu3) ...
Setting up libexpat1 (2.0.1-7.2ubuntu1.1) ...
Setting up libmagic1 (5.09-2ubuntu0.3) ...
Setting up file (5.09-2ubuntu0.3) ...
Setting up mime-support (3.51-1ubuntu1) ...
update-alternatives: using /usr/bin/see to provide /usr/bin/view (view) in auto mode.
Setting up netbase (4.47ubuntu1) ...
Setting up libsasl2-modules (2.1.25.dfsg1-3ubuntu0.1) ...
Setting up openssl (1.0.1-4ubuntu5.16) ...
Setting up libapr1 (1.4.6-1) ...
Setting up libaprutil1 (1.3.12+dfsg-3) ...
Setting up libaprutil1-dbd-sqlite3 (1.3.12+dfsg-3) ...
Setting up libaprutil1-ldap (1.3.12+dfsg-3) ...
Setting up apache2.2-bin (2.2.22-1ubuntu1.6) ...
Setting up apache2-utils (2.2.22-1ubuntu1.6) ...
Setting up libclass-isa-perl (0.36-3) ...
Setting up ssl-cert (1.0.28ubuntu0.1) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Setting up libswitch-perl (2.16-2) ...
Setting up perl-modules (5.14.2-6ubuntu2.4) ...
Setting up perl (5.14.2-6ubuntu2.4) ...
update-alternatives: using /usr/bin/prename to provide /usr/bin/rename (rename) in auto mode.
Setting up apache2.2-common (2.2.22-1ubuntu1.6) ...
Enabling site default.
Enabling module alias.
Enabling module autoindex.
Enabling module dir.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module status.
Enabling module auth_basic.
Enabling module deflate.
Enabling module authz_default.
Enabling module authz_user.
Enabling module authz_groupfile.
Enabling module authn_file.
Enabling module authz_host.
Enabling module reqtimeout.
Setting up apache2-mpm-worker (2.2.22-1ubuntu1.6) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up apache2 (2.2.22-1ubuntu1.6) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
 ---> 634855a43331
Removing intermediate container 5765c09b530f
Successfully built 634855a43331
[email protected]:~/kishore$

Now let's take a look at our new image. We can do this using the docker images command.

docker images srijan/custom1

[email protected]:~/kishore$ docker images srijan/custom1
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
srijan/custom1      latest              634855a43331        About a minute ago   277.4 MB
[email protected]:~/kishore$

Here you will find that the images will have by default apache2 installed. Lets crosscheck it as :

docker run -t -i srijan/apache2:webserver /bin/bash

Now you will get in the new container with the apache2 with 12.04ubuntu pre-installed.

5 Hostname change

Suppose you want to change the hostname or you want a customized hostname e.g. in my case

I will use

docker run -h '' -t -i srijan/custom1 /bin/bash

It will yield the image container as

[email protected]:~/kishore$ docker run -h '' -t -i srijan/custom1 /bin/bash
[email protected]:/# hostname -f
[email protected]:/#

6 Useful commands for docker

  • pull (pull is used to pull an image or a repository from the registry) 

docker run -i -t ubuntu /bin/bash

  • commit (commit is used to save the container)

docker commit 73527b8b4261 srijan/apache2 8ce0ea7a1528

  • cp (Copy files/folders from a container's filesystem to the host path. Paths are relative to the root of the filesystem.)


where CONTAINER is container & copy files/folders from the PATH to the HOSTPATH

  • start and stop a Container:

docker start 4386fb97867d

docker stop 4386fb97867d

where 4386fb97867d is your container number
  • export ( Export the contents of a filesystem as a tar archive to STDOUT)

docker export 4386fb97867d > latest.tar

  • import (Create an empty filesystem image and import the contents of the tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then optionally tag it.)

docker import

Import from a local file:

Import to docker via pipe and stdin.

cat exampleimage.tgz | sudo docker import - exampleimagelocal:new

Import from a local directory:

sudo tar -c . | sudo docker import - exampleimagedir


  • history (Show the history of an image)

docker history [OPTIONS] IMAGE

sudo docker history  ea7d6801c538

  • images (It will show the images)

docker images [OPTIONS] [NAME]

It follows some options as

-a, --all=false      Show all images (by default filter out the intermediate image layers)
  -f, --filter=[]:     Provide filter values (i.e. 'dangling=true')
  --no-trunc=false     Don't truncate output
  -q, --quiet=false    Only show numeric IDs

  • info (Display system-wide information)

[email protected]:~$ sudo docker info

[email protected]:~$ sudo docker info
Containers: 20
Images: 65
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 105
Execution Driver: native-0.2
Kernel Version: 3.13.0-30-generic
WARNING: No swap limit support
  • inspect (Return low-level information on a container/image)


  • kill (Kill a running container/send SIGKILL, or specified signal)


docker login localhost:8080

It will login to a self-hosted registry.

  • logs (Fetch the logs of a container)

docker logs CONTAINER

  • ps (List containers)

docker ps [OPTIONS]

 It have options as:

-a, --all=false       Show all containers. Only running containers are shown by default.
--before=""           Show only container created before Id or Name, include non-running ones.
-l, --latest=false    Show only the latest created container, include non-running ones.
-n=-1                 Show n last created containers, include non-running ones.
--no-trunc=false      Don't truncate output
-q, --quiet=false     Only display numeric IDs
-s, --size=false      Display sizes
--since=""            Show only containers created since Id or Name, include non-running ones.

  • push (Push an image or a repository to the registry)

docker push NAME[:TAG]

  • restart (It will restart a running container)


  • rm (It will remove one or more containers)


  • rmi (It will remove one or more images)

docker rmi IMAGE [IMAGE...]

  • run (Run a command in a new container)

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

It have following options:

-a, --attach=[]            Attach to stdin, stdout or stderr.
-c, --cpu-shares=0         CPU shares (relative weight)
--cidfile=""               Write the container ID to the file
-d, --detach=false         Detached mode: Run container in the background, print new container id
--dns=[]                   Set custom dns servers
--dns-search=[]            Set custom dns search domains
-e, --env=[]               Set environment variables
--entrypoint=""            Overwrite the default entrypoint of the image
--env-file=[]              Read in a line delimited file of ENV variables
--expose=[]                Expose a port from the container without publishing it to your host
-h, --hostname=""          Container host name
-i, --interactive=false    Keep stdin open even if not attached
--link=[]                  Add link to another container (name:alias)
--lxc-conf=[]              (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
-m, --memory=""            Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
--name=""                  Assign a name to the container
--net="bridge"             Set the Network mode for the container
                               'bridge': creates a new network stack for the container on the docker bridge
                               'none': no networking for this container
                               'container:<name|id>': reuses another container network stack
                               'host': use the host network stack inside the container
  -p, --publish=[]           Publish a container's port to the host
                               format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
                               (use 'docker port' to see the actual mapping)
  -P, --publish-all=false    Publish all exposed ports to the host interfaces
  --privileged=false         Give extended privileges to this container
  --rm=false                 Automatically remove the container when it exits (incompatible with -d)
  --sig-proxy=true           Proxify all received signal to the process (even in non-tty mode)
  -t, --tty=false            Allocate a pseudo-tty
  -u, --user=""              Username or UID
  -v, --volume=[]            Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)
  --volumes-from=[]          Mount volumes from the specified container(s)
  -w, --workdir=""           Working directory inside the container
  • save (Save an image to a tar archive (streamed to stdout by default)

docker save IMAGE

  • search (Search the docker index for images)

docker search TERM

  • tag (Tag an image into a repository)


  • top (Lookup the running processes of a container)

docker top CONTAINER [ps OPTIONS]

  • version (Show the docker version information.)

sudo docker version

[email protected]:~$ sudo docker version
[sudo] password for srijan: 
Client version: 1.0.1
Client API version: 1.12
Go version (client): go1.2.1
Git commit (client): 990021a
Server version: 1.0.1
Server API version: 1.12
Go version (server): go1.2.1
Git commit (server): 990021a

6 Summary

I have tried to get you more familiar with the docker, I hope this guide will help you in understanding docker & utilize docker in your test/production environments.

For more info refer

Share this page:

0 Comment(s)

Add comment