编写以代码0退出的容器,并将其记录为空

时间:2019-09-21 14:24:08

标签: python django python-3.x docker docker-compose

我需要用Docker容器化Django Web项目。我将项目分为仪表板,api服务器和数据库。当我键入DELETE时,它会打印docker-compose up并在api服务器容器中退出(0),并且键入api-server exited with code 0时,它将返回空,但其他容器正常。我不知道如何检查问题。

api-server目录结构如下

docker logs api-server

一些yml内容如下

api-server
    server/
    Dockerfile
    requirements.txt
    start.sh
    ...
    ...

api-server的一些Dockerfile内容如下

dashboard:
    image: nginx:latest
    container_name: nginx-dashboard
    volumes:
      - /nginx/nginx/default:/etc/nginx/conf.d/default.conf:ro
      - /nginx/dist:/var/www/html:ro
    ports:
      - "80:80"
    depends_on:
      - api-server
  api-server:
    build: /api-server
    container_name: api-server
    volumes:
      - /api-server:/webapps
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres
    container_name: Postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    ports:
      - "5432:5432"

start.sh如下

FROM python:3.6
ENV PYTHONUNBUFFERED 1

RUN mkdir /webapps
WORKDIR /webapps

RUN apt-get clean && apt-get update && apt-get upgrade -y && apt-get install -y python3-pip libpq-dev apt-utils
COPY ./requirements.txt /webapps/
RUN pip3 install -r /webapps/requirements.txt

COPY . /webapps/

CMD ["bash","-c","./start.sh"]

键入#!/usr/bin/env bash cd server/ python manage.py runserver 0.0.0.0:8000 的结果如下

docker-compose up

root@VM:/home/test/Documents/ComposeTest# docker-compose up Creating network "composetest_default" with the default driver Creating Postgres ... done Creating api-server ... done Creating dashboard ... done Attaching to Postgres, api-server, dashboard Postgres | The files belonging to this database system will be owned by user "postgres". Postgres | This user must also own the server process. ... ... api-server exited with code 0 api-server exited with code 0 为空

如果您能告诉我如何解决此问题,我将不胜感激,最好提供解决方案。

1 个答案:

答案 0 :(得分:1)

您已经在构建期间将api-server复制到Dockerfile,这应该可以正常工作,但是在Docker中,它会覆盖所有pip包和代码。

    volumes:
      - /api-server:/webapps

从Docker组合中删除该卷,它应该可以工作。

第二件事设置了bash脚本的权限。

COPY . /webapps/
RUN chmod +x ./start.sh

第三件事,您确实需要使用bash运行python,因为bash中没有CMD无法执行的功能,所以为什么不作为CMD?

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