Dockerfiles
In this lesson, we'll discuss Dockerfiles.
Overview #
The creation of Docker images is done via files named
Dockerfile. One of Docker’s strengths is that Dockerfiles are easy
to write and therefore, the rolling out of software can be automated
without any problems.
The typical components of a Dockerfile are:
- 
FROMdefines a base image on which the installation is based. A base image for a microservice usually contains a Linux distribution and basic software, such as the JVM, for example. - 
RUNdefines commands that execute to create the Docker image. In essence, aDockerfileis a shell script that installs the software. - 
CMDdefines what happens when the Docker container is started. Typically, only one process should run in one Docker container. This is started byCMD. - 
COPYcopies files in the Docker image.ADDdoes the same; however, it can also unpack archives and download files from a URL on the Internet.COPYis simpler to understand because it does not extract archives, for example. Also, from a security perspective, it can be problematic to download software from the Internet into Docker containers. Therefore,COPYshould be given preference overADD. - 
EXPOSEexposes a port of the Docker container. This can then be contacted by other Docker containers or can be tied to a port of the Docker host. 
A comprehensive
reference is
available on the Internet which contains additional details to the
commands in
Dockerfile.
An example for a Dockerfile #
A simple example of a Dockerfile for a Java microservice looks like
this:
FROM openjdk:11.0.2-jre-slimCOPY target/customer.jar .CMD /usr/bin/java -Xmx400m -Xms400m -jar customer.jarEXPOSE 8080
- 
The first line defines the base image with
FROM. It is downloaded from the public Docker hub. The image contains a Linux distribution and a Java Virtual Machine (JVM). - 
The second line adds a JAR file to the image with ...