An introduction to Docker with Java.
935

Docker is an open platform for Developing, Packaging, Shipping and Running applications.

It is the primary user interface to Docker. It accepts commands from the user and communicates back and forth with a Docker daemon.
It runs on a host machine. The user does not directly interact with the daemon, but instead through the Docker client with the RESTful api or sockets.
A Client with a Daemon as also as the docker-compose tool. Usually referred simply as "docker".

Creates servers, installs Docker on them and configures the Docker client to talk to them.

A tool for defining and running complex applications with Docker (eg a multi-container application) with a single file.

Swarm pools together several Docker hosts and exposes them as a single virtual Docker host (clustering). It scales up to multiple hosts.

A (hosted) service containing repositories of images which responds to the Registry API.


docker run -i -t ubuntu:15.04 /bin/bash
/bin/bash)Screencast: Steps of a Docker workflow.


A Dockerfile is a text document that contains all the commands a user could call on the command line to create an image.
docker pull ubuntu
docker run -it --name ubuntu_example ubuntu /bin/bash
Screencast: SSH into a container
Let's build a jenkins image
git clone [email protected]:komljen/dockerfile-examples.git ~/Docker-presentation
cd ~/Docker-presentation/jenkins
docker build -t jenkins-local .
// Test it
docker run -d -p 8099:8080 --name jenkins_example jenkins-local
// Open http://localhost:8099
curl localhost:8099
Screencast: Docker build an image
Let's use Apache server
cd ~/Docker-presentation
mkdir apache-example
cd apache-example
docker pull eboraas/apache
docker run --name apache_volume_example \
-p 8180:80 -p 443:443 \
-v $(pwd):/var/www/ \
-d eboraas/apache
// Locally create an index.html file
mkdir html
cd html
echo "It works using docker volume." >> index.html
// Open http://localhost:8180 to view the html file
curl localhost:8180
Screencast: Docker simple volume example
// General info
man docker // man docker run
docker help // docker help run
docker info
docker version
docker network ls
// Images
docker images // docker [IMAGE_NAME]
docker pull [IMAGE] // docker push [IMAGE]
// Containers
docker run
docker ps // docker ps -a, docker ps -l
docker stop/start/restart [CONTAINER]
docker stats [CONTAINER]
docker top [CONTAINER]
docker inspect -f "{{ .State.StartedAt }}" [CONTAINER]
docker rm [CONTAINER]
Screencast: Common Docker commands
Let's create a JBoss Ticket Monster app
docker pull rafabene/wildfly-ticketmonster
docker pull postgres:9.4
docker pull donnex/pgweb
// Start a container for postgres
docker run -d --name postgres_db \
-e POSTGRES_USER=ticketmonster \
-e POSTGRES_PASSWORD=ticketmonster-docker \
postgres:9.4
// Create a container for Ticket Monster app
// Usage: --link [name or id]:alias
docker run -d --name ticketmonster \
-p 8088:8080 \
--link postgres_db:db \
rafabene/wildfly-ticketmonster-h2
// Open http://localhost:8088/ticket-monster/ to see the App UI
// There is a proper linking
docker inspect -f "{{ .HostConfig.Links }}" ticketmonster
// Link a pgweb ui
docker run -d -p 8022:8080 \
--link postgres_db \
donnex/pgweb
// Open http://localhost:8022 and connect to the Pgweb UI
// Tip to find the Postgres credentials.
// Type: docker inspect --format='{{json .Config}}' postgres_db
Let's do the same but with docker-compose.yml
git clone [email protected]:theodorosploumis/docker-java.git ~/docker-java
cd ~/docker-java/examples/docker-compose
// Run docker-compose using the docker-compose.yml
docker-compose up -d
// Same as previous
// Eg open http://localhost:8088/ticket-monster/ etc
Following from previous...
// Create up to 10 Ticket Monster apps
docker-compose scale mywildfly=10
// Get their IP addresses
docker ps
// Open several apps (eg http://localhost:32781 etc)
// All apps share the same database
See examples at hub.docker.com/u/jess
// Grant access to everyone on the X Server (locally)
// Otherwise the containers below will never start
xhost +
// Libreoffice
docker run -it \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=unix$DISPLAY \
--name cathode \
jess/cathode
// SublimeText 3
docker run -it \
-e NEWUSER=$USER \
-v $HOME/.config/sublime-text-3/:/root/.config/sublime-text-3 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
--name sublime_text \
jess/sublime-text-3
// Audacity (sound in docker container)
docker run --rm \
-u $(id -u):$(id -g) \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
-v /dev/snd:/dev/snd \
-v "$HOME:$HOME" \
-w "$HOME" \
-e DISPLAY="unix$DISPLAY" \
-e HOME \
$(find /dev/snd/ -type c | sed 's/^/--device /') \
knickers/audacity
// Disable access to x11
xhost -
There are known best practices (see a list at examples/tips)
| Type | Software |
|---|---|
| Cluster & orchestrate | Swarm, Kubernetes, Marathon, MaestroNG, decking, shipyard |
| Registry | Portus, Docker Distribution, Docker Hub, Quay, Google Container Reg., Artifactory, ProjectAtomic, Treescale, Canister |
| PaaS | Rancher, Tsuru, dokku, flynn, Octohost, DEIS |
Next: Docker in production, Scaling, Private registries, PaaS.
Content type
Image
Digest
Size
9.7 MB
Last updated
almost 10 years ago
docker pull tplcom/docker-java