Docker for Java Developers

Basic Docker commands

  • docker images ls to list docker images
  • docker container ls to list containers

Containers and Images

Docker CLI

  • docker info shows basic docker infor about containers/images/volumes, runtimes, kernal version, OS details, etc
  • docker version client and server version for docker api
  • docker --help list of commands available
  • prefer to use management commands like:
    • docker image ls
    • docker container ls

Run a container

  • docker container run jboss/wildfly download wildfly and run it. this will be run in the same terminal
  • docker container -d run jboss/wildfly run container in background (detached mode)
  • docker container stop containername/containerid to stop container
  • docker container rm containername/id to remove the container
  • docker container run -d --name web jboss/wildfly to run a container with desired name
  • docker container rm -f web stop and remove the container. forced
  • docker container run -it --name web jboss/wildfly bash to start a container named web in interactive mode an log in to bash. exit to exit container
  • docker image ls -aq list all image ids. docker image rm -f $(docker image ls -aq) to remove all images. similarly all containers can be removed
  • docker image tag helloworld:1 helloworld:latest to tag version 1 as latest. we need to explicitly tag an image as latest if we want it to be latest.
  • docker image push helloworld:latest to push image to docker hub -> it wont work. docker image tag helloworld:2 ravikrsingh/helloworld:latest then enter credentials to push image to docker hub
  • docker run -d -p 5000:5000 --restart always --name registry registry:2.6.0. docker image tag helloworld:latest localhost:5000/ravikrsingh20/helloworld:latest. docker image push localhost:5000/ravikrsingh20/helloworld

Export ports and attach volumes

  • docker container run -d --name web -P jboss/wildfly publish the port. Maps localhost:32768 to wildfly’s 8080 port
  • docker container logs web shows all the logs of container named web
  • docker container run -d --name web -p 8085:8080 jboss/wildfly maps localhost:8085 to wildfly 8080 port
  • docker container run -d --name web -p 8085:8080 -v /path/to/source/war/file/webapp.war:/opt/jboss/wildfly/standalone/deployments/webapp.war jboss/wildfly take a war file from local machine and mount it as a point in container. Browse to localhost:8085/webapp to see your webapplication
  • docker container logs containername/id -f to tail logs from docker container

Creating a docker image

  • Reference docs for creating docker file
  • docker build image -t helloworld . build a docker image tagged as helloworld using current directory. docker image build can also be used for docker build.
  • docker history helloworld shows how image is built. docker container run helloworld to run the container.

Java docker image

Simple Dockerfile for java:

1
2
FROM openjdk
CMD java -version

docker image build -t hello-java . to build hello-java container using pwd

Simple Dockerfile for java using jdk-alpine image (lower container size):

1
2
FROM openjdk:jdk-alpine
CMD java -version

docker image build -t hello-java:2 to build container hello-java tagged as version 2. docker container run hello-java:2 to run this new container

Copy files in Docker image

To copy files and directories to container:

  1. COPY to copy files/directories
  2. Add to allow tar auto-extraction along with copy. eg ADD sample.tar.gz /tmp.
  3. Use curl/wget to download files from remote URL

some other useful commands

RUN is used to install software package, execute only once CMD is used for executing container. only last CMD command is executed -> Overriden from CLI. ENTRYPOINT configures the container executable; can be overriden using –entrypoint from CLI. default value is /bin/sh -c EXPOSE exposes the network poirts on which the container is listening VOLUME creates a mount point with the specified name. docker container run … -v ~data:/opt/var… USER HEALTHCHECK: performs a healthcheck on the application inside the container. HEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost:8091/pools || exit 1

Docker and maven

There are maven plugins that can be used to build containers as part of maven lifecycle. eg. mvn package can build container images. mvn install can run container images

Few of the options:

Docker Compose

Introduction

Its used for multiple components. multicontainer applications. docker-compose.yml is default name. docker-compose.override.yml to override configurations. multiple files specified using -f

Sample docker-compose.yml file

1
2
3
4
5
6
7
8
version: '3'
services:
  web:
    image: jboss/wildfly
    volumes:
      - /path/to/local/deployment/dir:/opt/jboss/wildfly/standalone/deployments
    ports: 
      - 8090:8080

docker-compose up -d build docker container and start it in detached mode docker-compose logs -f to tail to logs from docker-compose command docker-compose down to bring the service down. stops the container, removes the container and network mapping.

Multiple Services via docker-compose

Compose file containing two services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
version: '3'
services:
  web:
    image: arungupta/couchbase-javaee:travel
    environment: COUCHBASE_URI=db
    ports:
      - 8090:8080
      - 9990:9990
    depends_on: 
      - db
  db:
    image: arungupta/couchbase:travel
    ports: 
      - 8091:8091
      - 8092:8092
      - 8093:8093
      - 11210:11210