0

I need to containerize a Django Web project with docker. I divided the project into dashboard, api-server and database. When I type docker-compose up, it print api-server exited with code 0 and api-server container Exited (0), and I type docker logs api-server, it return empty, but other container normal. I don't know how to check problem.

api-server directory structure is as follows

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

Some compose yml content is as follows

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"

Some Dockerfile content of api-server is as follows

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"]

start.sh is as follows

#!/usr/bin/env bash
cd server/
python manage.py runserver 0.0.0.0:8000

type docker-compose up result as follows

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

docker logs api-server is empty

I would very appreciate it if you guys can tell me how to check this problems, It is better to provide a solution.

moluzhui
  • 1,003
  • 14
  • 34

1 Answers1

1

You are already copying api-server to Dockerfile during build time which should work fine, but in Docker compose it all override all the pip packages and code.

    volumes:
      - /api-server:/webapps

Remove the volume from your Docker compose and it should work.

Second thing set permission to the bash script.

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

Third thing, you do need to run python using bash as there is no thing in the bash that CMD can not perform so why not as a CMD?

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Adiii
  • 54,482
  • 7
  • 145
  • 148