BROUGHT TO YOU BY:
C O NT E NT S
Get More Refcardz! Visit dzone.com/refcardz
221
Getting Started
With Docker
About Docker
Docker Architecture
Getting Started
Typical Local Workflow
Other Helpful Commands
By Christopher M. Judd
Dockerfile, and more...
A B O U T D O CK E R
Almost overnight, Docker has become the de facto
standard that developers and system administrators
use for packaging, deploying, and running distributed
applications. It provides tools for simplifying
DevOps by enabling developers to create templates
called images that can be used to create lightweight
virtual machines called containers, which include
their applications and all of their applications
dependencies. These lightweight virtual machines
can be promoted through testing and production
environments where sysadmins deploy and run them.
Docker
Images
Docker makes it easier for organizations to automate
infrastructure, isolate applications, maintain
consistency, and improve resource utilizations.
A recipe or template for creating Docker
containers. It includes the steps for installing and
running the necessary software.
Docker
Like a tiny virtual machine that is created from
Container the instructions found within the Docker image
originated
Similar to the popular version control software Git,
Docker has a social aspect, in that developers and
sysadmins are able to share their images via
Docker
Client
Command-line utility or other tool that takes
advantage of the Docker API (https://docs.docker.
com/reference/api/docker_remote_api) to
communicate with a Docker daemon
Docker
Host
A physical or virtual machine that is running a
Docker daemon and contains cached images as
well as runnable containers created from images
G E T T I N G S TA R T E D W I T H D O C K E R
Docker Hub.
Docker is an open-source solution that runs natively
on Linux but also works on Windows and Mac using a
lightweight Linux distribution and VirtualBox. Many
Site24x7
DOCKER MONITORING
tools have also grown up around Docker to make it
easier to manage and orchestrate complex distributed
applications.
Get Detailed Insight into Docker Containers
D O CK ER A RCHITEC TU R E
Docker utilizes a client-server architecture and a remote
API to manage and create Docker containers built upon
Linux containers. Docker containers are created from
CPU
Docker images. The relationship between containers
and images are analogous to the relationship between
objects and classes in object-oriented programming.
D Z O NE, INC.
Cache
Memory
DZ O NE .C O M
I/O
Site24x7
Cache
Memory
I/O
CPU
DOCKER MONITORING
Analyze resource usage and performance
metrics of containers and hosts
LEARN MORE
Your First YEAR is on Us!
Sign Up for Free
at http://bit.ly/dzone365
All-in-One Monitoring Solution for DevOps and IT
Website
Application
Server
Cloud
Network
Website Performance
Website Availability
Public Status Pages
Mail Server
DNS
Java
.NET
RUM
Ruby on Rails
Mobile APM
Windows
Linux
On-Premise
SQL
Amazon EC2
Amazon RDS
Amazon S3
VMware
Docker
Router
Firewall
Switch
www.Site24x7.com
3
Docker
Registry
Docker
Machine
GETTING STARTED WITH DOCKER
A repository of Docker images that can be
used to create Docker containers. Docker Hub
(https://hub.docker.com) is the most popular
social example of a Docker repository.
build the container from the Docker Hub, then build and
A utility for managing multiple Docker hosts,
which can run locally in VirtualBox or remotely
in a cloud hosting service such as Amazon Web
Services, Microsoft Azure, or Digital Ocean
everything is configured properly, run the following
run it.
To run the simple hello-world container to make sure
commands:
docker run hello-world
Ultimately this command prints a message to standard
G E T TI N G STA RTE D
output explaining all the steps that took place to display
INSTALLING DOCKER
the message.
For Mac and Windows the installation could not be simpler.
T YPI C A L LO C A L WO R K FLOW
All you need to do is download and install the Docker
Toolbox found at https://www.docker.com/toolbox. The
installer includes the Docker Client, Docker Machine,
Docker has a typical workflow that enables you to create
Compose (Mac only), Kitematic, and VirtualBox.
images, pull images, publish images, and run containers.
HOT
TIP
Since Docker is based on the Linux Container
technologies which are not available on Mac and
Windows, VirtualBox is used to run a tiny Linux
kernel containing the Docker server.
At the time of this writing, installing Docker on Linux is
not as easy. To install Docker on Linux you may have to
install some prerequisites; check https://docs.docker.com/
The typical Docker workflow involves building an image
installation for specific instructions. For some distributions
from a Dockerfile, which has instructions on how to configure
there may be packages available using its native package
a container or pull an image from a Docker Registry such as
manager. For other distributions you will need to run:
Docker Hub. With the image in your Docker environment,
curl -sSL https://get.docker.com/ | sh
you are able to run the image, which creates a container
as a runtime environment with the operating systems,
Optionally on Linux you can install Docker-Machine as
software, and configurations described by the image. For
root; to do so, execute the following:
example, your result could be a container on the Debian
operating system running a version of MySQL 5.5, which
curl -L https://github.com/docker/machine/releases/
download/v0.4.0/docker-machine_linux-amd64 > /
usr/local/bin/docker-machine
chmod +x /usr/local/bin/docker-machine
creates a specific database with users and tables required
by your web application. These runnable containers
can be started and stopped like starting and stopping a
virtual machine or computer. If manual configurations or
If you want to create machines locally, you will also need to
software installations are made, a container can then be
install VirtualBox using the instructions found at https://
www.virtualbox.org/wiki/Linux_Downloads.
committed to make a new image that can be later used to
As of the date of this publication, Docker-Machine is still
an image with your team or the world, you can push your
considered in beta and is not recommended for production use.
images to a Docker registry.
RUNNING A CONTAINER
PULL IMAGE FROM DOCKER REGISTRY
With Docker installed you are able to begin running
The easiest way to get an image is to visit https://hub.
containers. If you dont already have the container you
docker.com and find an already prepared image to build
want to run, Docker will download the image necessary to
a container from. There are are many certified official
create containers from it. Finally, when you want to share
D Z O NE, INC .
DZ O NE .C O M
GETTING STARTED WITH DOCKER
accounts for common software such as MySQL, Node.js,
This displays all the locally cached images, including the
Java, Nginx, or WordPress, but there are also hundreds of
ones created using the build command.
thousands of images created by ordinary people as well. If
REPOSITORY
<none>
mysql
you find an image you want, such as mysql, execute the pull
command to download the image.
docker pull mysql
TAG
<none>
5.5.45
IMAGE ID
4b9b8b27fb42
0da0b10c6fd8
VIRTUAL SIZE
214.4 MB
213.5 MB
As you can see, the build command created an image with
a repository name and tag name of <none>. This tends not
If you dont already have the image locally, Docker will
to be very helpful, so you can use a t option to name the
download the most current version of that image from
image for easier usage:
Docker Hub and cache the image locally. If you dont want
the current image and instead want a specific version, you
docker build t est-mysql .
can also add a tag to identify the desired version.
Listing the images again you can see the image is much
docker pull mysql:5.5.45
clearer.
HOT
TIP
REPOSITORY
est-mysql
mysql
If you know you will want to run the image
immediately after pulling, you can save a step by just
using the run command and it will automatically pull
it in the background.
TAG
latest
5.5.45
IMAGE ID
4b9b8b27fb42
0da0b10c6fd8
VIRTUAL SIZE
214.4 MB
213.5 MB
There is an alternative option to creating a custom image
BUILDING IMAGE FROM A DOCKERFILE
besides writing a Dockerfile. You can run an existing image
If you cant find what you need or dont trust the source of
with bash access then customize the image manually by
an image you find on Docker Hub, you can always create
installing software or changing configurations. When
your own images by creating a Dockerfile. Dockerfiles
complete you can run the docker commit command to
contain instructions for inheriting from an existing
create an image of the running container. This is not
image, where you can then add software or customize
considered a best practice since it is not repeatable or self-
configurations.
documenting like using the Dockerfile method.
The following is a simple example of what you might find
RUNNING AN IMAGE
in a file named Dockerfile:
To run a Docker image you just need to use the run
command followed by a local image name or one found
FROM mysql:5.5.45
RUN echo America/New_York | tee /etc/timezone &&
dpkg-reconfigure --frontend noninteractive tzdata
in Docker Hub. Commonly, a Docker image will require
some additional environment variables, which can be
specified with the -e option. For long-running processes
This Dockerfile example shows that the image created will
like daemons, you also need to use a d option. To start the
inherit from the certified mysql repository (specifically the
est-mysql image, you would run the following command to
5.5.45 version of MySQL). It then runs a Linux command to
configure the MySQL root users password, as documented
update the time zone to be Eastern Time.
in the Docker Hub mysql repository documentation:
More details on creating a Dockerfile will be provided later.
docker run -e MYSQL_ROOT_PASSWORD=root+1 -d estmysql
To build this image from the directory containing the
Dockerfile, run the following command:
To see the running container, you can use the Docker ps
docker build .
command:
docker ps
This command will create an unnamed image. You can see
it by running the command to list images.
The ps command lists all the running processes, the image
docker images
name they were created from, the command that was run,
D Z O NE, INC .
DZ O NE .C O M
GETTING STARTED WITH DOCKER
any ports that software are listening on, and the name of
want to run it again in the state it was in when you shut it
the container.
down, you can use the start command:
CONTAINER ID IMAGE
COMMAND
30645f307114 est-mysql /entrypoint.sh mysql
docker start my-est-mysql
PORTS NAMES
3306/tcp serene_brahmagupta
TAGGING AN IMAGE
Now that you have an image that you have run and
As you can see from the running processes above, the
validated, it is a good idea to tag it with a username, image
name of the container is serene_brahmagupta. This is an
name, and version number before pushing it to repository.
auto-generated name and may be challenging to maintain.
You can accomplish this by using the Docker tag command:
So it is considered a best practice to explicitly name the
docker tag est-mysql javajudd/est-mysql:1.0
container using the --name option to provide your name at
container start up:
PUSH IMAGE TO REPOSITORY
docker run --name my-est-mysql -e MYSQL_ROOT_
PASSWORD=root+1 -d est-mysql
Finally, you are ready to push your image to Docker Hub for
the world to use or your team to use via a private repository.
First, if you havent done so already, you will need to go
You will notice from the ps output that the container is
https://hub.docker.com/ to create a free account. Next you
listening to port 3306, but that does not mean you are able
need to login using the login command.
to use the MySQL command line or MySQL Workbench
locally to interact with the database, as that port is
docker login
only accessible in the secure Docker environment in
which it was launched. To make it available outside that
When prompted, input the username, password, and email
environment, you must map ports using the p option.
address you registered with.
Now push your image using the push command, specifying
docker run --name my-est-mysql -e MYSQL_ROOT_
PASSWORD=root+1 -p 3306:3306 -d est-mysql
your username, image name, and version number.
docker push javajudd/est-mysql:1.0
Now mysql is listening on a port that you can connect to.
But you still must know what the IP address is to connect.
After some time you will receive a message that the
To determine the IP address you can use the docker-
repository has been successfully pushed. If you log back
machine ip command to figure it out.
into your Docker Hub account, you will see the new repository.
docker-machine ip default
Using default as the machine name, which is the default
machine installed with the Docker Toolbox, you will receive
the IP address of the machine hosting your docker container.
With the IP address, you can now connect to MySQL using
your local MySQL command line.
mysql -h 192.168.99.100 -u root -proot+1
STOPPING AND STARTING CONTAINERS
Now that you have a Docker container running, you
can stop it by using the Docker stop command and the
container name:
docker stop my-est-mysql
The entire state of the container is written to disk, so if you
D Z O NE, INC .
DZ O NE .C O M
GETTING STARTED WITH DOCKER
EXECUTE COMMANDS
OTH E R H E LPFU L CO M M A N DS
You can execute commands in a running container using
LIST CONTAINERS
the exec command. To list the contents of the root of the
You have already seen how the ps command can list the
hard drive you can, for example, do the following:
running containers, but what about all the containers,
docker exec my-est-mysql ls /
regardless of their state? By adding the a option, you can
see them all.
If you want to ssh as root into the container, there is an
equivalent exec command you can run to gain access to a
docker ps -a
bash shell, and since all the communications between the
With a listing of all containers, you can decide which ones
Docker client and the Docker daemon are already encrypted,
to start or remove.
it is secure.
docker exec -it my-est-mysql bash
REMOVE CONTAINERS
When you are done using a container, rather than having it
RUN CONTAINER
lie around, you will want to remove it to reclaim diskspace.
The run command is the most complicated and featured of
To remove a container, you can use the rm command:
all the Docker commands. It can be used to do things such
docker rm my-est-mysql
as manage networking settings; manage system resources
such as memory, CPU, and filesystems; and configure
REMOVE IMAGES
security. Visit https://docs.docker.com/reference/run/ to see
You have already seen how the images command can
all options available.
list all the locally cached images. These images can take
D O CK E R FI LE
significant amounts of space, ranging from a megabyte
to several hundred megabytes, so you will want to purge
As you have already seen, the Dockerfile is the primary
unwanted images using the rmi command:
way of creating a Docker image. It contains instructions
docker rmi est-mysql
such as Linux commands for installing and configuring
software. The build command can refer to a Dockerfile on
During the debugging cycle of creating a new image, you
your PATH or to a URL, such as a GitHub repository. Along
may generate a large amount of unwanted and unnamed
with the Dockerfile, any files in the same directory or its
images, which are denoted with a name of <none>. You can
subdirectories will also be included as part of the build
easily remove all the dangling images using the following
process. This is helpful if you want the build to include
command:
scripts to execute or other necessary files for deployment.
docker rmi $(docker images -q -f dangling=true)
HOT
TIP
LIST PORTS
Its often helpful to know what ports are exposed by
a container, such as port 3306 for accessing a MySQL
If you wish to exclude any files or directories from
being included, you have the option of using a
.dockerignore file for this purpose.
INSTRUCTIONS
database or port 80 for accessing a web server. The port
Instructions are executed in the order in which they are
command can be used to display the exposed ports.
found in the Dockerfile. The Docker file can also contain
docker port my-est-mysql
line comments starting with the # character.
This table contains the list of commands available.
LIST PROCESSES
If you need to see the processes running in a container, you
INSTRUCTION
DESCRIPTION
can use the top command (similar to running the Linux top
FROM
This must be the first instruction in the Dockerfile
and identifies the image to inherit from
MAINTAINER
Provides visibility and credit to the author of
the image
command):
docker top my-est-mysql
D Z O NE, INC .
DZ O NE .C O M
7
INSTRUCTION
DESCRIPTION
RUN
Executes a Linux command for configuring
and installing
ENTRYPOINT
The final script or application used to
bootstrap the container, making it an
executable application
CMD
Provide default arguments to the ENTRYPOINT
using a JSON array format
LABEL
Name/value metadata about the image
ENV
Sets environment variables
COPY
Copies files into the container
ADD
Alternative to copy
WORKDIR
Sets working directory for RUN, CMD,
ENTRYPOINT, COPY, and/or ADD instructions
EXPOSE
Ports the container will listen on
VOLUME
Creates a mount point
USER
User to run RUN, CMD, and/or ENTRYPOINT
instructions
GETTING STARTED WITH DOCKER
&& rm -rf /usr/local/mysql/mysql-test /usr/
local/mysql/sql-bench \
&& rm -rf /usr/local/mysql/bin/*-debug /usr/
local/mysql/bin/*_embedded \
&& find /usr/local/mysql -type f -name *.a
-delete \
&& apt-get update && apt-get install -y
binutils && rm -rf /var/lib/apt/lists/* \
&& { find /usr/local/mysql -type f -executable
-exec strip --strip-all {} + || true; } \
&& apt-get purge -y --auto-remove binutils
ENV PATH $PATH:/usr/local/mysql/bin:/usr/local/
mysql/scripts
RUN mkdir -p /etc/mysql/conf.d \
&& { \
echo [mysqld]; \
echo skip-host-cache; \
echo skip-name-resolve; \
echo user = mysql; \
echo datadir = /var/lib/mysql; \
echo !includedir /etc/mysql/conf.d/; \
} > /etc/mysql/my.cnf
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT [/entrypoint.sh]
DOCKERFILE EXAMPLE
EXPOSE 3306
CMD [mysqld]
This an example of the official MySQL 5.5 Dockerfile
found at https://github.com/docker-library/mysql/
This example Dockerfile performs the following actions:
blob/5836bc9af9deb67b68c32bebad09a0f7513da36e/5.5/
Dockerfile, which uses many of the available instructions.
Extends from an existing Debian image called
debian:jessie
FROM debian:jessie
RUN groupadd -r mysql && useradd -r -g mysql
mysql
RUN mkdir /docker-entrypoint-initdb.d
RUN apt-get update && apt-get install -y perl
--no-install-recommends && rm -rf /var/lib/apt/
lists/*
RUN apt-get update && apt-get install -y libaio1
&& rm -rf /var/lib/apt/lists/*
RUN gpg --keyserver ha.pool.
sks-keyservers.net --recv-keys
A4A9406876FCBD3C456770C88C718D3B5072E1F5
Uses the RUN instruction to configure the image by adding
some groups, making a directory, and installing required
software using the Debian apt-get package manager
Runs gpg to setup some encryption with PGP
Uses the ENV instruction to define the major and minor
versions of MySQL represented in this image
Runs a long line of commands to install and configure
the system followed by another environment variable
ENV MYSQL_MAJOR 5.5
ENV MYSQL_VERSION 5.5.45
to set up the system PATH
RUN apt-get update && apt-get install -y curl
--no-install-recommends && rm -rf /var/lib/apt/
lists/* \
&& curl -SL http://dev.mysql.com/get/
Downloads/MySQL-$MYSQL_MAJOR/mysql-$MYSQL_
VERSION-linux2.6-x86_64.tar.gz -o mysql.tar.gz \
&& curl -SL http://mysql.he.net/Downloads/
MySQL-$MYSQL_MAJOR/mysql-$MYSQL_VERSIONlinux2.6-x86_64.tar.gz.asc -o mysql.tar.gz.asc \
&& apt-get purge -y --auto-remove curl \
&& gpg --verify mysql.tar.gz.asc \
&& mkdir /usr/local/mysql \
&& tar -xzf mysql.tar.gz -C /usr/local/mysql
--strip-components=1 \
&& rm mysql.tar.gz* \
# continued ->
D Z O NE, INC .
Uses the RUN command to create a configuration file
Uses the VOLUME command to map a file system
Uses the COPY command to copy and rename the script
it will execute when the container starts up, followed by
the ENTRYPOINT which specifies the same script to execute
Uses EXPOSE to declare port 3306 as the standard
MySQL port to be exposed
Uses CMD to specify that the command-line argument
passed to the ENTRYPOINT at container startup time is
the string mysqld
DZ O NE .C O M
GETTING STARTED WITH DOCKER
D O CK E R M ACH I N E
docker-machine ls
Docker Machine is another command-line utility used for
START AND STOP MACHINES
managing one or more local or remote machines. Local
Docker Machines can be started using the docker-machine
machines are often run in separate VirtualBox instances.
start command.
Remote machines may be hosted on cloud providers such as
docker-machine start qa
Amazon Web Services (AWS), Digital Ocean, or Microsoft Azure.
CREATE LOCAL MACHINES
Once the machine is started, you have to configure the
When installing the Docker Toolbox, you will be given a default
Docker command line, which Docker Daemon should be
Docker Machine named default. This is easy to use to get
interacting with. You can do this using the docker-machine
started, but at some point you may need multiple machines
env command and evaluating it with eval.
to segment the different containers you are running. You
docker-machine env qa
eval $(docker-machine env qa)
can use the docker-machine create command to do this:
docker-machine create -d virtualbox qa
To stop a machine, use the docker-machine stop command.
docker-machine stop qa
This creates a new local machine using a VirtualBox image
named qa.
HOT
TIP
LIST MACHINES
If you need to see what machines you have configured you
can run the docker-machine ls command:
The docker-machine start and stop commands
literally start and stop VirtualBox VMs. If you have the
VirtualBox Manager open, you can watch the state of
the VM change as you run the commands.
ABOUT THE AUTHOR
Christopher M. Judd is the CTO and a partner at Manifest
Solutions (http://www.manifestcorp.com), an international
speaker, an open source evangelist, the Central Ohio Java
Users Group (http://www.cojug.org) and Columbus iPhone
Developer User Group leader, and the co-author of Beginning
Groovy and Grails (Apress, 2008), Enterprise Java Development on
a Budget (Apress, 2003), and Pro Eclipse JST (Apress, 2005), as well
as the author of the childrens book Bearable Moments. He has
spent 20 years architecting and developing software for Fortune 500
companies in various industries, including insurance, health care, retail,
government, manufacturing, service, and transportation. His current
focus is on consulting, mentoring, and training with Java, Java EE,
Groovy, Grails, Cloud Computing, and mobile platforms like iPhone,
Android, Java ME, and mobile web.
CREDITS:
Editor: G. Ryan Spain | Designer: Yassee Mohebbi | Production: Chris Smith | Sponsor Relations: Chris Brumfield | Marketing: Chelsea Bosworth
BROWSE OUR COLLECTION OF 250+ FREE RESOURCES, INCLUDING:
RESEARCH GUIDES: Unbiased insight from leading tech experts
REFCARDZ: Library of 200+ reference cards covering the latest tech topics
COMMUNITIES: Share links, author articles, and engage with other tech experts
JOIN NOW
DZone communities deliver over 6 million pages each month to more than 3.3 million software
developers, architects and decision makers. DZone offers something for everyone, including
news, tutorials, cheat sheets, research guides, feature articles, source code and more.
"DZone is a developer's dream," says PC Magazine.
Copyright 2015 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any
DZONE,
DZONE, INC.
INC.
form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
DZONE, INC.
150 PRESTON EXECUTIVE DR.
CARY, NC 27513
888.678.0399
919.678.0300
REFCARDZ FEEDBACK WELCOME
refcardz@dzone.com
SPONSORSHIP OPPORTUNITIES
DZONE.COM
DZONE.COM
sales@dzone.com
VERSION 1.0
$7.95