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 WORKDIR instruction in Linux

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you "how to use WORKDIR instructions in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use WORKDIR instructions in Linux".

The WORKDIR directive is used to declare the current working directory, and then the current directory of each layer is changed to the specified directory. The format is WORKDIR.

If the directory does not exist, WORKDIR will help you set up the directory. Again! Don't think that writing Dockerfiel is writing shell scripts.

Here is an example of an error:

RUN cd / app

RUN echo "hello" > world.txt

If you run the Dockerfile as a build image, you will find that the / app/world.txt file cannot be found, or that its content is not hello.

The reason is very simple: the execution environments of these two lines of RUN commands are fundamentally different, and they are two completely different containers. This is the error caused by a lack of understanding of Dockerfile's concept of building tiered storage.

As mentioned earlier, each RUN starts a container, executes commands, and then submits changes to the storage layer files.

The two lines of RUN build and start their own brand new containers.

So if you need to change the location of the working directory for future layers, you should use the WORKDIR directive.

FROM alpine

WORKDIR / a/b

RUN touch a_b_f.txt

WORKDIR / a

RUN touch a_f.txt

[root@localhost workdir] # docker run-it alpine:workdir / bin/sh

/ a # ls

A_f.txt b

/ a # cd b

/ a/b # ls

A_b_f.txt

If you leave a message at the bottom of the article, you can listen to the course and receive all the video tutorials such as Qianfeng HTML5, UI interaction design, PHP, Java+ cloud data, big data development, VR/AR/Unity game development, Python artificial intelligence, Linux cloud computing, full stack software testing, network security and so on.

COPY instruction

Format:

COPY...

COPY [",..."]

Like RUN instructions, there are two formats, one similar to the command line and one similar to function calls.

It can be an absolute path within the container or a relative path relative to the working directory specified by WORKDIR. The destination path does not need to be created beforehand, and if the directory does not exist, it will be created before copying the file.

The COPY directive will copy the file or directory of the source path from the context directory of the build to a location within the image of the new layer. For example:

COPY qf.json / usr/src/app/

Note that the following is wrong

COPY qf.json / usr/src/app

This will copy the qf.json to the app file in the / usr/src/ directory

It can be multiple and wildcards are supported, such as:

COPY qf* / app/

COPY q?.txt / app/

Using the COPY directive, all kinds of metadata for the source file are retained.

Such as read, write, execution permissions, file change time, and so on.

ADD instruction

The format and nature of the ADD instruction and COPY are basically the same. But some functions have been added to COPY.

Support for automatic decompression, compression format support: gzip, bzip2 and xz

It is officially recommended to use COPY to copy files.

The ADD assignment invalidates the cache when building the image, resulting in slow construction of the image.

According to the principles selected in the COPY and ADD directives, all file copies use the COPY directive and use ADD only where automatic decompression is required.

ADD qf.tar.gz /

USER instruction

USER is to change the identity of executing commands such as RUN, CMD, and ENTRYPOINT.

This user must exist (established) in the container beforehand, otherwise it cannot be switched.

If you want to change your identity during execution as a script executed by root, for example, you want to run a service process as an established user instead of using su or sudo, these require cumbersome configuration and often make errors in an environment where TTY is missing. Gosu is recommended.

The above is all the contents of the article "how to use WORKDIR instructions in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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