如何在使用apache2运行Django的docker容器中运行python manage.py migrate



我在带有apache2Docker容器中运行Django应用程序,我需要在Dockerfiledocker-compose中添加命令python manage.py migrate,但无法运行它。

Dockerfile

FROM ubuntu
RUN apt-get update
# Avoid tzdata infinite waiting bug
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Africa/Cairo
RUN apt-get install -y apt-utils vim curl apache2 apache2-utils
RUN apt-get -y install python3 libapache2-mod-wsgi-py3
RUN ln /usr/bin/python3 /usr/bin/python
RUN apt-get -y install python3-pip
#Add sf to avoid ln: failed to create hard link '/usr/bin/pip': File exists
RUN ln -sf /usr/bin/pip3 /usr/bin/pip
RUN pip install --upgrade pip
RUN pip install django ptvsd
COPY www/demo_app/water_maps/requirements.txt requirements.txt
RUN pip install -r requirements.txt
ADD ./demo_site.conf /etc/apache2/sites-available/000-default.conf
EXPOSE 80
WORKDIR /var/www/html/demo_app
CMD ["apache2ctl", "-D", "FOREGROUND"]
CMD ["python", "manage.py", "migrate", "--no-input"]

docker compose

version: "2"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=database_innvoentiq
- POSTGRES_USER=database_user_innvoentiq
- POSTGRES_PASSWORD=mypasswordhere
- PGDATA=/tmp
django-apache2:
build: .
container_name: water_maps
environment:
- POSTGRES_DB=database_innvoentiq
- POSTGRES_USER=database_user_innvoentiq
- POSTGRES_PASSWORD=mypasswordhere
- PGDATA=/tmp
ports:
- '80:80'
volumes:
- ./www/:/var/www/html
depends_on:
- db

这里发生的情况是,在Dockerfile 中运行最后一个CMD之后,容器就存在了

这样做:

django-apache2:
build: .
container_name: water_maps
environment:
- POSTGRES_DB=database_innvoentiq
- POSTGRES_USER=database_user_innvoentiq
- POSTGRES_PASSWORD=mypasswordhere
- PGDATA=/tmp
ports:
- '80:80'
volumes:
- ./www/:/var/www/html
command: >
sh -c 'python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000'
depends_on:
- db

或者使用以下命令运行docker compose:

docker-compose run --rm projectname sh -c "python manage.py filename"

相关内容

  • 没有找到相关文章

最新更新