Using Docker run options to override settings in the Dockerfile

Using Docker run options to override settings in the Dockerfile

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

Then when starting a container, you can change some parameters in the image file, and these parameters in the image file are often defined by the Dockerfile file.

But not all definitions in the Dockerfile file can be redefined when starting the container. The Dockerfile instructions that cannot be overwritten by docker run are as follows:

  • FROM
  • MAINTAINER
  • RUN
  • ADD
  • COPY

1. Overwrite the ENTRYPOINT instruction

The ENTRYPOINT instruction in the Dockerfile file is used to give the default entry point after the container is started.
The ENTRYPOINT instruction gives the default behavior after the container is started. It is generally difficult to override when starting the container, but command parameters can be appended. Here is an example:

  • docker run --entrypoint /bin/bash ... , gives the subsequent command parameters of the container entry
  • docker run --entrypoint="/bin/bash ..." ... , gives a new shell to the container
  • docker run -it --entrypoint="" mysql bash , reset the container entry

2. Overwrite CMD command

The CMD instruction in the Dockerfile file gives the default instructions to be executed after the container is started.

When starting the container, you can set new command options for docker run to overwrite the CMD instruction in the Dockerfile file (the CMD instruction in the Dockerfile file will no longer be consulted). Here is an example:

  • docker run ... <New_Command> , you can give a different command to override the default instructions in the Dockerfile

If the ENTRYPOINT instruction is also declared in the Dockerfile file, the above instructions will be appended to the ENTRYPOINT instruction as parameters.

3. Override the EXPOSE instruction

The EXPOSE instruction in the Dockerfile file is used to reserve ports for the host where the container is located.

Apparently this is a feature of the runtime container, so docker run can conveniently override that instruction. Here is an example:

  • 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 another container
  • docker run -P , open all ports

4. Override ENV directive

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

When starting a container, the following environment variables are automatically set for the container:

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

docker run can conveniently override this instruction. Here is an example:

docker run -e "key=value" ... , set a 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 OLDPWD
declare -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"

Setting or overriding environment variables via scripts

5. Override the VOLUME instruction

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

  • docker run -v ...
  • docker run -volumes-from ...

6. Overwrite USER command

The default user inside the container is root (uid=0).
In the Dockerfile file, you can specify other users as the default user of the container through USER.

  • docker run -u="" ...
  • docker run --user="" ...

Docker run supports the following forms of -u:

  • user
  • user:group
  • uid
  • uid:gid
  • user:gid
  • uid:group

7. Override the WORKDIR directive

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

If the path does not exist, it is created even if it is not used in subsequent instructions.

In one, multiple WORKDIRs can exist. For relative paths, subsequent instructions inherit from the previous instruction.

In WORKDIR, you can reference previously defined environment variables.

  • docker run -w="" ...
  • docker run --workdir="" ...

Reference Links:

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

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

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Execute multiple commands after docker run
  • Ubuntu vps installs docker and reports an error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Problem solved
  • Detailed explanation of how to use the Docker run command
  • Docker Cannot connect to the Docker daemon. Is the docker daemon running on this host error solution
  • How to view the docker run startup parameter command (recommended)

<<:  Implementing countdown effect with javascript

>>:  JavaScript to achieve drop-down menu effect

Recommend

Overview of MySQL Statistics

MySQL executes SQL through the process of SQL par...

Take you to understand MySQL character set settings in 5 minutes

Table of contents 1. Content Overview 2. Concepts...

Use CSS to create 3D photo wall effect

Use CSS to create a 3D photo wall. The specific c...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

WeChat applet learning wxs usage tutorial

What is wxs? wxs (WeiXin Script) is a scripting l...

Centos7.3 How to install and deploy Nginx and configure https

Installation Environment 1. gcc installation To i...

Implementation of React star rating component

The requirement is to pass in the rating data for...

Vue.js cloud storage realizes image upload function

Preface Tip: The following is the main content of...

Docker connection mongodb implementation process and code examples

After the container is started Log in to admin fi...

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...