Chapter 2. Docker and Your First Application

This chapter explains how to build and run your first Docker container using Java.

You’ll learn the syntax needed to create Docker images using Dockerfiles and run them as a containers. Sharing these images using Docker Hub is explained. Deploying a sample Java EE application using prebuilt Docker images is then covered. This application will consist of an application server and a database container on a single host. The application will be deployed using Docker Compose and Docker Swarm. The same application will also be deployed using Kubernetes.

Dockerfile

Docker builds images by reading instructions from a text document, usually called a Dockerfile. This file contains all the commands a user can usually call on the command line to assemble an image. The docker build command uses this file and executes all the instructions in this file to create an image.

The build command is also passed a context that is used during image creation. This context can be a path on your local filesystem or a URL to a Git repository. The context is processed recursively, which means any subdirectories on the local filesystem path and any submodules of the repository are included.

It’s recommended to start with an empty directory in order to keep the build process simple. Any directories or files that need to be included in the image can be added to the context.

A file named .dockerignore may be included in the root directory of the context. This file ...

Get Docker for Java Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.