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 Dockerfile?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is Dockerfile". In daily operation, I believe many people have doubts about what is Dockerfile. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is Dockerfile?" Next, please follow the editor to study!

A DockerFile construction process parses the basic knowledge of 1.Dockerfile content

Each reserved word instruction must be an uppercase letter followed by at least one parameter

Instructions are executed sequentially from top to bottom

# for comments

Each instruction creates a new mirror layer and commits the mirror.

2 the general process of executing Dockerfile by Docker

Docker runs a container from the base image

Execute an instruction and make changes to the container

Perform an operation similar to docker commit to submit a new mirror layer

Docker runs a new container based on the image you just submitted

Execute the next instruction in dockerfile until all instructions have been executed

3 small summaries

From the perspective of application software, Dockerfile, Docker image and Docker container represent three different stages of software.

Dockerfile is the raw material of software.

Docker images are deliverables of the software

The Docker container can be thought of as the running state of the software. Dockerfile is oriented to development, Docker image becomes the delivery standard, and Docker container involves deployment and operation and maintenance, all of which are indispensable. Together, they serve as the cornerstone of Docker system.

Dockerfile, you need to define a Dockerfile,Dockerfile that defines everything the process needs. Dockerfile involves executing code or files, environment variables, dependency packages, runtime environment, dynamic link libraries, distributions of the operating system, service processes and kernel processes (when the application process needs to deal with system services and kernel processes, you need to consider how to design the permission control of namespace), and so on.

Docker image. After defining a file with Dockerfile, docker build will generate a Docker image. When you run the Docker image, you will actually start to provide services.

Docker container, which provides services directly.

II. DockerFile architecture (reserved word instruction) FROM

Basic image. Which image is the current new image based on?

MAINTAINER

Name and email address of the mirror maintainer

RUN

Commands that need to be run when the container is built

EXPOSE

The port exposed by the current container

WORKDIR

Specify the working directory in which the terminal logs in by default after creating the container, and a foothold.

ENV

Used to set environment variables during the construction of an image

For example, the environment variable ENV MY_PATH / usr/mytest can be used in any subsequent RUN instruction, just as you specify the environment variable prefix before the command, or you can use these environment variables directly in other instructions, such as WORKDIR $MY_PATH

ADD

Copy the files under the host directory into the mirror and the ADD command automatically processes URL and decompresses the tar package

COPY

Similar to ADD, copy files and directories to the image. Copy the file / directory in the build context directory to the location in the mirror of the new layer

VOLUME

Container data volumes for data preservation and persistence

CMD

Specify a command to run when the container starts

Three-case Base image (scratch)

99% of the images in Docker Hub are built by installing and configuring the required software in the base image

Custom image mycentos1 authoring

Hub default CentOS image

MyCentOS content Dockerfile

FROM centosMAINTAINER zhlENV MYPATH / usr/localWORKDIR $MYPATHRUN yum-y install vimRUN yum-y install net-toolsEXPOSE 80CMD echo $MYPATHCMD echo "success-ok" CMD / bin/bash2 Construction

Docker build-f DockerFile directory-t new image name: TAG. . Represents the current directory

Docker build-f / opt/docker/mydocker/Dockerfile-t zhl/centos1.1.

