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

What is the process of building DockerFile?

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how the construction process of DockerFile is". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how the construction process of DockerFile is.

DockerFileDockerFile introduction

Dockerfile is a file used to build docker images! Command parameter script!

Build steps:

1. Write a dockerfile file

2. Docker build is built into a mirror image

3. Docker run runs the image

4. Docker push publish image (DockerHub, Aliyun image repository! (divided into public and private)

Check out how the authorities do it?

Many official images are basic packages, and many functions are not available. We usually build our own images!

Officials can make images, and so can we!

DockerFile construction process

Basics:

1. Each reserved keyword (instruction) must be in uppercase

2. Execute from top to bottom

3 # indicates a comment

4. Each execution will create and submit a new mirror layer and submit it!

Dockerfile is development-oriented. If we want to release the project and do a mirror image in the future, we need to write a dockerfile file, which is very simple!

Docker image has gradually become the standard for enterprise development and delivery, and must be mastered!

Steps: development, deployment, online, operation and maintenance. not a single one can be omitted

DockerFile: build file, defines all the steps, source code

DockerImages: the image generated by DockerFile, and the final product released and run (originally jar package, war package)

Docker container: a container is an image that runs to provide services.

DockerFile's instruction

In the past, we all used to use other people's. After learning the instructions, let's practice writing a mirror image by ourselves!

FROM # basic image, everything starts from here to build, such as centos

MAINTAINER # who wrote the image, name + mailbox

RUN # commands that need to be run when building an image

ADD # steps, such as tomcat image, this tomcat package! Add content

Working directory of the WORKDIR # image

VOLUME # mounted directory

EXPOSE # exposes port configuration (not frequently used)

CMD # specifies the command to run when this container starts. Only the last one will take effect and can be replaced.

ENTRYPOINT # specifies the command to run when this container starts, and you can append the command

ONBUILD # when you build an inherited DockerFile, the ONBUILD instruction is run. It's a trigger command.

COPY # is similar to ADD, copying our files to the image

ENV # sets environment variables when building!

Actual combat test

99% of the images in Docker Hub are FROM scratch from this base image, and then configure the required software and configuration to build.

Create your own centos

# 1 write the file of DockerFile

[root@dockertest dockerfile] # cat mydockerfile-centos

FROM centos

MAINTAINER qls

ENV MYPATH / usr/local

WORKDIR $MYPATH

RUN yum-y install vim

RUN yum-y install net-tools

EXPOSE 80

CMD echo $MYPATH

CMD echo "- end----"

CMD / bin/bash

# 2 build an image through a file

# Command docker build-f dockerfile file path-t image name: [tag]

Successfully built 8563498e0ad3

Successfully tagged mycentos:0.1

# 3 Test run

Comparison: previous native centos

We added the mirror image after

We can list the change history of the local image

We usually get a mirror image, so we can study how it is done.

The difference between CMD and ENTRYPOINT

CMD # specifies the command to run when this container starts. Only the last one will take effect and can be replaced.

ENTRYPOINT # specifies the command to run when this container starts, and you can append the command

Test cmd

# write DockerFile files

[root@dockertest dockerfile] # vim dockerfile-cmd-test

FROM centos

CMD ["ls", "- a"]

# build build Image

[root@dockertest dockerfile] # docker build-f dockerfile-cmd-test-t cmdtest.

...

Successfully built 8a6998147175

Successfully tagged cmdtest:latest

# run runs and finds that the ls-a command is in effect

[root@dockertest dockerfile] # docker run 8a6998147175

.

..

.dockerenv

Bin

Dev

Etc

Home

Lib

Lib64

# want to add a command-l ls-al

[root@dockertest dockerfile] # docker run 8a6998147175-l

Docker: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "- l": executable file not found in $PATH: unknown.

In the case of # cmd,-l replaces the CMD ["ls", "- a"] command, but-l is not a command, so an error is reported!

Test ENTRYPOINT

[root@dockertest dockerfile] # vim dockerfile-cmd-entrypoint

FROM centos

ENTRYPOINT ["ls", "- a"]

[root@dockertest dockerfile] # docker build-f dockerfile-cmd-entrypoint-t entrypoint-test.

Sending build context to Docker daemon 4.096kB

Step 1/2: FROM centos

-- > 300e315adb2f

Step 2ax 2: ENTRYPOINT ["ls", "- a"]

-- > Running in d309ff728d2a

Removing intermediate container d309ff728d2a

-- > e4c1e1e6d384

Successfully built e4c1e1e6d384

Successfully tagged entrypoint-test:latest

[root@dockertest dockerfile] # docker run e4c1e1e6d384

.

..

.dockerenv

Bin

Dev

Etc

Home

Lib

Lib64

Lost+found

Media

Mnt

Opt

Proc

Root

Run

Sbin

Srv

Sys

Tmp

Usr

Var

# our additional command is directly concatenated after our ENTRYPOINT command!

[root@dockertest dockerfile] # docker run e4c1e1e6d384-l

Total 0

Drwxr-xr-x 1 root root 6 Jan 22 01:26.

Drwxr-xr-x 1 root root 6 Jan 22 01:26..

-rwxr-xr-x 1 root root 0 Jan 22 01:26. Dockerenv

Lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin-> usr/bin

Drwxr-xr-x 5 root root 340 Jan 22 01:26 dev

Drwxr-xr-x 1 root root 66 Jan 22 01:26 etc

Drwxr-xr-x 2 root root 6 Nov 3 15:22 home

Lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib-> usr/lib

Lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64-> usr/lib64

Drwx- 2 root root 6 Dec 4 17:37 lost+found

Drwxr-xr-x 2 root root 6 Nov 3 15:22 media

Drwxr-xr-x 2 root root 6 Nov 3 15:22 mnt

Drwxr-xr-x 2 root root 6 Nov 3 15:22 opt

Dr-xr-xr-x 124 root root 0 Jan 22 01:26 proc

Dr-xr-x--- 2 root root 162 Dec 4 17:37 root

Drwxr-xr-x 11 root root 163 Dec 4 17:37 run

Lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin-> usr/sbin

Drwxr-xr-x 2 root root 6 Nov 3 15:22 srv

Dr-xr-xr-x 13 root root 0 Jan 20 02:39 sys

Drwxrwxrwt 7 root root 145 Dec 4 17:37 tmp

Drwxr-xr-x 12 root root 144 Dec 4 17:37 usr

Drwxr-xr-x 20 root root 262 Dec 4 17:37 var

[root@dockertest dockerfile] #

Many of the commands in Dockerfile are so similar that you need to compare tests to understand them!

Actual combat: Tomcat mirror image

1. Prepare tomcat package for image files and jdk package.

2, the preparation of dockerfile file, the official name Dockerfile,buile will automatically look for this file, do not need to specify-f!

FROM centos

MAINTAINET qls

COPY readme.txt / usr/local/readme.txt

ADD jdk-8u60-linux-x64.tar.gz / usr/local/

ADD apache-tomcat-8.0.27.tar.gz / usr/local

RUN yum-y install vim

ENV MYPATH / usr/local

WORKDIR $MYPATH

ENV JAVA_HOME / usr/local/jdk1.8.0_60

ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

ENV CATALINA_HOME / usr/local/apache-tomcat-8.0.27

ENV CATALINA_BASE / usr/local/apache-tomcat-8.0.27

ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD / usr/local/apache-tomcat-8.0.27/bin/startup.sh & & tail-F / usr/local/apache-tomcat-8.0.27/bin/logs/catalina.out

3. Build an image

# docker build-t diytomcat.

4. Start the image

5. Access testing

6. Publish the project (because the volume is mounted, you can publish the project if you write the project locally)

Hello,qls

Hello World!

Found: the project is deployed successfully, you can visit ok directly!

Thank you for your reading, the above is the content of "how the DockerFile construction process is", after the study of this article, I believe you have a deeper understanding of the DockerFile construction process, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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