Using Docker file to build Docker images for SpringBook applications

Posted by Black Rider on Sun, 08 Sep 2019 14:10:09 +0200

SpringBoot e-commerce project mall (20k+star) address: https://github.com/macrozheng/mall

brief introduction

Last time I wrote an article Building Docker Image with Maven Plug-in It describes the way to construct docker image through docker-maven-plugin, which relies on the self-built Registry image warehouse. This article describes another way to use Dockerfile to build a docker image, which does not rely on a self-built image warehouse, but requires only the jar package and a Dockerfile file to be applied.

Common Dockerfile instructions

ADD

For copying files, format:

ADD <src> <dest>

Examples:

# Copy the mall-tiny-docker-file.jar package in the current directory to the / directory of the docker container
ADD mall-tiny-docker-file.jar /mall-tiny-docker-file.jar

ENTRYPOINT

Specifies the commands to be executed when the docker container is started in the following format:

ENTRYPOINT ["executable", "param1","param2"...]

Examples:

# Specify that the docker container runs the jar package at startup
ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]

ENV

Used to set environment variables, format:

ENV <key> <value>

Examples:

# Setting root password at mysql runtime
ENV MYSQL_ROOT_PASSWORD root

EXPOSE

Declare the port that needs to be exposed (only declare that the port will not be opened), format:

EXPOSE <port1> <port2> ...

Examples:

# Declare that the service runs on port 8080
EXPOSE 8080

FROM

Specify the base image to be relied on, format:

FROM <image>:<tag>

Examples:

# This image needs to rely on the mirror of Java 8
FROM java:8

MAINTAINER

Specify the name and format of the maintainer:

MAINTAINER <name>

Examples:

MAINTAINER macrozheng

RUN

Commands executed during container construction can be used to customize container behavior, such as installing software, creating files, etc.

RUN <command>
RUN ["executable", "param1","param2"...]

Examples:

# During container construction, you need to create a mall-tiny-docker-file.jar file in / directory
RUN bash -c 'touch /mall-tiny-docker-file.jar'

Building SpringBook Application Mirror Using Dockerfile

Writing Dockerfile files

# The basic image that the image needs to rely on
FROM java:8
# Copy the jar package in the current directory to the / directory of the docker container
ADD mall-tiny-docker-file-0.0.1-SNAPSHOT.jar /mall-tiny-docker-file.jar
# Create a mall-tiny-docker-file.jar file during the run
RUN bash -c 'touch /mall-tiny-docker-file.jar'
# Declare that the service runs on port 8080
EXPOSE 8080
# Specify that the docker container runs the jar package at startup
ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]
# Name of designated maintainer
MAINTAINER macrozheng

Packaging applications with maven

Double-click the package command in IDEA to package:

After successful packaging, display:

[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ mall-tiny-docker-file ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.749 s
[INFO] Finished at: 2019-06-16T14:11:07+08:00
[INFO] Final Memory: 43M/306M
[INFO] ------------------------------------------------------------------------

Upload the application jar package and Dockerfile files to the linux server:

Building docker image on Linux

Execute the following commands in the directory where Dockerfile is located:

# - t denotes the specified mirror repository name / mirror name: mirror label. denotes the use of the Docker file in the current directory
docker build -t mall-tiny/mall-tiny-docker-file:0.0.1-SNAPSHOT .

Output the following information:

Sending build context to Docker daemon  36.37MB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : ADD mall-tiny-docker-file-0.0.1-SNAPSHOT.jar /mall-tiny-docker-file.jar
 ---> c920c9e9d045
Step 3/5 : RUN bash -c 'touch /mall-tiny-docker-file.jar'
 ---> Running in 55506f517f19
Removing intermediate container 55506f517f19
 ---> 0727eded66dc
Step 4/5 : EXPOSE 8080
 ---> Running in d67a5f50aa7d
Removing intermediate container d67a5f50aa7d
 ---> 1b8b4506eb2d
Step 5/5 : ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]
 ---> Running in 0c5bf61a0032
Removing intermediate container 0c5bf61a0032
 ---> c3614dad21b7
Successfully built c3614dad21b7
Successfully tagged mall-tiny/mall-tiny-docker-file:0.0.1-SNAPSHOT

View the docker image:

Run mysql service and set it up

1. Start with the docker command:

docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root  \
-d mysql:5.7

2. Enter the docker container running mysql:

docker exec -it mysql /bin/bash

3. Open the client with mysql command:

mysql -uroot -proot --default-character-set=utf8

4. Modify the permissions of the root account so that any ip can access:

grant all privileges on *.* to 'root'@'%'

5. Create mall database:

create database mall character set utf8

6. Copy mall.sql file to mysql container/directory:

docker cp /mydata/mall.sql mysql:/

7. Import sql file into database:

use mall;
source /mall.sql;

Running mall-tiny-docker-file application

docker run -p 8080:8080 --name mall-tiny-docker-file \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny-docker-file/logs:/var/logs \
-d mall-tiny/mall-tiny-docker-file:0.0.1-SNAPSHOT

Access Interface Document Address http://192.168.3.101:8080/swagger-ui.html:

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-docker-file

Public Number

mall project In the whole series of learning courses, we pay attention to the first time acquisition of the public number.

Topics: Linux Docker MySQL Java snapshot