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 explanation of Docker Series 10:docker file instructions (2)

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

10. Instruction 10:RUN instruction

The RUM command is the command to run when building an image based on Dockerfile

For example, get the nginx installation package based on url: after getting the gz package, you need to use the RUN instruction to run tar to unpack the nginx package.

Case: write docker file and make a mirror image

# my first docker fileFROM busybox:latestMAINTAINER "zxhk" ENV DOC_ROOT=/data/\ WORK_DIR=/var/usr/src/\ REPO_DIR=/etc/yum.repos.d/\ MYSQL_DIR=/data/mysql/ COPY index.html ${DOC_ROOT:-/var/www/html/} COPY yum.repos.d $REPO_DIRWORKDIR $WORK_DIRADD http://nginx.org/download/nginx-1.17.6.tar.gz. / RUN cd ${ WORK_DIR} & & tar-xf nginx-1.17.6.tar.gz & & mv nginx-1.17.6 nginxVOLUME $MYSQL_DIREXPOSE 80/tcp 53/udp [root@host1 img1] # docker build-t miniser:v1-8. /

Start a container based on the image to detect whether it has been decompressed

[root@host1 img1] # docker run-rm-name T1 miniser:v1-8 ls / var/usr/src/nginxnginx-1.17.6.tar.gz

11. Instruction 11:CMD instruction

CMD defines the program to be run by default when an image file is launched as a container, that is, the program with a pid of 1

There can be multiple CMD, but only the last one is valid.

There are three formats for CMD instructions

Format 1:CMD

In this format, the command is automatically run as a child of shell. The advantage is that there can be all kinds of special symbols in this command, but the disadvantage is that the process number of the command is not 1.

You can also make the ID of this process 1 with the help of exec

