Docker 容器从缓存启动时丢失所有文件

问题描述 投票:0回答:1

我正在尝试创建一个用于开发 Django 后端的 Dockerfile。但是,当我完成我的 Dockerfile 并尝试构建它时,出现了一个非常奇怪的错误。当我的容器成功构建并自动启动时,我复制到其中的所有文件都丢失了。

#dockerfile
FROM python:3.11.2

WORKDIR /Django

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

ENV WATCHPACK_POLLING=true

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
#CMD ["python"]

Container build log

Deploying 'Django Dockerfile: Dockerfile'…
Building image…
Preparing build context archive…
[==================================================>]140/140 files
Done

Sending build context to Docker daemon…
[==================================================>] 48.40kB
Done

Step 1/8 : FROM python:3.11.2
 ---> df3e9d105d6c
Step 2/8 : WORKDIR /Django
 ---> Using cache
 ---> e496b0316a98
Step 3/8 : COPY requirements.txt .
 ---> Using cache
 ---> e6e1dd8df826
Step 4/8 : RUN pip install -r requirements.txt
 ---> Using cache
 ---> d64b72a3cd11
Step 5/8 : COPY . .
 ---> 0f61d894b1ea
Step 6/8 : ENV WATCHPACK_POLLING=true
 ---> Running in 5587dfc9c5fb
Removing intermediate container 5587dfc9c5fb
 ---> 5eb852bef2ae
Step 7/8 : EXPOSE 8000
 ---> Running in 3534176d6039
Removing intermediate container 3534176d6039
 ---> 9c9ab9c03246
Step 8/8 : CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
 ---> Running in fe97a0eab43e
Removing intermediate container fe97a0eab43e
 ---> 58ed83df5082

Successfully built 58ed83df5082
Existing container found: 485eabfc723df5463d9f3a7e4784e3f43ab118ba58c11c34213603e85948857e, removing…
Creating container…
Container Id: a0184f6cc50e06cbe189c44ae8962ab2cd893f4223c3227b789e6d4e65979446
Container name: 'Django'
Starting container 'Django'

Container log

我尝试将

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
更改为
CMD ["python"]
并使用
ls
检查文件,它是空的。

file check

然后我找到最后一个缓存并用它来生成一个容器,发现这个过程运行得非常好,这是最奇特的部分。

Container from cache

working fine

Watching for file changes with StatReloader
2023-04-20T10:43:50.818914463Z Performing system checks...
2023-04-20T10:43:50.818984773Z 
2023-04-20T10:43:50.905103187Z System check identified no issues (0 silenced).
2023-04-20T10:43:50.954496076Z 
2023-04-20T10:43:50.954545196Z You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
2023-04-20T10:43:50.954636019Z Run 'python manage.py migrate' to apply them.
2023-04-20T10:43:50.965569363Z April 20, 2023 - 10:43:50
2023-04-20T10:43:50.965617213Z Django version 4.2, using settings 'fit5120Django.settings'
2023-04-20T10:43:50.965624273Z Starting development server at http://0.0.0.0:8000/
2023-04-20T10:43:50.965626713Z Quit the server with CONTROL-C.
2023-04-20T10:43:50.965840952Z 

我使用的环境:

Windows 11 Docker for windows v4.17.1 PyCharm 2023.1(专业版)

配置设置

Docker setting

configuration setting

总之,目前我的Dockerfile构建的容器无法正常运行,会提示文件不存在的错误

我不知道这是我的Dockerfile的问题还是Docker的bug,但由于我需要为我的团队提供一个Docker来完成我们的任务,我希望能解决这个问题并使我的团队能够正确地构建容器来自 Dockerfile。

python django docker pycharm docker-for-windows
1个回答
0
投票

问题解决了,是权限问题。

-v
命令需要权限,但是我没有以管理员权限启动PyCharm,导致
-v
命令映射文件失败,导致所有文件丢失

© www.soinside.com 2019 - 2024. All rights reserved.