Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
unsignedcpp
docker_practice
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
docker_practice
/
image
/
dockerfile
/
workdir.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
36 lines (23 loc) · 1.75 KB
master
Breadcrumbs
docker_practice
/
image
/
dockerfile
/
workdir.md
Copy path
Top
File metadata and controls
Code
Blame
36 lines (23 loc) · 1.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
WORKDIR
指定工作目录
格式为
`
WORKDIR
<
工作目录路径
>
`。
使用 `
WORKDIR
` 指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指定的目录,如该目录不存在,`
WORKDIR
` 会帮你建立目录。
之前提到一些初学者常犯的错误是把 `
Dockerfile
` 等同于 Shell 脚本来书写,这种错误的理解还可能会导致出现下面这样的错误:
`
`
`docker
RUN cd /app
RUN echo "hello" > world.txt
`
`
`
如果将这个 `
Dockerfile
` 进行构建镜像运行后,会发现找不到 `
/
app
/
world.
txt
` 文件,或者其内容不是 `
hello
`。原因其实很简单,在 Shell 中,连续两行是同一个进程执行环境,因此前一个命令修改的内存状态,会直接影响后一个命令;而在 `
Dockerfile
` 中,这两行 `
RUN
` 命令的执行环境根本不同,是两个完全不同的容器。这就是对 `
Dockerfile
` 构建分层存储的概念不了解所导致的错误。
之前说过每一个 `
RUN
` 都是启动一个容器、执行命令、然后提交存储层文件变更。第一层 `
RUN
cd
/
app
` 的执行仅仅是当前进程的工作目录变更,一个内存上的变化而已,其结果不会造成任何文件变更。而到第二层的时候,启动的是一个全新的容器,跟第一层的容器更完全没关系,自然不可能继承前一层构建过程中的内存变化。
因此如果需要改变以后各层的工作目录的位置,那么应该使用 `
WORKDIR
` 指令。
`
`
`docker
WORKDIR /app
RUN echo "hello" > world.txt
`
`
`
如果你的 `
WORKDIR
` 指令使用的相对路径,那么所切换的路径与之前的 `
WORKDIR
` 有关:
`
`
`docker
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
`
``
`RUN pwd`
的工作目录为
`/a/b/c`
。
You can’t perform that action at this time.