Root@zhanghl:/opt/docker/mydocker# docker build-f / opt/docker/mydocker/Dockerfile-t zhl/centos1.1 .Sending build context to Docker daemon 2.048kBStep 1 usr/local 10: FROM centos-- > 300e315adb2fStep 2 usr/local 10: MAINTAINER zhl-- > Running in 9cef0b83aa4dRemoving intermediate container 9cef0b83aa4d-- > e03c70aa84ddStep 3 ENV MYPATH 10: ENV MYPATH / usr/local-- > Running in a06eb1e589daRemoving intermediate container a06eb1e589da-- > f817774de33bStep 4 MYPATH 10: WORKDIR $MYPATH-- > Running in 2638200252e1Removing intermediate container 2638200252e1-- > e0ac249d8189Step 5 / 10: RUN yum-y install vim-- > Running in 23a14562d0d6. -> 059822073508Step 6 install net-tools 10: RUN yum-y install net-tools-> Running in 4a437d96a483. -> c49737740d20Step 7 ok 10: EXPOSE 80-- > Running in 326caf878a8eRemoving intermediate container 326caf878a8e-- > 6690abcca60fStep 8 ok 10: CMD echo $MYPATH-- > Running in 47664a0e6e10Removing intermediate container 47664a0e6e10-- > 8db1cff1fad3Step 9 success 10: CMD echo "success-ok"-- > Running in 7ad2234cdfbdRemoving intermediate container 7ad2234cdfbd-- > a08723e08f62Step 10 a08723e08f62Step 10: CMD / bin/bash-> Running in e0ecfa0bf1f3Removing intermediate container e0ecfa0bf1f3-> 77c006bf4b15Successfully built 77c006bf4b15Successfully tagged zhl/centos1.1:latestroot@zhanghl:/opt/docker/mydocker#3 operator

Docker run-it new image name: TAG.

Root@zhanghl:/opt/docker/mydocker# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEzhl/centos1.1 latest 77c006bf4b15 3 minutes ago 291MBzhl/centos latest e4998e64dece 38 hours ago 209MBzhl/mytomcat 1.1 a636ea3caaed 4 days ago 672MBmongo latest 995ccc33e58f 8 days ago 449MBtomcat latest c0e850d7b9bb 9 days ago 667MBhello-world latest d1165f221234 8 weeks ago 13.3kBcentos Latest 300e315adb2f 4 months ago 209MBroot@zhanghl:/opt/docker/mydocker# docker run-it 77c006bf4b15 [root@01ba3472d1c3 local] # pwd/usr/local [root@01ba3472d1c3 local] # ifconfigeth0: flags=4163 mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 21 bytes 2716 (2.6KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@01ba3472d1c3 local] # vim a.txt [root@01ba3472d1c3 local] # cat a.txthello4 lists the change history of the image

Docker history [Mirror name]

[root@01ba3472d1c3 local] # docker history 77c006bf4b15bash: docker: command not found [root@01ba3472d1c3 local] # exitexitroot@zhanghl:/opt/docker/mydocker# docker history 77c006bf4b15IMAGE CREATED CREATED BY SIZE COMMENT77c006bf4b15 7 minutes ago / bin/sh-c # (nop) CMD ["/ bin/sh" > CMD / ENTRYPOINT image case

All are commands that specify a container to run when it starts.

CMD

There can be multiple CMD instructions in Dockerfile, but only the last one takes effect, and CMD will be replaced by the parameter after docker run

Explanation and demonstration of case tomcat

The ls-l command is appended to replace the command to run tomcat in principle, so that docker does not start.

ENTRYPOINT

The parameters after docker run are passed to ENTRYPOINT as arguments, after which a new command combination is formed.

Case

Make a CMD version of a container that can query IP information

DockerFile: Dockerfile-myid

FROM centosRUN yum install-y curlCMD ["curl", "www.trackip.net/ip?json"] root@zhanghl:/opt/docker/mydocker# docker build-f Dockerfile-myid-t myid:1.1 .Sending build context to Docker daemon 3.072kBStep 1 myid:1.1 3: FROM centos-> 300e315adb2fStep 2 curl 3: RUN yum install-y curl-> Using cache-- > 82abeb79293fStep 3max 3: CMD ["curl" "www.trackip.net/ip?json"]-- > Running in bafa2c73ec55Removing intermediate container bafa2c73ec55-- > a566bc415a96Successfully built a566bc415a96Successfully tagged myid:1.1root@zhanghl:/opt/docker/mydocker# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyid 1.1 a566bc415a96 5 seconds ago 243MBmyip latest 251ed5335c1a 30 minutes ago 243MBzhl/centos1.1 latest 77c006bf4b15 3 hours ago 291MBzhl/centos latest e4998e64dece 40 hours ago 209MBzhl/mytomcat 1.1 a636ea3caaed 4 days ago 672MBmongo latest 995ccc33e58f 8 days ago 449MBtomcat latest c0e850d7b9bb 9 days ago 667MBhello-world latest d1165f221234 8 weeks ago 13.3kBcentos latest 300e315adb2f 4 months ago 209MBroot@zhanghl:/opt/docker/mydocker# docker run-it myid:1.1 {"IP": "175.161.93.164" "Country": "CN", "UA": "curl\ / 7.61.1", "Time": 1619934102}

