dockerfile common instructions

Posted by dewbie on Sun, 17 Oct 2021 20:07:54 +0200

FROM

Syntax:

 FROM <image>:<tag>

Indicates the base image from which the new image is built. If tag is not selected, the default value is latest.
If it is not based on any image, it is written as: FROM scratch. Official note: scratch image is an empty image, which can be used to build busybox and other ultra-small images. It can be said that it is really to build its own image FROM scratch.

FROM centos:7

LABEL

Syntax:

LABEL <key>=<value> <key>=<value> <key>=<value> ...

The function is to assign a LABEL to the image. You can also use LABEL to specify the mirror author.

LABEL maintainer="xxxx.com"

RUN

Syntax:

RUN <command>

The Shell command that runs when building the image. For example, in the new image, we want to create a java directory in the / usr/local directory.

RUN mkdir -p /usr/local/java

ADD

Syntax:

ADD <src>... <dest>

Copy files or directories to the image. src can be a local file or a local compressed file, and the compressed file will be decompressed automatically. It can also be a url. If src is written as a url, ADD is similar to the wget command, and then it will be downloaded automatically
Press. It's equivalent to doing cp and tar

ADD jdk-11.0.6_linux-x64_bin.tar.gz /usr/local/java

COPY

Syntax:

COPY <src>... <dest>

Copy files or directories to the image. The usage is the same as ADD, but it does not support automatic download and decompression.

COPY jdk-11.0.6_linux-x64_bin.tar.gz /usr/local/java

EXPOSE

Syntax:

EXPOSE <port> [<port>/<protocol>...]

Expose the listening port of the container runtime to the outside. You can specify whether the port listens to TCP or UDP. If no protocol is specified, it will be defaulted
Think TCP.

EXPOSE 80 443 8080/tcp
If you want to have a mapping relationship between the container and the port of the host, you must add it when the container starts -P Parameters.

ENV

Syntax:

# Add single
ENV <key> <value>
#  Add multiple
ENV <key>=<value> ...

Setting environment variables in container

ENV JAVA_HOME /usr/local/java/jdk-11.0.6/

CMD

Syntax:

CMD ["executable","param1","param2"] 
For example: CMD["/usr/local/tomcat/bin/catalina.sh", "run"]
CMD ["param1","param2"] 
For example: CMD [ "echo", "$JAVA_HOME" ]
CMD command param1 param2 
For example: CMD echo $JAVA_HOME

Shell command to execute when starting the container. There can only be one CMD instruction in Dockerfile. If more than one CMD is set, only
The last CMD will take effect.

CMD ehco $JAVA_HOME
If a command is specified when creating a container, the CMD The command is overridden.
If the image is called centos:7 ,When creating a container, the command is: docker run -it --name centos7 centos:7 echo "helloworld" perhaps docker run -it --name centos7 centos:7 /bin/bash 
No output $JAVA_HOME Environment variable information, because CMD Command by echo "helloworld" , /bin/bash Covered.

ENTRYPOINT

Syntax:

ENTRYPOINT ["executable", "param1", "param2"] 
For example: ENTRYPOINT["/usr/local/tomcat/bin/catalina.sh", "run"]

ENTRYPOINT command param1 param2 
For example: ENTRYPOINT ehco $JAVA_HOME

The Shell command executed when starting the container, similar to CMD, will not be overwritten by the parameters specified on the docker run command line. There can only be one entry point instruction in Dockerfile. If multiple entry points are set, only the last entry point will take effect.

ENTRYPOINT ehco $JAVA_HOME

If both ENTRYPOINT and CMD are written in the Dockerfile, and the CMD instruction is not a complete executable
Line command, then the content specified by CMD will be used as the parameter of ENTRYPOINT;

If both entry point and CMD are written in Dockerfile, and CMD is a complete instruction, then
It will cover each other. Who will take effect in the end

WORKDIR

Syntax:

WORKDIR /path/to/workdir

Set the working directory for RUN, CMD, ENTRYPOINT, AND COPY AND and.

WORKDIR /usr/local

VOLUME

Syntax:

VOLUME ["/var/lib/mysql"]

The / var/lib/mysql directory of the container will be automatically mounted as an anonymous volume at runtime. The anonymous volume is in the / var/lib/docker/volumes directory of the host machine

Specify the container mount point to a directory or other container automatically generated by the host. The general usage scenario is when persistent data storage is required.

Generally, it is not used in Dockerfile. It is more common to specify the data volume through - v when docker run s.

Topics: Linux Docker dockerfile