Abstract
Last time I wrote an article using Maven plugin to build A Docker image for SpringBoot application, which describes the way to build a Docker image through the docker-Maven-plugin, which needs to rely on the self-built Registry image repository. This article will talk about another way, using Dockerfile to build docker image, this way does not need to rely on the self-built image repository, just need the application JAR package and a Dockerfile file.
Dockerfile common directives
ADD
For copying files, format:
ADD <src> <dest>
Copy the code
Example:
#Copy the mall-tiny-docker-file.jar package from the current directory to the/directory of the Docker container
ADD mall-tiny-docker-file.jar /mall-tiny-docker-file.jar
Copy the code
ENTRYPOINT
Specifies the command to be executed when the docker container is started.
ENTRYPOINT ["executable"."param1"."param2". ]Copy the code
Example:
#Specifies that the Jar package is run when the Docker container starts
ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]
Copy the code
ENV
Used to set environment variables in the following format:
ENV <key> <value>
Copy the code
Example:
#Set the root password when mysql is running
ENV MYSQL_ROOT_PASSWORD root
Copy the code
EXPOSE
Declare ports to be exposed (only declare ports not to be opened) in the following format:
EXPOSE <port1> <port2> ...
Copy the code
Example:
#Declare that the service is running on port 8080
EXPOSE 8080
Copy the code
FROM
Specify the base image to rely on in the format:
FROM <image>:<tag>
Copy the code
Example:
#This image needs to be a dependent image of java8
FROM java:8
Copy the code
MAINTAINER
Specify the name of the maintainer in the following format:
MAINTAINER <name>
Copy the code
Example:
MAINTAINER macrozheng
Copy the code
RUN
This command is used to customize the behavior of the container, such as installing some software, creating some files, etc.
RUN <command>
RUN ["executable"."param1"."param2". ]Copy the code
Example:
#During container construction, you need to create a mall-tiny-docker-file.jar file under /
RUN bash -c 'touch /mall-tiny-docker-file.jar'
Copy the code
Build the SpringBoot application image using Dockerfile
Write a Dockerfile file
#The mirror needs the underlying mirror on which it depends
FROM java:8
#Copy the jar packages in the current directory to the/directory of the Docker containerThe ADD mall - tiny - docker - file - 0.0.1 - the SNAPSHOT. Jar/mall - tiny - docker - file. The jar#Create a mall-tiny-docker-file.jar file while running
RUN bash -c 'touch /mall-tiny-docker-file.jar'
#Declare that the service is running on port 8080
EXPOSE 8080
#Specifies that the Jar package is run when the Docker container starts
ENTRYPOINT ["java", "-jar","/mall-tiny-docker-file.jar"]
#Specify the name of the maintainer
MAINTAINER macrozheng
Copy the code
Package applications using Maven
Double-click the package command in IDEA to package:
[INFO] - spring - the 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] ------------------------------------------------------------------------Copy the code
Upload the application JAR package and Dockerfile to the Linux server:
Build a Docker image on Linux
Run the following command in the directory where the Dockerfile resides:
#-t specifies the name of the image repository/image. Image label. Dockerfile in the current directory is usedDocker build-t mall-tiny/mall-tiny-docker-file:0.0.1-SNAPSHOTCopy the code
The following information is displayed:
Sending build context to Docker daemon 36.37MB
Step 1/5 : FROM java:8
---> d23bdf5b1b1bADD 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
---> c3614dad21b7Successfully built c3614DAD21b7 Successfully tagged mall-tiny/mall-tiny-docker-file: 0.1-snapshotCopy the code
View docker image:
Run the mysql service and set
1. Use the docker command to start:
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.7Copy the code
2. Enter the docker container running mysql:
docker exec -it mysql /bin/bash
Copy the code
3. Run the mysql command to open the client:
mysql -uroot -proot --default-character-set=utf8
Copy the code
4. Change the permission of the root account so that any IP address can access the root account:
grant all privileges on*. *to 'root'@The '%'
Copy the code
SQL > create mall database;
create database mall character set utf8
Copy the code
6. Copy the mall. SQL file to the/directory of the mysql container:
docker cp /mydata/mall.sql mysql:/
Copy the code
Import SQL file to database:
use mall;
source /mall.sql;
Copy the code
Run the 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 - the file/logs: / var/logs \ - d mall - tiny/mall - tiny - docker - file: 0.0.1 - the SNAPSHOTCopy the code
Access interface document address http://192.168.3.101:8080/swagger-ui.html:
Project source code address
Github.com/macrozheng/…
The public,
Mall project full set of learning tutorials serialized, attention to the public number the first time access.