Problem, add a parameter-I to report an error at runtime

Root@zhanghl:/opt/docker/mydocker# docker run-it myid:1.1-idocker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "- I": executable file not found in $PATH: unknown.

WHY

We can see errors that cannot be found in the executable file, executable file not found. As we said earlier, the image name is followed by command, and the runtime replaces the default value of CMD. So the-I here replaces the original CMD instead of adding it after the original curl-s http://ip.cn. And-I is not an order at all, so naturally it can't be found. So if we want to add the parameter-I, we have to re-enter the command completely: $docker run myip curl-s http://ip.cn-I

Make a container for querying IP information in ENTRYPOINT version

DockerFile:

FROM centosRUN yum install-y curlENTRYPOINT ["curl", "www.trackip.net/ip?json"]

Build

Root@zhanghl:/opt/docker/mydocker# docker build-f Dockerfile-Entrypoint-myip-t myip:1.2 .Sending build context to Docker daemon 4.096kBStep 1 ENTRYPOINT 3: FROM centos-> 300e315adb2fStep 2 curl 3: RUN yum install-y curl-> Using cache-- > 82abeb79293fStep 3 82abeb79293fStep 3: ENTRYPOINT ["curl", "www.trackip.net/ip?json"]-> Running in 70070b1d358cRemoving intermediate container 70070b1d358c-- > f1f60b285381Successfully built f1f60b285381Successfully tagged myip:1.2root@zhanghl:/opt/docker/mydocker#

Running

Root@zhanghl:/opt/docker/mydocker# docker run-it myip:1.2 {"IP": "175.161.93.164", "Country": "CN", "UA": "curl\ / 7.61.1", "Time": 1619934632} root@zhanghl:/opt/docker/mydocker# root@zhanghl:/opt/docker/mydocker# docker run-it myip:1.2-iHTTP/1.1 200 OKDate: Sun 02 May 2021 05:50:38 GMTContent-Type: text/plainContent-Length: 76Connection: keep-aliveSet-Cookie: _ _ cfduid=d6e71854852b8ade1b5e9d1ba03386d6a1619934638 Expires=Tue, 01-Jun-21 05:50:38 GMT; path=/; domain=.trackip.net; HttpOnly SameSite=LaxCF-Cache-Status: DYNAMICcf-request-id: 09cd3a08d100000530688e1000000001Report-To: {"group": "cf-nel", "max_age": 604800, "endpoints": [{"url": "https:\ / a.nel.cloudflare.com\ / report?s=aZpOakyvJEJIy5RKJSanPc6kAxlXG390nfy3ADvHSEbFkG7pzavegW8Xwt7RiF%2F6ExbnXvvp9ZiGHKJ0gD%2F%2F6fm5IyWGJPv%2FeKG1mY%2FGnWQ%3D"}]} NEL: {"report_to": "cf-nel", "max_age": 604800} Server: cloudflareCF-RAY: 648ef9214d210530-LAXalt-svc: h4-27 = ": 443" Ma=86400, h4-28 = ": 443"; ma=86400, h4-29 = ": 443"; ma=86400 {"IP": "175.161.93.164", "Country": "CN", "UA": "curl\ / 7.61.1", "Time": 1619934639} root@zhanghl:/opt/docker/mydocker# root@zhanghl:/opt/docker/mydocker#ONBUILD command case

Modify the DockerFile of the previous step

FROM centosRUN yum install-y curlENTRYPOINT ["curl", "www.trackip.net/ip?json"] ONBUILD RUN echo "father images onbuild-886"

Build Mirror

Docker build-f Dockerfile-Entrypoint-myip-t myip_father.

