About Raquel Pau

Raquel has graduated from Computer Engineering in the Universitat Politecnica de Catalunya. She also holds a Master degree in Computation (Software Engineering) from the same university. She has an I+D profile focused about model driven development. Additionally, she is the project leader of Walkmod, an open source project to code conventions, which is part of the result of her interest in the model driven development. Currently, Raquel works as a Software Architect in the Sparsity-Technologies.

Docker Tutorial for Beginners

In this article we are going to explain the main features of Docker. We are going to briefly explain why containers are necessary, which are the main commands to use and how to use Docker in our integration tests. The following table shows an overview of the whole article:

1. Why do we need Docker?

Docker is a tool to avoid the usual headaches of conflicts, dependencies and inconsistent environments, which is an important problem for distributed applications, where we need to install or upgrade several nodes with the same configuration.

Docker is a container manager, which means that is able to create and execute containers that represent specific runtime environments for your software. In contrast with virtual machines like VirtualBox, Docker uses resource isolation features of the Linux kernel to allow independent “containers” to run within a single Linux instance, avoiding the overhead of starting and maintaining virtual machines. A computer with docker can run multiple containers at the same time.

2. Machines, Images and Containers

In order to use docker, it is important to have clear some vocabulary. Docker distinguishes three important concepts: docker machines, docker images and containers.

Docker containers are executions of an specific runtime environment. For example, to simulate a machine with an specific database up. These runtime environments are called Docker images, which are the result of executing the set of commands that appear in an specific script-like file called Dockerfile. Therefore, Docker allow having multiple containers of an specific Docker image. These could be compared with the concepts of program and process. Indeed, containers like processes, can be created, stopped, died, or running.

Docker machines are local (and virtual) machines or remote machines(e.g in a cloud such as Amazon AWS or DigitalOcean) with a docker running. Like physical machines, docker machines has an specific IP address. Each docker machine can manage multiple docker images and containers. From our own personal computer, the docker-machine command, allow us connecting to all our docker machines to manage their containers and images.

2.1. Installing docker and docker-machine

Follow the next steps to install docker and docker machine

Download and install the docker binary file from here.Once, you have followed the installation instructions, you should be able to run the following command.Docker installation test

3. Understanding the commands

There are many docker commands. In fact, if we run simply docker from our terminal, we can see all the available commands.

Docker commands

Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders from a container to a HOSTDIR or to STDOUT
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on a container or image
kill Kill a running container
load Load an image from a tar archive or STDIN
login Register or log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within a container
port List port mappings or a specific mapping for the CONTAINER
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart a running container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image(s) to a tar archive
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop a running container
tag Tag an image into a repository
top Display the running processes of a container
unpause Unpause all processes within a container
version Show the Docker version information
wait Block until a container stops, then print its exit code
Run 'docker COMMAND --help' for more information on a command.

In this section, we will see how to manage docker machines, how to pull a docker image using an specific docker machine and how to create a container for such docker image.

3.1 Creating a machine

First of all, we will list the set of available docker machines with the following command:

List Docker Machines

docker-machine ls

In case of not having any machine, we are going to create a new one as follows:

Docker installation test

docker-machine create --driver virtualbox default

Notice that in order to create a docker machine, we need to specify a driver, which will determine if it is a virtual machine, in this case virtual box, or if it is a connection to docker running in an external machine.

3.2 Creating an image

Docker images are created in an specific docker machine. In order to define the docker machine that we want to use, we need to run the following command:

Select docker machine

eval "$(docker-machine env default)"

In order to check the effects, list the available docker machines again. At this moment, a new machine should appear with a new IP address (192.168.99.100) and contains an asterisk in the ACTIVE column.

Docker images are the binary files of runtime environments. The contents of a docker image are the result of executing the set of instructions that appear in a plain text file called Dockerfile. However, in order to avoid building docker images from Dockerfiles over and over again, there is an online repository for docker images called Docker Hub.

For example, in order to pull a MongoDB image, we only need to execute the docker pull command as follows:

Docker pull command

docker pull mongo

In order to see the installed docker images in our docker machine, use the following command. Notice that a new entry appears with the Mongo image. Each image has a unique identifier of the docker image for our machine called IMAGE ID.

List Docker Images

docker images

If you are interested to understand how to create a Dockerfile, we strongly recommend follow this example.

3.3 Creating a container

Docker containers are executions of docker images. In order to create a container from an image, we use the command docker run. For example:

List Docker Images

docker run mongo

After that, we can check the list of containers, with the following instruction.

List docker containers

docker ps -a

Notice that our container has an identifier too, called CONTAINER ID. We will use it later.

The IP address to connect to this container from our application is the IP of our docker machine (192.168.99.100).

3.4 Cleaning the environment

At this point we have downloaded a docker machine and we have started a docker container. In order to stop and remove the container and the image we need to execute the following instructions:

Stop the container:

Stop Docker Container

docker stop #containerId

Remove the container:

Remove Docker Container

docker rm #containerId

Remove the docker image:

Remove Docker Image

docker rmi #imageId

4.Running integration tests with Maven

There are different approaches to design integration tests with Docker. In this example, we will design a Maven project, which before executing JUnit tests, it automatically starts a MongoDB Docker container.

4.1 Maven configuration

First of all, copy this pom.xml file. We are going to use the docker-maven-plugin to automatically load the defined Docker images and create a container before running the integration tests. In order to support integration tests, we use the maven-failsafe-plugin.

4.2 Creating integration tests

Integration tests are executed after creating a MongoDB container by the maven-docker-plugin. We are going to create a test to validate the following Zoo component, which only has one operation called addAnimal that inserts an animal to MongoDB.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.