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 the Dockerfile command correctly

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces you how to correctly use the Dockerfile command, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1 FROM-specify the basic image

The basic image does not exist and will be pulled on Docker Hub.

Use format:

FROM: [tag]

FROM @ digest [check code]

When the current host does not have this image, it will automatically go to the official website HUB to download.

.

2 MAINTANIER-provide personal information provided by the Dockerfile producer

[gradually abandoned]

LABLE-substitute for MAINTANIER

Specific use:

LABLE maintainer= "author Information"

Use format:

MAINTANIER "guowei"

...

3 COPY-copy the files in the host to the mirror!

The file should be in the Dockerfile working directory

Src original file

-- wildcards are supported

-- usually relative paths

Dest destination path

-- usually absolute path

Strings separated by white space characters need to be "", otherwise they will be treated as two files!

File replication guidelines:

1 src must be a path in the context of build, not its parent directory

2 if src is a directory, its internal files or subdirectories will be copied recursively

But the src directory itself will not be copied

3 if more than one src is specified or wildcards are used in the src, the dest must be a

Directory and must end with /

4 if the dest implementation does not exist, it will be created automatically, including its parent directory

...

4 ADD-similar to the COPY command

URL path is supported-if you can access the network, you will access the network for download

Go to the local and pack it into the mirror image!

Operating guidelines:

1 if src is URL and dest does not end with /, the file specified by src will be downloaded and

Directly created as dest; if dest ends with /, the file name is the file specified by URL

Will be downloaded directly and saved as dest/filename

2 if the package is compressed, it will be unzipped, but the tar file obtained through the URL path will not be expanded

3 if there are multiple src, or if they use wildcards indirectly or directly, the dest must be a

A directory path that ends with /, which is treated as a normal file if dest does not end with /

The contents of the src will be written directly to the dest!

...

5 WORKDIR-specify the working directory

Each time will only affect the subsequent instructions of this instruction.

ADD nginx-1.14.2.tar.gz / usr/local/src/-unaffected

WORKDIR / usr/local/src/

ADD nginx-1.14.2.tar.gz. /-- affected

...

6 VOLUME-Volume

Only volumes managed by docker can be defined:

VOLUME / data/mysql

At run time, a volume directory is randomly generated under the host directory!

....

7 EXPOSE opens the specified port for the container to listen to to communicate with the outside world

Use format:

EXPOSE 80/tcp 23/udp

Defaults to tcp without agreement

Use the-P option to expose the port specified here!

But the port associated with this port by the host is random!

...

8 ENV

Used to define the required environment variables for the image and can be located in the Dockerfile file

Other subsequent commands are called

Call format:

$An or ${A}

ENV

ENV =

In the first format, everything after key is treated as part of it.

Therefore, only one variable can be set at a time!

The second format allows you to set multiple variables at a time, each of which is a =

If there are spaces in it, you can escape it with a backslash (\)

It can also be identified by adding quotation marks. In addition, the backslash can also be used for service life.

When defining multiple variables. It is recommended to use the second way to complete all functions in the same layer

Specific usage:

ENV JAVA_HOME / usr/local/jdk

ENV JRE_HOME $JAVA_HOME/jre

ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/

ENV PATH $PATH:$JAVA_HOME/bin/

ENV A / web/html

COPY index.html ${A:-/web/html}

Pass variables in docker run:

Docker run-e [list] transfer variable value

If you assign variables in dockerfile, you can also continue to assign values in docker run.

Docker run-- name b1-- rm-e A=xx [mirror ID]

Will not affect the process of docker build!

Printenv-- output environment variable information

...

9 RUN command:

Use format:

RUN

RUN [",", "]

To run it in the first format

This means that the PID of this process in the container cannot be 1 and cannot receive Unix signals, so when using the docker stop command

To stop the container, this process does not receive a signal

The parameter in the second syntax format is an array in JSON format, where is the command to be run, followed by

For the options or arguments passed to the command, however, the command specified in this format does not run it as "/ bin/sh-c" >

So common shell operations such as variable substitution and wildcard substitution will not take place, but if the command to be run depends on the

For this shell feature, you can replace it with the following format:

RUN ["/ bin/bash", "- c", "", ""]

....

10 CMD command: running in docker run

There are three ways to write grammar.

1. CMD ["executable", "param1", "param2"]-start a process with an ID of 1

Specific examples:

CMD ["/ bin/sh", "- c", "/ bin/httpd", "- f", "- h / web/html]

2. CMD ["param1", "param2"]

3. CMD command param1 param2-A child process that operates directly as shell

Param*= execution parameters

For example, the second kind:

CMD ["nginx"]

Docker run-it-p 8888purl 80 172.20.23.31/server1/nginx-base:v1 nginx

Can only be double quotation marks!

CMD ["param1", "param2"]

-- this usage is used to provide default parameters for ENTRYPOINT instructions

Can be used to execute scripts:

Add a script:

ADD run_tomcat.sh / apps/tomcat/bin/run_tomcat.sh

RUN chmod + x / apps/tomcat/bin/run_tomcat.sh

RUN chown-R tomcat:tomcat / apps / data/tomcat

CMD ["/ apps/tomcat/bin/run_tomcat.sh"]-reference script!

...

11 ENTRYPOINT

A function similar to the CMD directive, which is used to specify the default running program for the container, making the container look like a separate

Executable program of

Unlike CND, programs started by this instruction are not overridden by the parameters specified by the docker run command line

Moreover, these command line arguments are passed as arguments to the program specified by ENTRYPOINT

Use format:

ENTRYPOINT

ENTRYPOINT [",", "]

The command parameters passed in by the docker run command override the contents specified by CMD and are appended to the ENTRYPOINT

Command is finally used as its argument

There can also be more than one of this directive in the Dockerfile file, but only the last one takes effect!

In docker run, commands passed with the-- entrypoint string option can override the Dockerfile

Defined ENTRYPOINT instruction

How to get the Nginx profile to receive parameters

Create a script:

#! / bin/bash

#

Cat > / etc/nginx/conf.d/www.conf

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