Customize the image based on Dockerfile

Customize the image based on the Dockerfile and start the container to run the web service based on the image
1. Image concept: The image is a package of the application and its required function libraries, environments, dependencies, and configurations.
2. Mirror structure: including three parts
1. Base image (BaseImage): application-dependent system function library, environment, configuration, files, etc.
2. Layer (layer): add packages, dependencies, configurations, etc. to BaseImage, each Each operation forms a new layer
3. Entry point: the entry point of the image operation, generally the script and parameters of the program startup. The Dockerfile
for custom image construction
is a text file, in which each instruction is used to explain what operation to perform to build the image. Each instruction forms a layer layer.
The following is a basic Dockerfile

#指定基础镜像
FROM. ubuntu:16.04
#配置环境变量,JDK的安装目录
E的V JAVA_ DIR=/usr/local
#拷贝jdk和java项目的包
COPY ./jdk8.tar.gz $JAVA_ _DIR/
COPY ./demo-test-0.0.1-SNAPSHOT.jar /tmp/app.jar
#安装JDK .
RUN cd $JAVA_ _DIR \ .
&& tar -xf ./jdk8.tar.gz \
&& mv ./jdk1.8.0_ _144 ./java8
#配置环境变量
ENV JAVA_ HOME=$JAVA_ DIR/java8
ENV PATH=$PATH: $JAVA_ HOME/bin
#暴露端口
EXPOSE 8090
#入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar

The simplified Dockerfile is as follows (hand over the installation of jdk to the official image java:8-alpine to complete)

#指定基础镜像
FROM java:8-alpine
COPY ./demo-test-0.0.1-SNAPSHOT.jar /tmp/app.jar
#暴露端口
EXPOSE 8090
#入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar

docker-demo.jar: Your own microservice jar package
EXPOSE 8090: Your own microservice port, which will be mapped with the container port when running the container later.
Build the image and start the container, and run the web service steps:
1. Start docker
2. Create a new directory (I created /docker-demo)
3. Enter the directory, and combine the Dockerfile, jdk, and your own microservice jar package prepared earlier upload to this directory
insert image description here

4. Build the image: docker build -t javaweb:1.0 .
javaweb:1.0 Two parts constitute the image, name: version, this part can be set arbitrarily by yourself
(note: there is a "." at the end of the command means to use the current image to create, do not ignore)
insert image description here
5 , Start the container: docker run --name web -p 8090:8090 -d javaweb:1.0
web: container name (unique)
-p 8090:8090: port mapping, the front is the container port, and the back is the service port
-d: represents the background Start
the javaweb:1.0 mirror
insert image description here
6. Access the project with a browser
insert image description here
7. Check the log: docker logs -f container name
insert image description here

Guess you like

Origin blog.csdn.net/qiaojunt/article/details/125080110