我有这个多阶段的Dockerfile。我在构建映像中创建一个程序,将内容压缩,然后将其复制到主映像中,然后将其解压缩。容器启动后,当我进入容器时,将无法再找到文件。但是,使用“ ls”命令,我可以看到它已被复制并提取。我不知道这是否与应用程序的根目录作为卷有关。我这样做是为了在更改代码后加快构建速度。
docker-compose.yml
version: "3"
services:
web:
build: .
ports:
- "5000:5000"
- "5432:5432"
volumes:
- ".:/code"
environment:
- PORT=5000
# TODO: Should be set to 0 for production
- PYTHONUNBUFFERED=1
Dockerfile
# Build lab-D
FROM gcc:8.2.0 as builder
RUN apt-get update && apt-get install -y libxerces-c-dev
WORKDIR /lab-d/
RUN git clone https://github.com/lab-d/lab-d.git
WORKDIR /lab-d/lab-d/
RUN autoreconf -if
RUN ./configure --enable-silent-rules 'CFLAGS=-g -O0 -w' 'CXXFLAGS=-g -O0 -w' 'LDFLAGS=-g -O0 -w'
RUN make
RUN make install
WORKDIR /lab-d/
RUN ls
RUN tar -czf labd.tar.gz lab-d
# Main Image
FROM library/python:3.7-stretch
RUN apt-get update && apt-get install -y python3 python3-pip \
postgresql-client \
# lab-D requires this library
libxerces-c-dev \
# For VIM
apt-file
RUN apt-file update && apt-get install -y vim
RUN pip install --upgrade pip
COPY requirements.txt /
RUN pip3 install --trusted-host pypi.org -r /requirements.txt
RUN pwd
RUN ls .
COPY --from=builder /lab-d/labd.tar.gz /code/labd.tar.gz
WORKDIR /code
RUN pwd
RUN ls .
RUN tar -xzf labd.tar.gz
RUN ls .
run pwd
RUN ls .
CMD ["bash", "start.sh"]
docker-compose build --no-cache
...
Step 19/29 : RUN pwd
---> Running in a856867bf69a
/
Removing intermediate container a856867bf69a
---> f1ee3dca8500
Step 20/29 : RUN ls .
---> Running in ee8da6874808
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
requirements.txt
root
run
sbin
srv
sys
tmp
usr
var
Removing intermediate container ee8da6874808
---> e8aec80955c9
Step 21/29 : COPY --from=builder /lab-d/labd.tar.gz /code/labd.tar.gz
---> 72d14ab4e01f
Step 22/29 : WORKDIR /code
---> Running in 17873e785c17
Removing intermediate container 17873e785c17
---> 57e8361767ca
Step 23/29 : RUN pwd
---> Running in abafd210abcb
/code
Removing intermediate container abafd210abcb
---> c6f430e1b362
Step 24/29 : RUN ls .
---> Running in 40b9e85261c2
labd.tar.gz
Removing intermediate container 40b9e85261c2
---> f9ee8e04d065
Step 25/29 : RUN tar -xzf labd.tar.gz
---> Running in 6e60ce7e1886
Removing intermediate container 6e60ce7e1886
---> 654d3c791798
Step 26/29 : RUN ls .
---> Running in 0f445b35f399
lab-d
labd.tar.gz
Removing intermediate container 0f445b35f399
---> 7863a15534b1
Step 27/29 : run pwd
---> Running in 9658c6170bde
/code
Removing intermediate container 9658c6170bde
---> 8d8e472a1b95
Step 28/29 : RUN ls .
---> Running in 19da5b77f5b6
lab-d
labd.tar.gz
Removing intermediate container 19da5b77f5b6
---> 140645efadbc
Step 29/29 : CMD ["bash", "start.sh"]
---> Running in 02b006bdf868
Removing intermediate container 02b006bdf868
---> 28d819321035
Successfully built 28d819321035
Successfully tagged -server_web:latest
start.sh
#!/bin/bash
# Start the SQL Proxy (Local-only)
pwd
ls .
./cloud_sql_proxy -instances=api-project-123456789:us-central1:sq=tcp:5432 \
-credential_file=./config/google_service_account.json &
ls .
# Override with CircleCI for other environments
cp .env.development .env
ls .
python3 -u ./server/server.py
答案 0 :(得分:1)
在您的Dockerfile
中,您
COPY --from=builder /lab-d/labd.tar.gz /code/labd.tar.gz
WORKDIR /code
RUN tar -xzf labd.tar.gz
但是您的docker-compose.yml
指定了
volumes:
- ".:/code"
这将导致主机上的当前目录挂载在容器中的/code
上,并且Dockerfile所做的每一项工作都被隐藏。