Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

This tutorial contains some notes about using Docker as container for running virtual environments. The examples were done on Ubuntu, hence they are prefixed with the sudo command.

1. Introduction to docker

Docker is a light weight container, allowing to run pre-configured system images on another operating system. Compared to other virtualization technologies, Docker allows to run many containers per VM and uses less memory/CPU resources. Docker does this, as does not require an hypervisor for each container.

Docker allows to package the full stack in a container:

  • OS,

  • JVM,

  • App server

  • Application with its configuration

This makes the virtual machine portable across environments. Developers can use the same setup as you can use in your production environment.

2. Use cases

Docker allows to solve the following use cases

  • You can to use a specific version of a operating system for testing

  • You want to run your software tests in an controlled environment

  • You want to configure a system and share the configuration

3. Docker components

Docker consists of the following components:

  • Images

  • Containers

  • Daemon

  • Clients

  • Registries

3.1. Images

Images are read-only templates which provide functionality for running an instance of this image (container). An example for a image is the latest release of Ubuntu. Images are defined as layers, for example, you can add Java to the Ubuntu image and get another image based on this.

The Docker hub provides lot of pre-configured images. You can modify existing images and save these modifications as new image.

4. Containers

Container are the started components based on images. They contain the actual application and dependencies but share the same kernel. They can be started, stopped, paused, deleted. Containers are immutable and disposable.

4.1. Docker Daemon

Is used to manage the container. It runs natively on Linux and inside a VM on Windows and Mac OS X. To start it use the docker command.

5. Docker Clients

Clients (CLI, IDE) run on host VM. They provide the tools to interact with container, i.e., to start them.

5.1. Registries

Images are saved in a registry and have an ID with consists of a repository and a tag. For example, fedora:22, is an image which contains the Fedora 22 OS from the fedora repository.

To use an image you have to pull it from a registry, to share an image with others you have to push it to one. The default Docker registry is the Docker Hub. You can upload your personal images to Github, in this case you add your user name as prefix to the image, e.g., vogella/fedore:22

5.2. Dockerfile

A dockerfile allows to modify existing images. It contains the image that should be used and the adjustments to this image, e.g., which packages should be installed, system configuration etc.

6. Docker installation and setup

Install Docker Toolbox from https://www.docker.com/. The installation is well described on the getting started page. For example https://docs.docker.com/linux/step_one/ describes the installation for Linux

Afterwards you can test you installation as described on the webpage:

sudo docker run hello-world

If you issue the command the first time, it downloads the hello-world image and start it.

To allow your user to run docker commands without the sudo prefix, configure a new group and add you user to it.

sudo usermod -aG docker ubuntu

Afterwards you need to logout and login again. Validate that you can run docker commands without sudo.

docker run hello-world

7. Docker commands

7.2. List all available containers

Via the -a flag you list all contains, without you list the running containers.

docker ps -a

7.3. Create a new container

docker run image

7.4. Delete a container and an image

Use the following command to remove all your containers.

docker rm $(docker ps -a -q)

Use the following command to remove all your images.

docker rmi $(docker images -q)

8. Exercise: Creating an Fedora based Unix system with Git

To run a bash on the latest Fedara system, use the following command.

docker run -t -i fedora /bin/bash

This creates a docker container (if not yet present), start the Fedora system and runs a shell in this container. You can now install Git into this container.

dnf install git

Afterwards you need to create a new container from the running container to persists the changes. Use docker ps -l to list the running container. Create a new container with the docker commit command. This command supports the -m and -a flags. The -m flag allows us to specify a commit message, much like you would with a commit on a version control system. The -a flag allows us to specify an author for our update.

docker commit <yourcontainerid> vogella/fedoragit
# alternatively include the author and use a commit message
# docker commit -m "Adds Git to Fedora" -a "Lars Vogel"  9b14523f9e23  fedorawithgit

Then create and run the a new container based on your new image.

sudo docker run -t -i vogella/fedoragit /bin/bash

9. Exercise: Creating an Ubuntu based Unix system with Java

To run a bash on an Ubuntu 15.04 system, use the following command.

docker run -t -i ubuntu:15.04 /bin/bash

This creates a docker container (if not yet present), start the Ubuntu system and runs a shell in this container.

You can create your custom Docker images. For this you write a docker file describing your image. This file is typically named Dockerfile.

For example the following is a docker file which describes an image based on Ubuntu 15.10 with Java installed.

############################################################
# Dockerfile to run Java
# Based on Ubuntu Image
############################################################

# Set the base image to use to Ubuntu
FROM ubuntu:15.10

# Set the file maintainer (your name - the file's author)
MAINTAINER Lars Vogel

# Update the default application repository sources list
RUN apt-get update

# Install Java 8
RUN apt-get install -y openjdk-8-jdk

To create an Docker image from this file, enter the docker build -t [imagename] command. If you used for example ubuntujava8 as image name, you can now start the corresponding container with the docker run -t -i ubuntujava8 /bin/bash command and type in java to check if the installation was successfully.

10. Docker support in the Eclipse IDE

The Eclipse Linux tooling project also provides Docker tooling for the Eclipse IDE, which allows to use the Eclipse IDE as Docker client. The tooling provides also an editor for Docker files.

10.2. Using the Docker tooling in Eclipse

To access the Docker functionality in Eclipse open the Docker Tooling perspective via the Perspective ▸ Open Perspective ▸ Other…​ ▸ Docker Tooling menu entry.

11. About this website

Appendix A: Docker resources

A.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.