使用第二个docker-compose文件运行先前设置的容器后发生错误

时间:2019-02-08 00:48:24

标签: docker docker-compose dockerfile

我有两个docker-compose.yml文件,一个用于设置容器,另一个用于容器的后续运行:

docker-compose.setup.yml:

version: '3'
services:
  db:
    image: "postgres:11.1"
    env_file:
      - ./volumes/postgres_config/env_file
    networks:
      - db_nw

  pyramid_app:
    image: image_from_dockerfile
    env_file:
      - ./volumes/postgres_config/env_file
    volumes:
      - ./volumes/pyramid_app:/app/src
    image: image_from_dockerfile
    working_dir: /app
    expose:
      - 6543
    command: >
      sh -c "/app/venv/bin/pip install -r /app/src/requirements.pip &&
          /app/venv/bin/pip install -e '/app/src[testing]' &&
          /app/venv/bin/pserve /app/src/development.ini --reload"
    networks:
    - db_nw
    - web_nw
    depends_on:
      - db
  nginx:
    image: nginx:1.13.5
    ports:
    - "6543:80"
    volumes:
      - ./volumes/nginx_config:/etc/nginx/conf.d
    networks:
    - web_nw
    depends_on:
      - pyramid_app

networks:
  db_nw:
    driver: bridge
  web_nw:
    driver: bridge

volumes:
  conf.d:
  src:

docker-compose.yml:

version: '3'
services:
  db:
    image: "postgres:11.1"
    env_file:
      - ./volumes/postgres_config/env_file
    networks:
      - db_nw

  pyramid_app:
    image: image_from_dockerfile
    env_file:
      - ./volumes/postgres_config/env_file
    volumes:
      - ./volumes/pyramid_app:/app/src
    image: image_from_dockerfile
    working_dir: /app
    expose:
      - 6543
    command: /app/venv/bin/pserve /app/src/development.ini --reload
    networks:
    - db_nw
    - web_nw
    depends_on:
      - db
  nginx:
    image: nginx:1.13.5
    ports:
    - "6543:80"
    volumes:
      - ./volumes/nginx_config:/etc/nginx/conf.d
    networks:
    - web_nw
    depends_on:
      - pyramid_app

networks:
  db_nw:
    driver: bridge
  web_nw:
    driver: bridge

volumes:
  conf.d:
  src:

docker-compose.setup.yml运行正常并启动了我的web应用程序,但每次尝试运行后续docker-compose.yml文件时,我都收到“无此类文件或目录”错误:

PS C:\Users\Raj\Projects\github_example> docker-compose up
Starting 81f076500a73_github_example_db_1 ... done
Recreating bc2fafc2039d_github_example_pyramid_app_1 ... error

ERROR: for bc2fafc2039d_github_example_pyramid_app_1  Cannot start service pyramid_app: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"/app/venv/bin/pserve\": stat /app/venv/bin/pserve: no such file or directory": unknown

ERROR: for pyramid_app  Cannot start service pyramid_app: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"/app/venv/bin/pserve\": stat /app/venv/bin/pserve: no such file or directory": unknown
ERROR: Encountered errors while bringing up the project.

另外,这是我的Dockerfile:

FROM ubuntu:18.04
MAINTAINER Raj <raj@fake.email>

ENV PYTHONUNBUFFERED 1

RUN apt-get -yqq update && apt-get install -yqq python3 python3-dev python3-pip python3-venv

RUN mkdir -p /app/venv
RUN python3 -m venv /app/venv
RUN ls /app/venv

RUN /app/venv/bin/pip install --upgrade pip setuptools

WORKDIR /app

1 个答案:

答案 0 :(得分:1)

Docker工具的标准用法是将应用程序安装在Dockerfile中。您不会运行单独的Docker Compose序列来构建应用程序;所有“构建”步骤将按docker build顺序进行。您的Dockerfile可能如下所示:

FROM ubuntu:18.04
ENV PYTHONUNBUFFERED 1

# Install system-level dependencies
RUN apt-get -yqq update \
 && apt-get install -yqq python3 python3-dev python3-pip \
 && pip install --upgrade pip setuptools

# Install Python dependencies (in the Docker-isolated Python)
WORKDIR /app/src
COPY pyramid_app/requirements.pip .
RUN pip install -r requirements.pip

# Install the application proper
COPY pyramid_app/ ./
RUN pip install -e '.[testing]'

# Metadata to say how to run the application
EXPOSE 6543
CMD pserve ./development.ini --reload

现在所有这些设置信息,应用程序源和默认命令都在Docker映像中,您可以直接运行它,而无需任何特殊的运行时设置:

version: '3'
services:
  pyramid_app:
    build: .
    env_file:
      - ./volumes/postgres_config/env_file

我将删除特殊的隔离网络设置,而仅使用Docker Compose为您创建的单个默认网络。我不会试图强迫Docker在主机系统目录中完成所有工作。您不需要networks:volumes:expose:working_dir:来自Dockerfile;并且depends_on:几乎没有用(与此无关,expose:也是如此)。

建议的设置中实际发生的事情是Dockerfile在映像中创建了一个空的Python虚拟环境。第一个docker-compose.yml文件从该映像创建一个容器,在该容器中的虚拟环境 中安装软件,然后丢弃该容器及其已安装的软件;然后第二个docker-compose.yml文件从具有空虚拟环境的映像中启动第二个容器。