Docker
for Java developers
May, 25th 2019
Sergey Morenets, 2019
DEVELOPER 14 YEARS
TRAINER 6 YEARS
WRITER 4 BOOKS
Sergey Morenets, 2019
FOUNDER
SPEAKER
Sergey Morenets, 2019
Sergey Morenets, 2019
Sergey Morenets, 2019
Sergey Morenets, 2019
Sergey Morenets, 2019
Virtual machines
Sergey Morenets, 2019
Virtualization
Sergey Morenets, 2019
Docker
… is not programming language
… is not framework
… is tool
Sergey Morenets, 2019
Docker. Prehistory
FreeBSD created jails in 2000 to isolate environment
(filesystem, memory, network)
In 2001 Linux VServer appeared
Solaris introduced Solaris containers in 2004
Google started Process containers in 2006 that was later
renamed to control groups and merged with Linux kernel
Linux containers (LXC) based on Linux cgroups and
namespaces
Cgroups limit number of the resources for each process
Namespaces isolates resources for each process
Sergey Morenets, 2019
Docker
Allows to use custom images and publish container images
Released in March 2013
Moved from LXC to libconainer (or runC) as execution
environment
Main contributors: Cisco, Google, IBM, Microsoft and Red Hat
Integrates with Kubernetes and Chef
Sergey Morenets, 2019
Sergey Morenets, 2019
Sergey Morenets, 2019
Sergey Morenets, 2019
Docker survey
Sergey Morenets, 2019
Docker requirements
Sergey Morenets, 2019
Sergey Morenets, 2019
Docker Windows vs Linux usage
Historically on Windows Linux containers run inside Hyper-V
virtual machine
Starting since Windows 10 (and Windows 2016 Server) you
have native support of Linux containers and Hyper-V is not
needed
Windows containers cannot run on Linux host (opposite is
true)
Sergey Morenets, 2019
Task #1. Docker installation
1. (Windows 10 Pro, Linux, OS X 10.11 and newer) Choose
your operating system, download and install Docker
CE(community edition):
https://docs.docker.com/engine/installation/
2. (Windows 8 and earlier, OS X 10.10 and earlier) Download
and install Docker ToolBox:
https://www.docker.com/products/docker-toolbox
3. Check Docker version: docker --version
4. Run this command to view all the existing virtual machines:
docker-machine ls.
Sergey Morenets, 2019
Docker
Command-line interface (Docker client)
Background daemon
Set of remote services (REST API)
Sergey Morenets, 2019
Docker Enterprise
Sergey Morenets, 2019
Docker machine overview
docker-machine ls
docker-machine create --driver
docker-machine start/stop [machine]
docker-machine env [machine]
docker-machine ip [machine]
docker-machine ip default
Sergey Morenets, 2019
Docker machine drivers
AWS
Microsoft Azure
Digital Ocean
Google Compute engine
Microsoft Hyper-V
OpenStack
Oracle VirtualBox
VMWare Fusion
Sergey Morenets, 2019
Task #2. Working with virtual machines
1. View all the running virtual machines. What is the IP and URL
of the virtual machine?
2. Run docker info command to view system information about
Docker installation.
3. Run docker-machine version default and docker-machine env
default commands. What is their output?
4. Try to start/stop virtual machine: docker-machine start
default / docker-machine stop default
Sergey Morenets, 2019
Docker architecture
Sergey Morenets, 2019
Docker client
Interacts with Docker engine
Build and manage images
Run and manage containers
Sergey Morenets, 2019
Docker Hub
Sergey Morenets, 2019
Sergey Morenets, 2019
Sergey Morenets, 2019
Task #3. Repositories
1. Create new account at http://cloud.docker.com (if you don’t
have existing one)
2. Sign-in and create new public repository.
3. Open http://hub.docker.com and review your repository
details
4. Try to search on the site for java or mysql repositories. Which
tags do they support?
Sergey Morenets, 2019
Docker client commands(images)
Command Description
docker pull name[:tag] Retrieves an image from registry
docker images Displays list of images in the local repository
docker run name Runs an image and assigns unique ID
docker search Search image by name/stars/status
docker inspect Retrieves low-level information about image
docker push Pushes new version of image into repository
docker save Saves image as TAR archieve
docker rmi Removes one of more images
Sergey Morenets, 2019
Task #4. Images
1. Run hello-world image: docker run hello-world
2. Display list of all running and stop containers: docker ps -a
3. Remove container: docker rm <ID>, where <ID> is first 3-5
numbers of container ID
4. Remove hello-world image: docker rmi hello-world
5. Download hello-world image again: docker pull hello-world
and verify its existence in the list using command: docker
images
Sergey Morenets, 2019
Docker client commands(containers)
Command Description
docker ps Displays all the running containers
docker restart Restart one or more containers
docker rm Removes one or more containers
docker export/import Exports/imports container file system
docker rename Renames container
docker pause/unpause Pauses/unpauses container
docker exec Launch executables inside running container
docker commit Creates new image based on running
container
docker cp Copies files/folder between host machine and
container
Sergey Morenets, 2019
Task #5. Containers
1. Run Tomcat 9 container: docker run tomcat:9 and then try to
open http://localhost:8080 URL on the host machine. What is
the result?
2. Run command docker ps to view all the running containers.
What is the name of your container?
3. Stop container: docker stop <container_ID> and then run
container in the daemon mode: docker run -d tomcat:9.
What is the difference between daemon
4. and non-daemon mode?
Sergey Morenets, 2019
Task #6. Creating images
1. Run new Ubuntu container(or use existing one), go to the
container bash and install mc
2. Run mc command to verify that mc has been installed
3. Run command docker commit <ID> mc-local to create new
image with name mc-local
4. Run Ubuntu container using command: docker run -it ubuntu
echo ‘Hello!’ How does it work? Does it start container?
5. Run new Ubuntu container: docker run -it ubuntu /bin/sh.
Create folder test in the bash console.
Sergey Morenets, 2019
Resource constraints
Parameter Description
--memory Maximum amount of memory you can use(for
example, 512M)
--memory-swap Maximum amount of memory + swap
--cpus Number of CPU cores (0.0, 1, 1.5)
--cpuset-cpus Which cores to use (0-3), (1,2)
--blkio-weight Weight for direct I/O reading (500 is default)
--device-read-bps Limits read rate from device (/dev/sda:2mb)
Sergey Morenets, 2019
Task #7. Resource constraints
1. Run Tomcat 9 container: docker run -d --name=tomcat9
tomcat:9
2. Open bash console in the container: docker exec -it tomcat9
/bin/sh
3. Install ps utility: apt-get update && apt-get install -y procps
4. Run ps aux command in the console and review resource
utilization(CPU, Memory)
Sergey Morenets, 2019
Image layers
Image is read-only template
Image ID is SHA256 generated hash based on content
Sergey Morenets, 2019
Image and containers
Container has one writable layer
Sergey Morenets, 2019
Docker storage drivers
aufs (ext4, xfs) - Oldest
devicemapper (direct-lvm)
zfs (zfs)
overlay/overlay2 (ext4, xfs) – Similar to AUFS, but simple and
better performance
btrfs (btrfs)
Sergey Morenets, 2019
Building image
Dockerfile is Docker script to specify steps to build new
images
Each line can be a comment(#) or instruction
First instruction is always ‘FROM’
At least one CMD/ENTRYPOINT instruction should be
specified
Sergey Morenets, 2019
Instructions
Name Description
EXPOSE Expose port(s) that container listens at run-time
FROM Specify base image
HEALTHCHECK Specify steps to verify that main service in the
container is still running & responding
LABEL Add metadata for image in the key-value format
RUN Specify executable command to run inside image file
system when building image
SHELL Defines executable shell for the build
USER Specify user to run CMD/ENTRYPOINT/RUN instructions
Sergey Morenets, 2019
Instructions
Use the latest tag Use tag 17.10
Expose port 8000 in container for
other containers
Added two labels
Create /data folder in the image
Sergey Morenets, 2019
Building images
docker build . Path to the folder with
Dockerfile
docker build resources/docker
Sergey Morenets, 2019
Task #8. Building images
1. Create new empty folder images.
2. Create empty file Dockerfile there.
3. Create docs folder in the images folder and put there file
instructions.txt with description of your image.
4. Add the following lines to the Dockerfile:
5. FROM ubuntu
6. LABEL Author=“Author” Company=”Slice”
Sergey Morenets, 2019
Instructions
Name Description
ARG Specify arguments that can be passed when building
image
ADD Copies files /directories from host computer(remote
URL, archieves) to the image filesystem
CMD Specify executable or arguments for container
COPY Copies files /directories from host computer to the
image filesystem
ENTRYPOINT Specify executable to run inside container
ENV Set environment variable(s) in the key-value format
VOLUME Specify mount point with specified name
WORKDIR Specify working directory for
CMD/ENTRYPOINT/RUN/ADD/COPY instructions
Sergey Morenets, 2019
Instructions
Declare argument Provide default value
Copy myfile.txt from current
folder to /opt/data in the image
Added two environment
variables
Starts bash console when
container starts
Sergey Morenets, 2019
Dockerignore
Before Docker starts generating build context it search for
.dockerignore in the root folder
The file format is similar to .gitignore
Sensitive production configs and .git folder should be added
Sample line Description
# comment Ignored
*/temp* Exclude files and directories whose names start with
temp in any immediate subdirectory of the root
temp? Exclude files and directories in the root directory
whose names are a one-character extension of temp.
* Exclude everything
!README.md Exclude everything except README.md
Sergey Morenets, 2019
Task #9. Files/executables
1. Add new line to Dockerfile:
2. RUN echo ‘Building image …’
3. Build image. Does ‘Building image …’ message appear during
build? Does it appear if you start build again? Run docker
images command. Did new images appear in the list?
4. Add new lines to Dockerfile:
5. COPY docs/ /docs/
6. WORKDIR /docs
7. RUN cat instructions.txt
Sergey Morenets, 2019
Image build caching
Sergey Morenets, 2019
Base image selection
docker build -t app/local . Tomcat 8.5
docker build -t app/local --build-arg TOMCAT_VERSION=9 .
Sergey Morenets, 2019
Task #10. Customization
1. Add new line after ‘FROM ubuntu’:
2. Use new environment variable in the script: RUN echo
$message
3. Build image again and confirm that message is displayed.
4. Replace ENV line with two lines:
5. Build image and confirm that message is displayed
Sergey Morenets, 2019
Dangling images
Each instruction in Dockerfile creates intermediate layer
(image with no name and tag)
Dangling image is intermediate image that is not used any
more
Image v1 Image v1
Intermediate
layers ADD ADD
Dangling
layer Base image docker image prune
Sergey Morenets, 2019
Task #11. Dangling images
1. Run command docker images -a. What are the images with
no name and tag?
2. Create new Dockerfile
3. Build this image
4. Change second line in Dockerfile to RUN echo hello2
5. Build this image again. Now run command docker images.
Are there new images? Run command docker images -f
"dangling=true"
Sergey Morenets, 2019
Docker CLI
docker images – List all the images
docker ps – List all the containers
Sergey Morenets, 2019
Docker container CLI (new syntax)
Old command New command
docker ps docker container ls
docker restart docker container restart
docker rm docker container rm
docker export/import docker container export
docker rename docker container rename
docker pause/unpause docker container pause/unpause
docker exec docker container exec
docker commit docker container commit
docker run docker container create
docker cp docker container cp
docker container prune
Sergey Morenets, 2019
Docker image CLI (new syntax)
Old command New command
docker images docker image ls
docker build docker image build
docker inspect docker image inspect
docker pull docker image pull
docker rmi docker image rm
docker tag docker image tag
docker save docker image save
docker push docker image push
docker image prune
Sergey Morenets, 2019
Task #12. Docker CLI new syntax
1. Run command docker image ls to view all images.
2. Run command docker container ls to view all the containers.
3. Use command docker container exec to execute command
inside the container.
4. Use command docker image build to build an image
5. Use command docker container create to run container
6. Use command docker container prune to remove all stopped
containers
Sergey Morenets, 2019
Upgrading containers
MySQL 5.6 MySQL 5.6
image container with data
Copy data
MySQL 5.7
MySQL 5.7
container
Data loss
Sergey Morenets, 2019
Container goals
Containers are designed to be stateless (disposable)
When a container is stopped its data are not accessible
Containers are stored on the single host system
Sergey Morenets, 2019
Volumes
Sergey Morenets, 2019
Volume commands
Name Description
docker volume create Create new volume
docker volume ls Displays all the existing volumes
docker volume inspect Returns additional information about volume
docker volume rm Removes a volume(if it’s not used by container)
docker volume prune Removes unused volumes
docker run -it -v shared/data ubuntu
Volume name Folder inside container
docker run -it -v shared/shared_data debian
Sergey Morenets, 2019
Mount to host system
File or directory on host machine is mounted into container
File or directory on host machine is created on demand
Required sharing drives on Windows
docker run -it -v /opt/my_folder:/shared_data ubuntu
Host machine Container
Sergey Morenets, 2019
New mount format
Can be also bind or tmpfs
docker run --mount type=volume,source=myvol2,target=/opt ubuntu
Volume name Folder inside container
Sergey Morenets, 2019
Task #13. Volumes
1. Create new volume using command: docker volume create
shared
2. Verify that volume has created: docker volume ls. Review
volume information: docker volume inspect shared.
3. Run a container using command: docker run -v
shared:/shared <image>.
4. Verify that volume has been mounted: docker inspect –f “{{
.Mounts }}” <container_id>
Sergey Morenets, 2019
Sergey Morenets, sergey.morenets@gmail.com
Sergey Morenets, 2019