Docker Commands and Dockerfile
- Docker Commands and Dockerfile
- Docker commands
- Dockerfile
This paper focuses on the relevant knowledge of Docker commands and Dockerfile.
Docker commands
Website Portal:
- Docker Run Reference
- Docker commands
First of all, of course, the configuration command auto-completion, only need to send a file with curl to download the copy to a specific path, the specific operation reference command-line completion
In fact, Docker has a very complete command help hint, to which instruction is not clear, only need to add in the back --help
to see the help description. For example:
docker --help
the input can see all the executable commands.
- Pick one, such as the
run
command, then input docker run --help
and can see run
the relevant help.
Common commands:
docker images
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]//i.e.imageimage /bin/bash
Some common parameters are:
-a
- Delete all States of container
-a-q)
- Viewing a container that is already running with another TTY
exec${container_id} /bin/bash
- To view information about a container
${container_id}
In addition, in the above instruction, the container name and the container's container_id can be used, if the user does not specify the container name, Docker will default to each container to assign a relatively friendly random name, such as Fervent_perlman,high_galileo and so on
Dockerfile
Website Portal:
- Dockerfile Reference
- Best Practices for writing Dockerfiles
It feels like it's all in the document, and here's a few easy points to be confused about.
1.exec form vs Shell Form
In CMD
and ENTRYPOINT
both involve two forms ( CMD
more of the form of a complete parameter), such as:
CMD ["executable","param1","param2"]
(exec form, recommended)
CMD command param1 param2
(Shell form)
As for the difference between the two forms, the official notes are quite detailed, mainly the variable substitution, scripting environment and other aspects of the difference:
- Note:if cmd is used to provide default arguments for the entrypoint instruction, both the cmd and entrypoint instructions Should is specified with the JSON array format.
- Note:the exec form is parsed as a JSON array, which means, the must use Double-quotes (") around words not single-quo TES (').
- Note:unlike the shell form, the exec form does not invoke a command shell. This means the normal shell processing does not happen. For example, CMD ["echo", " H OM E "]WILLNoTd ovaRIabLesubsTITuTIoNoN HOME. If you want shell processing then either use the shell form or execute a shell directly, for example:cmd ["sh", "-C", "E Cho $HOME "].
2.ENTRYPOINT vs CMD
After reading the official understand how CMD and entrypoint interact
, think that the two are very similar, what is the difference between the two and some confusion, read the following article:
Dockerfile:entrypoint vs CMD
In short, entrypoint is more like an executable command that writes dead, and Cmd is more like a default option.
An image can only be used for a single purpose, just like an executable command, it is recommended to use entrypoint and cmd as the default parameter (the third form CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
). Because entrypoint is generally not overwritten (unless you explicitly use –entrypoit at run), and CMD is the defaults option, the Run command format from the previous text docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Users can override the default cmd by entering their command when running images.
3.ADD vs COPY
These two seem to be copying things from host to Docker's container, the official comparison is as follows:
Although ADD and copy are functionally similar, generally speaking, COPY is preferred. That ' s because it's more transparent than ADD. COPY only supports the basic copying of the local files to the container, while ADD have some features (like Local-only tar E Xtraction and remote URL support) that is not immediately obvious. Consequently, the best to add is the local tar file auto-extraction to the image, as in Add ROOTFS.TAR.XZ/.
In simple terms, the main difference is two points:
- Copy can only copy local files to container, and add also supports remote copy (remotely URL support)
- Add to automatically unzip local compressed files
Official recommendation with copy (preferred)
Reference links
Reference-add or COPY
Stackoverflow-docker COPY vs ADD
Author @brianway More articles: personal website |
CSDN |
Oschina
Docker Commands and Dockerfile