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

Dockerfile detailed explanation

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

Share

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

There are two ways to make Docker image: one is to use Docker container (docker save container_name-o test.tar) to export image, but to use Dockerfile to import all actions into a file and use the build command to make a mirror.

1. Basic structure of Dockerfile

The general Dockerfile is divided into four parts: basic image information, maintainer information, mirror operation instructions and container startup instructions.

FROM nginx:1.13.5-alpine MAINTAINER SXY Team # set time zone RUN echo 'Asia/Shanghai' > / etc/timezone ARG SIBE_VERSION=0 COPY images/files/entrypoint.sh / usr/local/bin/ RUN chmod Usingx / usr/local/bin/entrypoint.sh # Update to get support for Zip/Unzip Bash RUN apk-- update add zip unzip bash wget ENV WWW_TARGET / var/www/html/ COPY images/files/mysite.template / etc/nginx/conf.d/default.conf ADD dist/ / tmp/ RUN mkdir-p ${WWW_TARGET}\ & & mv / tmp/ ${WWW_TARGET}\ & & rm-rf / tmp/ ENV MGMT_API_URL http://192.168.3.223:19980 ENV MGMT_API_AUTHORIZATION d2ViX2FwcDjaGFuZ2VpdAids = RUN cp / var/www/html/ Scripts/http/configuration.json / var/www/html/scripts/http/configuration.json.template RUN cp / var/www/html/scripts/http/configuration.json / var/www/html/scripts/http/configuration.json.template2 # forward request and error logs to docker log collector RUN mkdir-p / var/log/webui & & ln-sf / dev/stdout / var/log/webui/access.log\ & & ln-sf / dev/stderr / var/log/webui/error.log EXPOSE 80 443 ENTRYPOINT [ "/ usr/local/bin/entrypoint.sh"] CMD ["nginx" "- g", "daemon off "] Information in entrypoint [root@test-227 files] # cat entrypoint.sh #! / bin/bash setup () {echo" Configure 1 management api url to ${MGMT_API_URL} "cat / var/www/html/scripts/http/configuration.json.template |\ sed" s # http://192.168.3.223:19980#${MGMT_API_URL}#g" > / var/www/html/scripts/http/configuration.json.template2 cat / var/www/ Html/scripts/http/configuration.json.template2 echo "Configure 2 AUTHORIZATION to ${MGMT_API_AUTHORIZATION}" cat / var/www/html/scripts/http/configuration.json.template2 |\ sed "sroomd2ViX2FwcDpjaGFuZ2VpdAUBG" {MGMT_API_AUTHORIZATION} # g "> / var/www/html/scripts/http/configuration.json cat / var/www/html/scripts/http/configuration.json} setup exec" $@ "

FROM: specify the basic image and the image in which to establish it

FROM or FROM:

The first instruction must be a FROM instruction

MAINTAINER: specify maintainer information

MAINTAINER

RUN: command to be executed in the mirror

RUN or RUN ["executable", "param1", "param2"]

The former runs commands in the shell terminal, namely / bin/bash-c, while the latter executes using exec, specifying that other terminals can be implemented in the second way, such as RUN ["/ bin/bash", "- c", "echo hello"]

WORKDIR: specifies the current working directory, which is equivalent to cd

WORKDIR / path/to/workdir

To configure a working directory for subsequent RUN,CMD,ENTRYPOINT instructions, multiple WORKDIR instructions can be used. Subsequent commands, if the parameter is a relative path, are based on the path specified by the previous command, such as

WORKDIR / a

WORKDIR b

WORKDIR c

RUN pwd

Then the final path / a/b/c

EXPOSE: specify the port on which the container needs to be opened

EXPOSE 80

Tell the docker service container which port to expose, but currently only in the container, if you want to expose at the node, you need-p 80:80, or-P (the host will expose a random port for it)

ENV: defining environment variables

ENV

Specifies that an environment variable is generated in the container

ENV managementapi http://192.168.3.226

COPY: copy the files under the root directory of the local dockerfile path to the container file

COPY

ADD: equivalent to COPY, but more powerful than COPY

ADD

This command also puts files anywhere on the host into the container file, but automatically expands when you can copy the tar file

VOLUME: Mount directory

VOLUME ["/ data"]

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

USER:

USER daemon

Specify the user name or UID when the container is run, and subsequent RUN will also use the specified user. When the service does not require administrator privileges, you can specify the running user through this command, and you can create the desired user RUN useradd-s / sbin/nologin-M www previously.

ENTRYPOINT

There are two formats:

ENTRYPOINT ["executable", "param1", "param2"]

ENTRYPOINT command param1 param2 (shell)

Configure the command to be executed after the container starts, and cannot be overridden by the docker run parameter. There can be only one ENTRYPOINT in each Dockerfile. When more than one is specified, only the last one takes effect.

CMD:

Three formats are supported

CMD ["executable", "param1", "param2"] executes using exec

CMD command1 command2 command3 is executed in / bin/bash and provided to applications that require interaction

Default parameters provided to ENTRYPOINT by CMD ["param1", "param2"]

Specifies the command to be executed when the container is started, and there can be only one CMD command per Dockerfile. If more than one command is specified and only the last one will be executed, the CMD command will be overwritten

The difference between ENTRYPOINT and CMD: ENTRYPOINT specifies the entry when the image starts, while CMD specifies the command when the container starts. When the two are shared, the complete startup command is like ENTRYPOINT+CMD. The advantage of using ENTRYPOINT is that when we start the image, it is like starting an executable program and only need to specify parameters on CMD. In addition, it is not easy to make mistakes when we customize CMD.

Dockerfile using CMD:

FROM mysql

CMD ["echo", "test"]

Dockerfile using ENTRYPOINT

FROM mysql

ENTRYPOINT ["echo", "test"]

ENTRYPOINT cannot override the parameters at execution time. It will take the command of boot image as a parameter, while CMD can override the default parameters.

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