Docker for Java Developers
Basic Docker commands
docker images ls
to list docker imagesdocker 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, etcdocker version
client and server version for docker apidocker --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 terminaldocker container -d run jboss/wildfly
run container in background (detached mode)docker container stop containername/containerid
to stop containerdocker container rm containername/id
to remove the containerdocker container run -d --name web jboss/wildfly
to run a container with desired namedocker container rm -f web
stop and remove the container. forceddocker 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 containerdocker image ls -aq
list all image ids.docker image rm -f $(docker image ls -aq)
to remove all images. similarly all containers can be removeddocker 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 hubdocker 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 portdocker container logs web
shows all the logs of container named webdocker container run -d --name web -p 8085:8080 jboss/wildfly
maps localhost:8085 to wildfly 8080 portdocker 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 webapplicationdocker 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:
|
|
docker image build -t hello-java .
to build hello-java container using pwd
Simple Dockerfile for java using jdk-alpine image (lower container size):
|
|
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:
COPY
to copy files/directoriesAdd
to allow tar auto-extraction along with copy. eg ADD sample.tar.gz /tmp.- 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:
- https://github.com/fabric8io/docker-maven-plugin See docs here: https://dmp.fabric8.io/
- https://github.com/spotify/dockerfile-maven
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
|
|
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
|
|