Construction for the application SpringBoot using Dockerfile image Docker
Original: Dream de Star macrozheng June 17
Last wrote an article using Maven plugins to build Docker mirror , about the way to build docker mirrored by docker-maven-plugin, in this way need to rely on self-built warehouse Registry mirror. This article describes another way, the use of docker Dockerfile constructed mirror, this embodiment does not depend on self mirror warehouse, jar and the package needs to be applied to a Dockerfile file.
Dockerfile common commands
ADD
To copy files, format:
ADD <src> <dest>
Example:
# 将当前目录下的mall-tiny-docker-file.jar包复制到docker容器的/目录下ADD mall-tiny-docker-file.jar /mall-tiny-docker-file.jar
ENTRYPOINT
Commands specified to run at startup docker container format:
ENTRYPOINT ["executable", "param1","param2"...]
Example:
# 指定docker容器启动时运行jar包ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]
ENV
Used to set the environment variable format:
ENV <key> <value>
Example:
# mysql运行时设置root密码ENV MYSQL_ROOT_PASSWORD root
EXPOSE
Statement needs to be exposed ports (only statement does not open ports), format:
EXPOSE <port1> <port2> ...
Example:
# 声明服务运行在8080端口EXPOSE 8080
FROM
Specify the desired base image dependent format:
FROM <image>:<tag>
Example:
# 该镜像需要依赖的java8的镜像FROM java:8
MAINTAINER
Designated maintainer names, format:
MAINTAINER <name>
Example:
MAINTAINER macrozheng
RUN
Command in the container construction process, we can use to customize the behavior of the command of the vessel, such as installing some software, create some files, format:
RUN <command>RUN ["executable", "param1","param2"...]
Example:
# 在容器构建过程中需要在/目录下创建一个mall-tiny-docker-file.jar文件RUN bash -c 'touch /mall-tiny-docker-file.jar'
Building SpringBoot application image using Dockerfile
Write Dockerfile file
# 该镜像需要依赖的基础镜像FROM java:8# 将当前目录下的jar包复制到docker容器的/目录下ADD mall-tiny-docker-file-0.0.1-SNAPSHOT.jar /mall-tiny-docker-file.jar# 运行过程中创建一个mall-tiny-docker-file.jar文件RUN bash -c 'touch /mall-tiny-docker-file.jar'# 声明服务运行在8080端口EXPOSE 8080# 指定docker容器启动时运行jar包ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]# 指定维护者的名字MAINTAINER macrozheng
Use maven packaged applications
Double-click the package in order to package the IDEA: After the successful demonstration of packaging:
[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 package and Dockerfile jar files to the linux server:
Construction of docker image on Linux
Execute the following command in the directory where Dockerfile:
# -t 表示指定镜像仓库名称/镜像名称:镜像标签 .表示使用当前目录下的Dockerfiledocker build -t mall-tiny/mall-tiny-docker-file:0.0.1-SNAPSHOT .
Output the following information:
Sending build context to Docker daemon 36.37MBStep 1/5 : FROM java:8 ---> d23bdf5b1b1bStep 2/5 : ADD mall-tiny-docker-file-0.0.1-SNAPSHOT.jar /mall-tiny-docker-file.jar ---> c920c9e9d045Step 3/5 : RUN bash -c 'touch /mall-tiny-docker-file.jar' ---> Running in 55506f517f19Removing intermediate container 55506f517f19 ---> 0727eded66dcStep 4/5 : EXPOSE 8080 ---> Running in d67a5f50aa7dRemoving intermediate container d67a5f50aa7d ---> 1b8b4506eb2dStep 5/5 : ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"] ---> Running in 0c5bf61a0032Removing intermediate container 0c5bf61a0032 ---> c3614dad21b7Successfully built c3614dad21b7Successfully tagged mall-tiny/mall-tiny-docker-file:0.0.1-SNAPSHOT
Docker View Mirror:
And set the service to run mysql
1. Use the command to start docker:
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. Go to the docker container to run the mysql:
docker exec -it mysql /bin/bash
3. Use the Open command mysql client:
mysql -uroot -proot --default-character-set=utf8
4. Modify the permissions for 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. mall.sql mysql files are copied to the container / directory:
docker cp /mydata/mall.sql mysql:/
7. The import sql file into the database:
use mall;source /mall.sql;
Run mall-tiny-docker-file applications
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 to interface documentation 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