Next, this time we will introduce COPY, ADD, CMD, ENTRYPOINT, ENV, WORKDIR.
COPY Used when passing files from the host to the container.
How to write
COPY <File name to copy> <Copy destination directory in the container>
This is a simple example, but it is a description when creating a directory on ubuntu OS and copying files.
Dockerfile
FROM ubuntu:latest
RUN mkdir /test_dir
COPY hoge.txt /test_dir
Another command to add files to the container is ADD
.
ADD
It is a command that can copy files and folders to a container, but there is a decisive difference from COPY
. ** You can copy + decompress the compressed tar file **.
If a file is heavy or a large file to be transferred in the folder hierarchy is passed to the container as it is, it will take time just to pass it to the container. In such a case, if you write it in ADD
when you compress it with tar and copy it to the container, the transmission itself will be quick and it will be expanded in the decompressed state.
How to write
ADD <Compressed file name to copy> <Copy destination directory in the container>
The only difference from COPY is whether the file is compressed, so I will omit the example.
ENTRYPOINT
Like CMD
, specify the last command to be executed during docker run.
CMD
can overwrite a command during docker run, but ENTRYPOINT
cannot overwrite the last command.
As for the description method, CMD
specifies the argument in succession like ["command "(," argument 1 "," argument 2 ")], but ENTRYPOINT
specifies only the command, and the argument is Specify using CMD
.
** The command cannot be changed for ENTRYPOINT
, but the argument part specified by CMD
can be overwritten. ** **
How to write in the case of CMD
FROM ubuntu:latest
CMD ["command", "Argument 1", "Argument 2"]
How to describe in the case of ENTRY POINT
FROM ubuntu:latest
ENTRYPOINT ["command"]
CMD ["Argument 1", "Argument 2"]
Basically, I think that CMD
is enough.
ENV You can set environment variables. There are various ways to describe it. It is also possible to write multiple items side by side as shown in the third example below.
How to write
ENV key1 val
ENV key2=val
ENV key3=val3 key3=val3
When writing multiple items at once and there is a half-width space in the environment variable value (val above), it can be set by enclosing it in double quotation marks or escaping it.
How to describe multiple ENV settings in one line
ENV key4="v a l 4" key5=v\ a\ l\ 5
If there is only one line, you can write as follows.
How to describe environment variable settings mixed with spaces in one ENV
ENV key6 v a l 6
Now you can do things like pass the path inside the container.
WORKDIR
You can change the directory where the command executed by RUN
described in the Dockerfile is executed.
How to write
WORKDIR absolute path
If it is cd
, it will return to the root every time RUN
is done, so if you want to fix the working directory and execute RUN
, use WORKDIR
to move the directory. ..
This command concludes the introduction of basic Docker instructions. By combining with the commands introduced last time, I think that you can cover the commands that you use frequently. Docker is convenient, so please try it.
Recommended Posts