Writing Dockerfile

Dockerfile is sometimes called the Docker build file. It is a simple text file that has a set of commands that the Docker client calls at the time of image creation. Dockerfile automates the Docker image creation process. The Dockerfile commands are almost identical to their equivalent Linux commands. So, there is no special syntax required for this build file, you can easily create your own Dockerfile to build Docker containers.

Docker uses Dockerfile to build a Docker image, and you can define all required dependencies and steps to run the Docker image inside your container. How to access resources, such as storage and network interfaces, can be defined in this Dockerfile. You can also virtualize disk drives inside this environment using Dockerfile. All resources that you have defined inside this Dockerfile will be isolated from the outside of your container. But you can define the application port to use it the outside world.

After the creation of Dockerfile, put this file in the application directory. Let's start creating a new blank file in our text editor and save it into the application directory. The following are the steps to write Dockerfile:

Create a file using the vim command and save it as Dockerfile. Note that the filename must be Dockerfile with a capital D.

Let's write the Dockerfile instructions into your Dockerfile:

#This is a Dockerfile for a microservice application 
 
# Use an official Java 8 runtime as a parent image 
FROM openjdk:8-jdk-alpine 
 
#Set maintainer email id 
MAINTAINER [email protected]  
 
# Set the working directory to /app 
WORKDIR /app 
 
# Copy the current directory contents into the container at /app 
ADD . /app 
 
# Install any needed packages libraries 
RUN mvn clean install 
 
# Build and create jar using maven command 
RUN mvn package 
 
# Make port 80 available to the world outside this container 
EXPOSE 80 
 
# Define environment variable 
ENV JAVA_OPTS="" 
 
# Run accounts-microservice.jar when the container launches 
CMD ["java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar accounts-microservice.jar", "accounts-microservice.jar"] 

As you can see, the preceding Dockerfile has some instructions to create a Docker image and container. The following points need to be noted from the Dockerfile:

  • The instructions in Dockerfile are case-insensitive; that means it is not necessary to write commands in a particular case, but you must follow conventions that recommend using uppercase.
  • Docker follows the top-to-bottom order to run instructions of Dockerfile. Each Dockerfile must have the first instruction as FROM in order to specify the base image. In our example, we are creating an image from the openjdk:8-jdk-alpine image.
  • In the preceding Dockerfile, a statement beginning with # is treated as a comment, such as #This is a Dockerfile for a microservice application. Other instructions, such as RUN, CMD, FROM, EXPOSE, and ENV can be used in our Dockerfile.
  • The next command is the person who is going to maintain this image. Here you specify the MAINTAINER keyword and just mention the email ID.
  • You can use the WORKDIR command to set the working directory for any RUN, CMD, and COPY instruction that follows it in Dockerfile. If the working directory does not exist, it will be created by default. This command can be used multiple times in Dockerfile.
  • The ADD command is used to copy the current directory contents into the container at /app.
  • The RUN command is used to run instructions against the image. In our case, the first RUN command is used to run the mvn command install and clean any needed packages libraries in our microservice application. The second RUN command creates a JAR file by running the mvn package maven command.
  • The EXPOSE command of Dockerfile is used to make port 80 available to the world outside this container.
  • The ENV command can be used to define the environment variables for our microservice application.
  • The last CMD command is used to execute the microservice application by the image.
  • Save Dockerfile and in the next section, we will discuss how to build the image using Spring Boot application.

As we have created Dockerfile with some instructions, there are more instructions available for Dockerfile as per as your application requirements. Because all instruction commands are very simple and easy to write, I am not going to explain them. The docker build command looks up the Dockerfile for instructions for building. Let's move to create a Spring Boot application and dockerize it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.151