我有以下文件结构
Django/
- IoT.yml
- Dockerfile_Build_Django
- requirements.txt
我用于萌芽图像的dockerfile(Dockerfile_Build_Django)如下:
FROM python:3.10.0a7-alpine3.13
ENV PYTHONUNBUFFERED 1
RUN mkdir /code/
WORKDIR /usr/src/app
COPY . .
RUN pip install -r requirements.txt
我的 docker-compose 文件如下:
Django_2.2:
build:
context: ./
dockerfile: Dockerfile_Build_Django
# Give an image name/tag
image: python:3.10.0a7-alpine3.13
container_name: Django_2.2
depends_on:
- Django_Mongo_4.2.12
tty: true
在“docker-compose -f IoT.yml up”设置和运行容器之后,我使用“docker exec -it Django_2.2 /bin/sh”来SSH访问Django_2.2容器,我发现:< /p>
[root@Mysite Django]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.10.0a7-alpine3.13 bba91cdea5a1 3 days ago 44.9MB
django_2.2 iot 37c35b93c3d8 6 days ago 86.3MB
[root@MySite Django]# docker history python:3.10.0a7-alpine3.13
IMAGE CREATED CREATED BY SIZE COMMENT
bba91cdea5a1 3 days ago /bin/sh -c #(nop) CMD ["python3"] 0B
<missing> 3 days ago /bin/sh -c set -ex; wget -O get-pip.py "$P… 8.01MB
<missing> 3 days ago /bin/sh -c #(nop) ENV PYTHON_GET_PIP_SHA256… 0B
<missing> 3 days ago /bin/sh -c #(nop) ENV PYTHON_GET_PIP_URL=ht… 0B
<missing> 3 days ago /bin/sh -c #(nop) ENV PYTHON_PIP_VERSION=21… 0B
<missing> 3 days ago /bin/sh -c cd /usr/local/bin && ln -s idle3… 32B
<missing> 3 days ago /bin/sh -c set -ex && apk add --no-cache --… 29.5MB
<missing> 3 days ago /bin/sh -c #(nop) ENV PYTHON_VERSION=3.10.0… 0B
<missing> 10 days ago /bin/sh -c #(nop) ENV GPG_KEY=A035C8C19219B… 0B
<missing> 10 days ago /bin/sh -c set -eux; apk add --no-cache c… 1.76MB
<missing> 10 days ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0B
<missing> 11 days ago /bin/sh -c #(nop) ENV PATH=/usr/local/bin:/… 0B
<missing> 11 days ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 11 days ago /bin/sh -c #(nop) ADD file:7119167b56ff1228b… 5.61MB
[root@MySite Django]# docker-compose -f IoT.yml build
Django_Mongo_4.2.12 uses an image, skipping
Building Django_2.2
Sending build context to Docker daemon 2.832MB
Step 1/7 : FROM python:3.10.0a7-alpine3.13
---> bba91cdea5a1
Step 2/7 : ENV PYTHONUNBUFFERED 0
---> Running in 4f04eff80f44
Removing intermediate container 4f04eff80f44
---> 894fa6105e9b
Step 3/7 : RUN mkdir /code/
---> Running in 41a547193037
Removing intermediate container 41a547193037
---> 08d8a6605228
Step 4/7 : WORKDIR /usr/src/app
---> Running in 1d33ed47543f
Removing intermediate container 1d33ed47543f
---> f4f22bc9b3bb
Step 5/7 : COPY . .
---> 317348f86075
Step 6/7 : RUN pip install --upgrade pip
---> Running in e43752bea2dd
Requirement already satisfied: pip in /usr/local/lib/python3.10/site-packages (21.0.1)
Removing intermediate container e43752bea2dd
---> 1ce0ff9deb41
Step 7/7 : RUN pip install -r requirements.txt
---> Running in 87fb8a9db240
Collecting Django==2.2.17
Downloading Django-2.2.17-py3-none-any.whl (7.5 MB)
Collecting pytz
Downloading pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.4.1-py3-none-any.whl (42 kB)
Installing collected packages: sqlparse, pytz, Django
Successfully installed Django-2.2.17 pytz-2021.1 sqlparse-0.4.1
Removing intermediate container 87fb8a9db240
---> 8471de36ee4d
Successfully built 8471de36ee4d
Successfully tagged python:3.10.0a7-alpine3.13
[root@MySite Django]#
[root@MySite Django]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.10.0a7-alpine3.13 8471de36ee4d 8 minutes ago 86.5MB
python <none> bba91cdea5a1 3 days ago 44.9MB
django_2.2 iot 37c35b93c3d8 6 days ago 86.3MB
我是否遗漏了 Docker 文档中的一些步骤?
答案 0 :(得分:0)
删除我之前构建的镜像,并再次运行 docker-compose,它现在构建一个新镜像并执行 dockerfile 中的所有 RUN / COPY / CMD 命令。一切都完美无缺。 基于上述行为,似乎 docker-compose 文件中的“image:”行要求 docker 守护进程检查本地是否存在此图像。如果是,则直接使用它,无需构建新的。当我手动删除该图像时,docker daemon 找不到该图像,因此构建了一个新图像。 根据 Docker 官方指南,https://docs.docker.com/compose/compose-file/compose-file-v3/#build, “图像:”行仅用于重命名最终图像,永远不要说它会检查图像是否已经存在,并可能影响图像构建。官方指南在这方面并不准确和准确。