您只需登录到正在运行的docker容器并运行命令。
- 建立你的堆栈:
docker-compose build -f path/to/docker-compose.yml
- 启动堆栈:
docker-compose up -f path/to/docker-compose.yml
- 显示docker正在运行的容器:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fcc49196a84 ex_nginx "nginx -g 'daemon off" 3 days ago Up 32 seconds 0.0.0.0:80->80/tcp, 443/tcp ex_nginx_1
66175bfd6ae6 ex_webapp "/docker-entrypoint.s" 3 days ago Up 32 seconds 0.0.0.0:32768->8000/tcp ex_webapp_1
# postgres docker container ...
- 获取您的django应用程序的容器ID并登录:
docker exec -t -i 66175bfd6ae6 bash
现在您已登录,然后转到正确的文件夹: cd path/to/django_app
现在,每次编辑模型时,都在容器中运行:python manage.py makemigrations
和python manage.py migrate
我还建议您为django docker容器文件使用docker-entrypoint自动运行:
- 碰撞的
- 迁移
- 运行服务器或使用gunicorn或uWSGI启动它
这是一个示例(docker-entrypoint.sh
):
#!/bin/bash
# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput
# Apply database migrations
echo "Apply database migrations"
python manage.py migrate
# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
You just have to log into your running docker container and run your commands.
但是,这样做的原因是什么?@LouisBarranqueiro