Root@zhanghl:/opt/docker/mydocker# docker build-f Dockerfile-Entrypoint-myip-t myip_father .Sending build context to Docker daemon 5.12kBStep 1 ENTRYPOINT 4: FROM centos-> 300e315adb2fStep 2 curl 4: RUN yum install-y curl-> Using cache-> 82abeb79293fStep 3 82abeb79293fStep 4: ENTRYPOINT ["curl" "www.trackip.net/ip?json"]-- > Using cache-- > f1f60b285381Step 4 father images onbuild-886 4: ONBUILD RUN echo "father images onbuild-886"-> Using cache-- > 1d20c7ac1ee1Successfully built 1d20c7ac1ee1Successfully tagged myip_father:latest

Display information:

Step 4ax 4: ONBUILD RUN echo "father images onbuild-886"

Write the child image DockerFile with myip:1.3 as the parent image. Dockerfile-Entrypoint-myip-sub

FROM myip_fatherRUN yum install-y curlENTRYPOINT ["curl" "www.trackip.net/ip?json"] ONBUILD RUN echo "father images onbuild-886" ~ docker build- f Dockerfile-Entrypoint-myip-sub-t myip_son .Sending build context to Docker daemon 5.12kBStep 1 build trigger 4: FROM myip_father# Executing 1 build trigger-- > Running in 9b6bfe884ccdfather images onbuild-886Removing intermediate container 9b6bfe884ccd-- > d45eda2663bcStep 2 curl 4: RUN yum install-y curl-- > Running in 3f4e383eec45Last metadata expiration check: 1:08:58 ago on Sun May 2 05:10:51 2021.Package curl-7.61.1-14.el8_3.1.x86_64 is already installed.Dependencies resolved.Nothing to do.Completeworthy removing intermediate container 3f4e383eec45-> 4cdefe20ba39Step 3tick 4: ENTRYPOINT ["curl" "www.trackip.net/ip?json"]-- > Running in 395df3226201Removing intermediate container 395df3226201-- > fc869c497482Step 4 father images onbuild-886 4: ONBUILD RUN echo "father images onbuild-886"-> Running in 36d3ac20d0feRemoving intermediate container 36d3ac20d0fe-- > c3615df75130Successfully built c3615df75130Successfully tagged myip_son:latest

The parent onbuild event is triggered during build

ONBUILD RUN echo "father images onbuild-886" Custom Image TOMCAT1 create Experimental Directory / opt/docker/mydocker/tomcat92 create c.txt

Touch c.txt

3 copy the compressed packages installed by jdk and tomcat into the directory total root@zhanghl:/opt/docker/mydocker/tomcat9# ll usage 152772drwxr-xr-x 2 root root 4096 May 2 16:33. / drwxr-xr-x 3 root root 4096 May 2 14:25.. /-rw-r--r-- 1 root root 11486964 May 2 14:30 apache-tomcat-9.0.45.tar.gz-rw-r--r-- 1 root root 0 May 2 14:28 c. Txt root root RW Murray RFT-1 root root 144935989 May 2 16:31 jdk-8u291-linux-x64.tar.gzroot@zhanghl:/opt/docker/mydocker/tomcat9# pwd/opt/docker/mydocker/tomcat94 create a new Dockerfile file FROM centosMAINTAINER ZHL# in the directory and copy the c.txt of the current context of the host to COPY c.txt / usr/local/cincontainer.txt# under the container / usr/local/ path Add java and tomcat to the container ADD jdk-8u291-linux-x64.tar.gz / usr/local/ADD apache-tomcat-9.0.45.tar.gz / usr/local/# install the vim editor RUN yum-y install vim# set the WORKDIR path for work access Login destination ENV MYPATH / usr/localWORKDIR $MYPATH# configure java and tomcat environment variable ENV JAVA_HOME/ usr/local/jdk1.8.0_291ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarENV CATALINA_HOME/ usr/local/apache-tomcat-9.0.45ENV CATALINA_BASE / usr/local/apache-tomcat-9.0.45ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin# container runtime listening The port EXPOSE 808 runs tomcat# ENTRYPOINT ["/ usr/local/apache-tomcat-9.0.45/bin/startup.sh"] # CMD ["/ usr/local/apache-tomcat-9.0.45/bin/catalina.sh" at startup "run"] CMD / usr/local/apache-tomcat-9.0.45/bin/startup.sh & & tail-F / usr/local/apache-tomcat-9.0.45/bin/logs/catalina.out

