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

Use the options of Docker run to override the setting details in Dockerfile

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

Share

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

Typically, we first define the Dockerfile file, and then build the image file through the docker build command. Then, you can start an instance of the container through docker run based on the image file.

Then when you start a container, you can change some parameters in the image file, which are often defined through the Dockerfile file.

However, not all definitions in the Dockerfile file can be redefined when the container is started. The Dockerfile file directives that docker run cannot overwrite are as follows:

FROMMAINTAINERRUNADDCOPY

1. Override the ENTRYPOINT instruction

The ENTRYPOINT instruction in the Dockerfile file is used to give the default entry after the container is started.

The ENTRYPOINT directive gives the default behavior of the container after startup, which is generally difficult to override when the container is started, but command parameters can be appended. Examples are as follows:

Docker run-- entrypoint / bin/bash..., give the subsequent command parameter docker run-- entrypoint= "/ bin/bash..." of the container entry, give the new Shelldocker run-it-- entrypoint= "" mysql bash of the container, and reset the container entry.

2. Override the CMD instruction

The CMD instruction in the Dockerfile file, which gives the instructions that are executed by default after the container is started.

When you start the container, you can set new command options for docker run to override the CMD directive in the Dockerfile file (no longer consult the CMD directive in the Dockerfile file). Examples are as follows:

Docker run, you can give other commands to override the default directives in the Dockerfile file.

If the ENTRYPOINT directive is also declared in the Dockerfile file, all of the above directives are appended to the ENTRYPOINT directive as parameters.

3. Override the EXPOSE instruction

The EXPOSE directive in the Dockerfile file to reserve the port to the host where the container is located.

This is obviously a feature of the runtime container, so docker run can easily override this directive. Examples are as follows:

Docker run-- expose= "port_number:port_number" docker run-p port_number:port_number/tcp, open the specified range of ports docker run-- link= "another_container_id", link to other containers docker run-P, open all ports

4. Override the ENV instruction

The ENV directive in the Dockerfile file is used to set the environment variables in the container.

When you start the container, automatically set the following environment variables for the container:

HOME, set the user's home directory HOSTNAME based on USER, the hostname of the default container PATH, default: / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binTERM, default xterm, if the container is assigned a pseudo TTY

Docker run can easily override this instruction. Examples are as follows:

Docker run-e "key=value"..., set the new environment variable key

Docker run-h..., overwrite HOSTNAME

Docker run ubuntu / bin/bash-c export

Declare-x HOME= "/" declare-x HOSTNAME= "85bc26a0e200" declare-x OLDPWDdeclare-x PATH= "/ usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" declare-x PWD= "/" declare-x SHLVL= "1" declare-x deep= "purple"

Set or override environment variables through scripts

5. Override the VOLUME instruction

The VOLUME instruction in the Dockerfile file, which is used to set the data volumes for the container.

Docker run-v...

Docker run-volumes-from...

6. Override the USER instruction

The default user inside the container is root (uid=0).

Other users can be specified as default users of the container through USER in the Dockerfile file.

Docker run-u = ""...

Docker run-- user= "...

Docker run supports-u in the following form:

Useruser:groupuiduid:giduser:giduid:group

7. Override the WORKDIR instruction

The WORKDIR instruction in the Dockerfile file, which is used to set the working directory for subsequent instructions.

If the set path does not exist, the path is created and is not used at all in subsequent instructions.

In one, there can be multiple WORKDIR. For relative paths, subsequent instructions inherit previous instructions.

In WORKDIR, you can refer to previously defined environment variables.

Docker run-w = ""...

Docker run-- workdir= "...

Reference link:

Https://docs.docker.com/engine/reference/run/

Https://docs.docker.com/engine/reference/builder/

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

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