Format 2:CMD [",", "]

In this format, the process is started directly as a process with ID 1.

Format 3:CMD [","]

This format requires the help of ENTRYPOINT to run

Case 1: automatically run apache when you use format 1 to make the container run

Step 1: create a dockerfile

FROM busyboxLABEL maintainer= "zxhk" ENV DOC_ROOT= "/ var/www/html/" RUN mkdir-p ${DOC_ROOT} & &\ echo "test" > ${DOC_ROOT} index.htmlCMD / bin/httpd-f-h ${DOC_ROOT}

Step 2: make a mirror image

[root@host1 img2] # docker build-t miniser:v2-1. /

Step 3: view the details of the image

[root@host1 img2] # docker inspect miniser:v2-1-f'{{.ContainerConfig.Cmd}'[/ bin/sh-c # (nop) CMD ["/ bin/sh"- c"/ bin/httpd-f-h ${DOC_ROOT}"]]

Step 4: create a container

[root@host1 img2] # docker run-name T1-rm-d miniser:v2-1

Step 5: log in to the container and view the container information

[root@host1 img2] # docker exec-it T1 / bin/sh/ # / # netstat-anActive Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 00:: 80: * LISTEN / # / # psPID USER TIME COMMAND 1 root 0:00 / bin / httpd-f-h / var/www/html/ 10 root 0:00 / bin/sh 15 root 0:00 ps/ #

Case 2: automatically run apache when you use format 2 to make the container run

FROM busyboxLABEL maintainer= "zxhk" ENV DOC_ROOT= "/ var/www/html/" RUN mkdir-p ${DOC_ROOT} & &\ echo "test" > ${DOC_ROOT} index.htmlCMD ["/ bin/sh", "- c", "/ bin/httpd", "- f", "- h", "${DOC_ROOT}"]

11. Instruction 11:ENTRYPOINT instruction

The function of ENTRYPOINT is similar to CMD, which is used to specify the program to be run by default after the container starts.

When starting the container, the command specified after run can override the CMD command in the image

Commands run by ENTRYPOINT will not be overridden by commands specified by docker run

Case: using ENTRYPOINT to create an image to start the container to run apache automatically

FROM busyboxLABEL maintainer= "zxhk" ENV DOC_ROOT= "/ var/www/html/" RUN mkdir-p ${DOC_ROOT} & &\ echo "test" > ${DOC_ROOT} index.htmlENTRYPOINT / bin/httpd-f-h ${DOC_ROOT}

Make an image and start the container test

[root@host1 img2] # docker build-t miniser:v2-2. / [root@host1 img2] # docker run-- name T2-- rm-it miniser:v2-2 ls / data

The command will get stuck at this point.

Ls / data is passed as an argument to httpd-f-h / data, because httpd does not recognize this parameter and therefore gets stuck

The combination of ENTRYPOINT and CMD

At this point, the contents after CMD will be passed to ENTRYPOINT as default parameters

FROM busyboxLABEL maintainer= "zxhk" ENV DOC_ROOT= "/ var/www/html/" RUN mkdir-p ${DOC_ROOT} & &\ echo "test" > ${DOC_ROOT} index.htmlCMD ["/ bin/httpd", "- f", "- h", "${DOC_ROOT}"] ENTRYPOINT ["/ bin/sh", "- c"]

The advantage of using ENTRYPOINT is that it is more convenient than CMD because it can pass commands directly to the container when it starts the container.

Case: dynamically generate a configuration file when the container is started

1. Create a working directory

[root@host1 img3] # mkdir / img3 [root@host1 img3] # cd / img3

2. Prepare a shell script to generate the configuration file

[root@host1 img3] # vim entrypoint. miniser:v2 > / etc/nginx/conf.d/www.conf-e "PORT=8080"-e "entrypoint.shcat"-4

Log in to the container again.

[root@host1] # docker exec-it nginx1 / bin/sh/ # cat / etc/nginx/conf.d/www.conf server {server_name 92c978c97b3c; listen 127.0.0.1 it nginx1 8080; root / data/web/html/;} /

In this way, you can quickly prepare specific configuration files for different environments

12. Instruction 12:USER instruction

Used to specify which user the main process in the container is running as

It can also be used to specify that when the CMD RUN ENTRYPOIND of dockerfile is executed, it runs as that user

You need to make sure that this user is in the / etc/passwd of the container, otherwise an error will be reported.

Syntax format:

USER |

13. Instruction 13:HEALTHCHECK instruction

Check whether the container and the services in the container are working properly

Functional options for HEALTHCHECK

-- interval=xx specifies how often it is checked (by default, every 30 seconds)

-- timeout=xx specifies the wait timeout (default is also 30s)

-- after start-period=xx starts the container, how long will it take to start the health check (default is 0 seconds, that is, no wait)

-- retries=xx specifies that an exception occurs only if the data is not returned after several requests (the default is 3 times)

The return value of HEALTHCHECK

0:success succeeded

1:unhealth is not healthy

2: reservation, meaningless

Case: check every 5 minutes, the timeout is 3 seconds

HEALTHCHECK-- interval=5m-- timeout=3s CMD curl-f http://1.2.3.4 | | exit 1

If the health check fails, return 1

14. Instruction 14:SHELL instruction

This is used to specify the program to be used by default when running the program

For example:

SHELL ["/ bin/sh", "- c"]

15. Instruction 15:STOPSIGNAL instruction

This instruction can specify what instructions are essentially sent to the container when executing docker stop. The default is to send 15, and if you want to execute stop, send 9 instructions.

Format

STOPSIGNAL 9

16. Instruction 16:ARG instruction

ARG also defines a variable, but this variable is used in the execution of build dockerfile

The version of nginx can be defined as a variable, and then the author can be dynamically specified by the variable when building the image.

Write an image file

The configuration file for FROM nginx:1.14-alpineARG info= "zxhk" LABEL author= "${info}" ENV NGX_DOC_ROOT='/data/web/html/'ADD index.html ${NGX_DOC_ROOT} ADD entrypoint.sh / bin/#nginx must end with; CMD ["/ usr/sbin/nginx", "- g", "daemon off;"] ENTRYPOINT ["/ bin/entrypoint.sh"]

Make a mirror image

[root@host1 img3] # docker build- t miniser:v2-5-build-arg new= "tom@qq.com".

The arg in Dockerfile is equivalent to configuring a default value. If no value is passed when making an image, the default value is used.

Pay attention to the distinction between ENV and ARG

You can pass a value in docker run, but not in docker build.

The variables defined by ENV are used when it is sufficient to build an image, while the default value of ARG can only be used when building an image.

17. Instruction 17:ONBUILD instruction

This instruction actually defines a trigger

The Dcokerfile defined by ONBUILD will not be executed when the docker build is executed, but will only be executed when someone else makes a new image based on this image.

Case: if someone makes a new image based on this image, ask them to download a file

Step 1: create a basic image

[root@host1 img3] # vim DockerfileFROM nginx:1.14-alpineLABEL author= "zxhk" ENV NGX_DOC_ROOT='/data/web/html/'ADD index.html ${NGX_DOC_ROOT} ADD entrypoint.sh / bin/ONBUILD ADD http://x.x.x.x/xxx / data/web/html/#nginx configuration file must end with CMD ["/ usr/sbin/nginx", "- g", "daemon off;"] ENTRYPOINT ["/ bin/entrypoint.sh"]

Make a mirror image

[root@host1 img3] # docker build-t base:v1.1. /

Step 2: make a new image based on the basic image

[root@host1 ~] # mkdir / img4 [root@host1 img4] # cd / img4 [root@host1 img4] # vim DockerfileFROM base:v1-1RUN mkdir / data

Make a mirror image

[root@host1 img4] # docker build-t newimg:v1-1. /

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