5 construct docker build-f / opt/docker/mydocker/tomcat9/DockerFile-t mytomcat9. Root @ zhanghl:/opt/docker/mydocker/tomcat9# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmytomcat9 latest 2b12a6a03ea0 2 minutes ago 643MB6 RUNdocker run-d-p 9080 mytomcat9 8080-- name myt9-v / opt/docker/mydocker/tomcat9/test:/usr/local/apache-tomcat-9.0.45/webapps/test-v / opt/docker/mydocker/tomcat9/ Tomcat9logs/:/usr/local/apache-tomcat-9.0.45/logs mytomcat9

Prepare two directories

Total root@zhanghl:/opt/docker/mydocker/tomcat9# mkdir testroot@zhanghl:/opt/docker/mydocker/tomcat9# mkdir tomcat9logsroot@zhanghl:/opt/docker/mydocker/tomcat9# ll usage 152784drwxr-xr-x 4 root root 4096 May 2 17:23. / drwxr-xr-x 3 root root 4096 May 2 14:25.. /-rw-r--r-- 1 root root 11486964 May 2 14:30 apache-tomcat-9.0.45.tar.gz- Rw-r--r-- 1 root root 0 May 2 14:28 c. Rw-r--r-- 1 root root 0 May 2 14:28 c. Txt test/drwxr-xr-x 2 root root 4096 May 2 17:23 tomcat9logs/7 verification

Enter the container to view the contents of the directory

Root@zhanghl:/opt/docker/mydocker/tomcat9# docker exec 882930ee0ac0 ls-ltotal 52drwxr-xr-x 1 root root 4096 May 2 09:16 apache-tomcat-9.0.45drwxr-xr-x 2 root root 4096 Nov 3 15:22 bin-rw-r--r-- 1 root root 0 May 2 06:28 cincontainer.txtdrwxr-xr-x 2 root root 4096 Nov 3 15:22 etcdrwxr-xr-x 2 root root 4096 Nov 3 15:22 gamesdrwxr-xr-x 2 root root 4096 Nov 3 15:22 Includedrwxr-xr-x 8 10143 10143 4096 Apr 7 19:26 jdk1.8.0_291drwxr-xr-x 2 root root 4096 Nov 3 15:22 libdrwxr-xr-x 3 root root 4096 Dec 4 17:37 lib64drwxr-xr-x 2 root root 4096 Nov 3 15:22 libexecdrwxr-xr-x 2 root root 4096 Nov 3 15:22 sbindrwxr-xr-x 5 root root 4096 Dec 4 17:37 sharedrwxr-xr-x 2 root root 4096 Nov 3 15:22 src8 combined with the above container volume to release the tested web service test

Create a Web service in a shared folder in the host

Root@zhanghl:/opt/docker/mydocker/tomcat9/test# mkdir WEB-INFroot@zhanghl:/opt/docker/mydocker/tomcat9/test# cd WEB-INF/root@zhanghl:/opt/docker/mydocker/tomcat9/test/WEB-INF# vim web.xmlroot@zhanghl:/opt/docker/mydocker/tomcat9/test/WEB-INF# vim a.jsp

Create a WEB-INFO directory and create a web.xml under it

Web.xml:

Test

Create an a.jsp in the test directory:

Insert title here-welcome-

Restart the container

Docker restart [container id]

Root@zhanghl:/opt/docker/mydocker/tomcat9/test/WEB-INF# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES882930ee0ac0 mytomcat9 "/ bin/sh-c'/ usr/lo..." 17 minutes ago Up 17 minutes 0.0.0.0 8080/tcp 9080-> 8080/tcp,:: 9080-> 8080/tcp myt9root@zhanghl:/opt/docker/mydocker/tomcat9/test/WEB-INF# docker restart 882930ee0ac0

View a web page

View the log

Root@zhanghl:/opt/docker/mydocker/tomcat9/tomcat9logs# cat catalina.out Mini Summary

At this point, the study of "what is Dockerfile" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report