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

How to use Dockerfile in Docker

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

Share

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

This article introduces how to use Dockerfile in Docker, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

I. basic structure of Dockerfile

Generally speaking, Dockerfile is divided into four parts: basic image information, maintainer information, mirror operation instructions and container startup instructions.' #'is a comment in Dockerfile. Let's take a look at the following small example:

# This my first nginx Dockerfile# Version 1.The basic Base images image FROM centos#MAINTAINER maintainer information MAINTAINER tianfeiyu # ENV setting environment variable ENV PATH / usr/local/nginx/sbin:$PATH#ADD file is placed in the current directory Copying in the past will automatically extract ADD nginx-1.8.0.tar.gz / usr/local/ ADD epel-release-latest-7.noarch.rpm / usr/local/ # RUN and execute the following command RUN rpm-ivh / usr/local/epel-release-latest-7.noarch.rpmRUN yum install-y wget lftp gcc gcc-c++ make openssl-devel pcre-devel pcre & & yum clean allRUN useradd-s / sbin/nologin-M www#WORKDIR is equivalent to cdWORKDIR / usr/local/nginx-1. 8.0 RUN. / configure-prefix=/usr/local/nginx-user=www-group=www-with-http_ssl_module-with-pcre & & make & & make installRUN echo "daemon off "> > / etc/nginx.conf#EXPOSE mapping port EXPOSE 80#CMD run the following command CMD [" nginx "]

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

The format is FROM or FROM:.

The first instruction must be a FROM instruction.

MAINTAINER: specify maintainer information

Format is MAINTAINER

RUN: command to be executed in the mirror

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

The former will run the command, / bin/bash-c, in the shell terminal; the latter will be executed using exec. Specifying the use of other terminals can be done in a second way, such as RUN ["/ bin/bash", "- c", "echo hello"].

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

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. For example

WORKDIR / aWORKDIR bWORKDIR cRUN pwd

The final path is / a/b/c.

EXPOSE: specify the port to be opened by the container

The format is EXPOSE [...]

Tell the port number exposed by the Docker server container for use by the interconnected system. When starting the container, the host will automatically assign a port to be forwarded to the specified port through-P _ ~ ~ Docker.

ENV: defining environment variables

The format is ENV. Specify an environment variable that will be used by subsequent RUN instructions and maintained while the container is running.

For example

ENV PATH / usr/local/nginx/sbin:$PATH

COPY: copy the local host's (the relative path of the directory where the Dockerfile is located) to the container

The format is COPY.

ADD: equivalent to COPY, but more powerful than COPY

Format is ADD

This command copies the specified to the container. It can be a relative path to the directory where Dockerfile resides; it can be a URL; or a tar file that will be automatically decompressed when copied into the container.

VOLUME: Mount directory

The format is VOLUME ["/ data"]

Create a mount point that can be mounted from a local host or other container, which is generally used to store databases, data that needs to be maintained, and so on.

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 user before, for example: RUN useradd-s / sbin/nologin-M www.

ENTRYPOINT

There are two formats:

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

Configure the commands that are executed after the container starts and cannot be overridden by the parameters provided by docker run. There can be only one ENTRYPOINT per Dockerfile, and when more than one is specified, only the last one takes effect.

CMD

Three formats are supported

CMD ["executable", "param1", "param2"] is executed using exec, recommended; CMD command param1 param2 is executed in / bin/bash and provided to applications that require interaction; CMD ["param1", "param2"] provides default parameters to ENTRYPOINT

Specifies the command to be executed when the container is started, and there can be only one CMD command per Dockerfile. If multiple commands are specified, only the last one will be executed. If the user starts the container and specifies the command to run, the command specified by CMD will be overwritten.

ONBUILD: does not take effect when building this image, but takes effect when building an image based on this image

The format is ONBUILD [INSTRUCTION]

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

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 starting the image is like starting an executable program that only needs to specify parameters on CMD; in addition, it is not easy to make mistakes when we need custom CMD.

Dockerfile using CMD:

[root@sta2 test] # cat Dockerfile FROM mysqlCMD ["echo", "test"]

Dockerfile using ENTRYPOINT:

[root@sta2 entrypoint] # cat Dockerfile FROM mysqlENTRYPOINT ["echo", "test"]

Conclusion: ENTRYPOINT cannot override the parameters at execution time, and CMD can override the default parameters.

You can use the following command to override the default parameters to facilitate debugging bug in Dockerfile:

[root@sta2 entrypoint] # docker run-it-entrypoint=/bin/bash feiyu/entrypoint:1

two。 Create a mirror

When building an image, you need to put the package and Dockerfile files you want to use in a directory, like this:

Use the following command to build a mirror image:

# docker build-t feiyu/nginx:1.8.

Docker images are layered, so each command in the creation process is equivalent to one layer:

# docker run-d-p 8080 80-- name mynginx feiyu/nginx:1.8 # launch container

On how to use Dockerfile in Docker to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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