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 are the basic ways to use Dockerfile

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the basic uses of Dockerfile". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the basic ways to use Dockerfile"?

The composition of Dockerfile

Dockerfile is divided into four parts

Basic image information

Maintainer information

Mirror operation instruction

Execute instructions when the container is started

Dockerfile instruction description

FROM

The format is FROM or FROM:

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

Example:

FROM centos:6.6

MAINTAINER

Format is MAINTAINER, specifying maintainer information

Example:

MAINTAINER Breeze Yan

RUN

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

The former will run the command / bin/sh-c in the shell terminal; the latter will be executed using exec. Each RUN instruction executes the specified command based on the current mirror and is submitted as a new mirror. When the command is long, you can use\ to wrap. Example:

RUN ["/ bin/bash", "- c", "echo hello"] CMD supports three formats: CMD ["executable", "param1", "param2"] # executes using exec, the recommended way CMD command param1 param2 # executes in / bin/sh, and provides default parameters provided to ENTRYPOINT by CMD ["param1", "param2"] # applications that require interaction.

Specifies the command to be executed when the window starts, and there can be only one CMD command per Dockerfile. If more than one is specified, only the last one will be executed. If the user specifies the command to run when starting the container, the command specified by CMD will be overwritten

Example:

CMD ["supervisord", "- c", "/ etc/supervisord.conf"]

EXPOSE

The format is EXPOSE [...]

Tell the docker container the exposed port for use by the interconnected system. When starting the container, the host will automatically assign a port to forward to the specified port when starting the container. Using-p, you can specify which local port is mapped.

Example:

EXPOSE 22 80

ENV

Format is ENV

Specifies an environment variable that will be used by subsequent RUN instructions and maintained while the container is running

Example:

ENV TZ "Asia/Shanghai" ENV TERM xterm

ADD

Format is ADD

This command copies the specified to the container. It can be a relative path (file or directory) of the directory where dockerfile is located, a URL, or a tar file (automatically extracted to a directory)

Example:

ADD aliyun-mirror.repo / etc/yum.repos.d/CentOS-Base.repoADD aliyun-epel.repo / etc/yum.repos.d/epel.repo

COPY

Format is COPY

Similar to ADD, replicating the local host is in the container, and when the destination path does not exist, it is automatically created. COPY is recommended when using the local directory as the source directory

ENTRYPOINT

Format: ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT command param1 param2 (executed in shell)

Similar to CMD, configure the commands that the container executes after startup, but are not overridden by the parameters provided by docker run. There can be only one ENTRYPOINT per dockerfile. If more than one is specified, only the last one is executed.

VOLUME

The format is VOLUME ["/ data"]

Create a mount point, similar to the-v option used when the container starts, except that the location of the mount to the host cannot be specified. The default is / var/lib/docker/vfs directory. It is generally used to store databases and data that need to be maintained.

USER

Format is 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 users before. You can use gosu when you want to obtain administrator privileges temporarily.

WORKDIR

Format is WORKDIR / path/to/workdir

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

Example:

# the final path is / a/b/cWORKDIR / aWORKDIR bWORKDIR c

ONBUILD

The format is ONBUILD [INSTRUTION]

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

For example, dockerfile creates an image A using the following:

... ONBUILD ADD. / app/srcONBUILD RUN / usr/local/bin/python-build-- dir / app/src...

If you create a new image based on image A, when the new dockerfile uses FROM A to specify the underlying image, the content of the ONBUILD instruction is automatically executed, which is equivalent to adding two instructions later.

FROM A

ADD. / app/srcRUN / usr/local/bin/python-build-- dir / app/src

Use the image of the ONBUILD instruction, which is recommended to be indicated in the label, such as: ruby:1.9-onbuild

Use Dockerfile to generate an image

# mysoft/centos:6.6 is the label of the newly generated image, and / tmp/dockerfile/centos6 is the path where dockerfile resides.

Docker build-t mysoft/centos:6.6 / tmp/dockerfile/centos6 so far, I believe you have a deeper understanding of "what are the basic ways to use Dockerfile". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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