代码之家  ›  专栏  ›  技术社区  ›  user994165

文件复制到Docker图像

  •  0
  • user994165  · 技术社区  · 6 年前

    我有一个多级文件。我在构建映像中创建一个程序,对内容加焦油,在主映像中复制它,然后解压它。一旦容器启动,当我进入容器时,我就再也找不到文件了。然而,使用“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"]
    

    ...
    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
    

    开始.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
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   David Maze    6 年前

    在你的 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

    推荐文章