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

Introduction to the image file Dockerfile of Docker container

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

Share

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

This article gives you a detailed introduction to the image file Dockerfile of the Docker container. Most of the knowledge points are often used by everyone, so I will share them for you as a reference. Let's follow the editor and have a look.

Dockerfile directive options:

1 、 FROM

Usage: FROM

1) from specifies the basic source image to build the image. If this image is not specified locally, it will be automatically pull from the docker public repository.

2) from must be the first command on a non-comment line in dockerfile, that is, writing a dockerfile must start with a from statement (except comments)

3) from can appear multiple times in a dockerfile. If there is a need to create multiple images in a dockerfile

4) if the from statement does not specify the label of the mirror, the label of latest is used by default

2 、 MAINTAINER

Usage: MAINTAINER

Specify the user who created the mirror

3 、 RUN

RUN: two ways to use

1) RUN

2) RUN "executable", "param1", "param2"

Each RUN instruction executes the command to create an image and submits it as a new image. The subsequent RUN is based on the image submitted by the previous RUN. The image is hierarchical and can be created through any historical submission point of an award, similar to the version control of far away.

The exec mode is parsed into an JSON array, so you must use double boot instead of single boot. The exec mode does not call a command shell, so it does not integrate the corresponding variables.

For example, RUN ["echo", "$HOME"] this way will not achieve the output HOME variable

The correct way should be: RUN ["sh", "- c", "echo", "$HOME"]

The cache generated by RUN will not fail when it is built and will be reused. You can use the-- no-cache option, that is, docker build-- no-cache, so that it will not be cached.

4 、 CMD

There are three ways to use CMD:

1) CMD "executable", "param1", "param2"

2) CMD "param1", "param2"

3) CMD "commamd", "param1", "param2"

CMD is the command executed when the container starts. If the user specifies the command to run when the container is started, the command specified by CMD will be overwritten.

Note: the CMD instruction can only be used once in dockerfile. If there are more than one, only the last one will take effect.

The purpose of CMD is to provide a default command execution option when starting the container. If the user specifies the command to run when starting the container, the command specified by CMD will be overwritten

CMD is executed when the container is started, but not when build is executed, while RUN is only executed when it is built. Starting the container has nothing to do with RUN after the subsequent image construction is completed.

5 、 EXPOSE

Usage: EXPOSE [...]

Tell the docker server the local port to which the container is mapped, and use the-p or-P option to take effect when docker run is required.

6 、 ENV

ENV # only one variable can be set in this way

ENV =. # this allows you to write multiple variables

Example:

ENV myname= "john" myDOg=rex\ the\ dog\

Mycat=fluffy

Equivalent to

ENV myName John

ENV myDog Rex The Dog

ENV myCat fluffy

7 、 ADD

Usage: ADD

ADD copies the local host file, directory, or remote file URLS to the path specified by the container.

Support regular fuzzy matching through Go

Example:

ADD hom* / mydir # all files starting with hom are copied to / mydir in the container

ADD hom?.txt / mydir # all files that begin with hom and end with txt are copied to / mydir in the container

8 、 COPY

Usage: COPY

Copy copies the new file to the container and adds it to the path specified by the container, using the same as ADD, except that the remote configuration file URLS cannot be specified

9 、 ENTRYPOINT

Usage:

ENTRYPOINT "executable", "param1", "param2"

ENTRYPOINT command param1 param2 (shell form)

Configure the commands that are executed after the container starts, and cannot be overridden by the parameters provided by docker run, while CMD can be overridden. If you overwrite, you can use the docker run-- entrypoint option

There can be only one ENTRYPOINT in each dockerfile. When more than one ENTRYPOINT is specified, only the last one takes effect.

Exec form ENTRYPOINT example:

Use exec form to set stable default commands and options through ENTRYPOINT, and use CMD to add options that are often changed beyond the default.

FROM ubuntu

ENTRYPOINT ["top", "- b"]

CMD ["- c"]

Using ENTRYPOINT to show the foreground running Apache service through Dockerfile

FROM debian:stable

RUN yum update & & yum install httpd-y

EXPOSE 80 443

VOLUME ["/ var/www/html", "/ var/log/apache2", "/ etc/apache2"]

ENTRYPOINT ["/ usr/sbin/apache2ctl", "- D", "FOREGROUND"]

Shell form ENTRYPOINT example:

This method is executed in / bin/sh-c, ignoring any CMD or docker run command options, to ensure that docker stop can stop containers that run ENTRYPOINT for a long time, and to ensure that the execution time uses the exec option

10 、 VOLUME

Usage: VOLUME ["/ data"]

Create a mount point that can be accessed from the local host or other container

11 、 USER

Which user to use to run container

Example:

ENTRYPOINT ["memcached"]

USER daemon

12 、 WORKDIR

Equivalent to CD command

13 、 ONBUILD

The command of the ONBUILD instruction is not executed when the mirror is built, but is executed in his child mirror.

After reading the above, do you have any further understanding of the image file Dockerfile of the Docker container? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

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