To understand Docker, let's create and run a docker image of a very simple application. Specifically, run an application that outputs "Hello world!" Once a second. Aim to do everything from build to execution on the container.
The Java source code is as follows. https://github.com/nannany/very-simple-application
I ran it on Windows10 HOME. I used Docker Toolbox to create the operating environment for Docker on Windows. See the following article for details. https://qiita.com/idani/items/fb7681d79eeb48c05144
Write Dockerfile → Execute docker build command The docker image is created with.
The rough flow of docker image creation looks like the figure below. As a character to be aware of,
Decide which path to add files to the build context when executing the docker build command.
At this time, describe the files that you do not want to bring to the build context in the .dockerignore
file.
Also, the COPY command in the Dockerfile determines what to bring to the image in the build context.
As a whole, it is as follows.
FROM ubuntu:disco
COPY . .
RUN apt-get update && apt-get install -y \
maven \
openjdk-8-jre \
&& cd simple \
&& mvn package
CMD ["java","-jar","simple/target/simple-1.0-SNAPSHOT.jar"]
First, write FROM
to select the base image.
Here, select ʻubuntu: disco` as appropriate.
Then write COPY ..
to copy the file from the build context to the image.
Next, install the packages (maven and openjdk) required to build the source and execute Java, and execute the maven jar creation command.
RUN apt-get update && apt-get install -y \
maven \
openjdk-8-jre \
&& cd simple \
&& mvn package
The way of writing is to imitate the following, and I was conscious of minimizing the number of layers and doing apt-get update and install at the same time. http://docs.docker.jp/engine/articles/dockerfile_best-practice.html
Finally, I wrote the following so that java -jar simple / target / simple-1.0-SNAPSHOT.jar
is executed after the container is started.
CMD ["java","-jar","simple/target/simple-1.0-SNAPSHOT.jar"]
The docker command to be executed when creating an image is
docker build -t simple-application -f Dockerfile.cmd .
In -t simple-application
, the name of the image is simple-application.
In -f Dockerfile.cmd
, the Dockerfile used when creating the image is Dockerfile.cmd in the path where the above command is executed. (By default, the Dockerfile in the path where you are executing the command is selected)
The final .
Means that the path under which the command is being executed will be added to the build context.
Let's operate the image created above with the following command.
docker run simple-application-cmd
It was displayed as below and it worked.
Above, I specified ʻubuntu: disco` as the base image and installed maven and Java on the image with RUN. However, since there is a base image that originally contains maven and Java, the Dockerfile that uses it is as follows. (I skipped it because I got an error in the test for some reason)
FROM maven:3-jdk-8
COPY . .
RUN cd simple && mvn package -Dmaven.test.skip=true
CMD ["java","-jar","simple/target/simple-1.0-SNAPSHOT.jar"]
Recommended Posts