Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed description of the Dockerfile instruction in docker to create a mirror

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Write at the front:

To continue the study of docker, I successfully ran tomcat with docker yesterday, but manually installed jdk and tomcat in the container of image running in centos. Today, learn to create an image with Dockerfile, build a java environment on it, and run a small spring boot project.

Dockerfile:

Dockerfile consists of one line of command statements and supports commenting with "#". Generally speaking, Dockerfile is divided into four parts: basic image information, maintainer information, mirror operation instructions and instructions executed when the container starts.

Writing rules and instruction usage of Dockerfile

Dockerfile instructions ignore case, it is recommended to use uppercase, use # as a comment, each line supports only one instruction, each instruction can carry multiple parameters.

Dockerfile instructions can be divided into two types according to their functions: build instructions and set instructions. The build directive is used to build the image, and its specified operations are not performed on the container running image; the setting directive is used to set the properties of the image, and its specified operations will be performed in the container running image.

Dockerfile directive:

1 、 FROM

Format: FROM or FROM:

The first instruction must be a FROM instruction, and if you create multiple mirrors in the same Dockerfile, you can use multiple FROM instructions (one for each mirror)

2 、 MAINTAINET

Format: MAINTAINET

Specify the maintainer's information

3 、 RUN

Format: RUN or RUN [",", "]

Each instruction will be executed based on the current mirror and submitted as a new mirror. (you can wrap it with "\")

4 、 CMD

Format: CMD [",", "]

Specifies the command to be executed when the container is started. There can be only one CMD instruction per Dockerfile, and if more than one instruction is specified, the last one is executed. (will be overwritten by the command specified at startup)

5 、 EXPOSE

Format: EXPOSE [...]

Tell the Docker server to expose the port, and port mapping needs to be done through-p when the container is started.

6 、 ENV

Format: ENV

Specifies the environment variable, which is used by the RUN instruction and saved while the container is running

7 、 ADD

Format: ADD

Copy the specified to the container, which can be a relative path to the directory where Dockerfile is located; it can be URL or tar.gz (automatic decompression)

8 、 COPY

Format: COPY

Copy the local host's (relative path to the directory where Dockerfile is located) to the container (COPY is recommended when using the local directory as the source directory)

9 、 ENTRYPOINT

Format: ENTRYPOINT [",", "]

Configure the commands that are executed after the container starts and cannot be overridden by the parameters provided by docker run. (there can be only one ENTRYPOINT in each Dockerfile, and when more than one is specified, only the last one takes effect)

10 、 VOLUME

Format: VOLUME ["/ mnt"]

Create a mount point that can be mounted from a local host or other container, which is generally used to store databases, data to be maintained, etc.

11 、 USER

Format: USER daemon

Specify the user name or UID when the container is run, and subsequent RUN will also use the specified user.

12 、 WORKDIR

Format: WORKDIR / path/to/workdir

Configure the working directory for subsequent RUN, CMD, and ENTRYPOINT instructions. (multiple WORKDIR instructions can be used, and subsequent commands will be based on the path specified by the previous command if the argument is a relative path.)

13 、 ONBUILD

Format: ONBUILD [INSTRUCTION]

Configure the operation instructions performed when the created mirror is used as the base mirror for other newly created mirrors

-split line--

Create a mirror:

1. Prepare jdk and spring boot small projects that need to be run.

Extract the jdk package to the directory where Dockerfile is located, and copy the spring boot mini project websocket.jar to the directory where Dockerfile is located:

2. Create a Dockerfile with the following contents:

FROM centosMAINTAINER yaolinCOPY jdk1.7.0_79 jdk1.7.0_79ADD websocket.jar app.jarENV JAVA_HOME=/jdk1.7.0_79ENV PATH=$JAVA_HOME/bin:$PATHENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarEXPOSE 8080ENTRYPOINT ["java", "- jar", "/ app.jar"]

Specify the base image centos

Specify maintainer information yaolin

Copy jdk

Add a small spring boot project and rename it to app.jar

Configure the jdk environment

Expose port 8080

Specify the execution to start the spring boot mini project

3. Compile and generate an image (. Represents the current path)

Docker build-t yaolin/websocket.

4. View the generated image

Docker images

5. Create a container and run

Docker run-p 192.168.0.105 8080-- name websocket yaolin/websocket

You can see that the container is running normally and printing the log, but this mode of operation preempts the terminal. Start using docker start websocket instead:

6. Access (on the physical machine, not on the virtual machine, here there seems to be a problem with my Spring boot project. A 500error is reported, but the access indicates that the container has run the spring boot project normally)

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report