【发布时间】:2019-08-01 21:01:52
【问题描述】:
我将 docker 用于带有 ubuntu 服务器的 django 项目。 当我总是改变一些东西时,我会删除我的 docker 图像并重新构建它,我的数据库也被删除了,我必须再次填充我的数据库。
我正在尝试以下步骤: 从 git 拉取新代码
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker-compose up --build -d
正如我提到的,我的数据库也被删除了。
我试过没有停止和删除。
只有docker-compose up --build -d
但它没有奏效。
我也试过
docker-compose restart
它也没有用。
我必须尝试什么。请注意,我是 docker 和 django 的新手。这是我的第一个项目。
docker-compose.yml 文件
version: '3'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
postgres:
container_name: postgres-db
image: postgres:9.6
ports:
- 5432:5432 # Bind host port 5432 to PostgreSQL port 5432
volumes:
- ./pgdb:/var/lib/postgresql/data
env_file: .env
environment:
- LC_ALL=C.UTF-8
web:
container_name: name
build: .
restart: "always"
env_file: .env
environment:
- VIRTUAL_HOST=xxx.xxx.xxx.xxx,localhost
- VIRTUAL_PORT=8000
- TIMEOUT=100
- HTTP_PORT=8000
- STATS_PORT=8001
volumes:
- .:/code
- ./media:/code/media
ports:
- "8000:8000"
links:
- postgres
depends_on:
- "postgres"
networks:
default:
external:
name: nginx-proxy
Dockerfile
FROM python:3.7
# Ensure that Python outputs everything that's printed inside
# the application rather than buffering it.
ENV PYTHONUNBUFFERED 1
ENV APP_ROOT /name
# Copy in your requirements file
ADD req.txt /req.txt
# Install build deps, then run `pip install`, then remove unneeded build deps all in a single step. Correct the path to your production requirements file, if needed.
RUN pip install virtualenvwrapper
RUN python3 -m venv /venv
RUN /venv/bin/pip install -U pip
RUN /venv/bin/pip install --no-cache-dir -r /req.txt
# Copy your application code to the container (make sure you create a .dockerignore file if any large files or directories should be excluded)
RUN mkdir ${APP_ROOT}
RUN mkdir ${APP_ROOT}/static
WORKDIR ${APP_ROOT}
ADD . ${APP_ROOT}
COPY mime.types /etc/mime.types
# uWSGI will listen on this port
EXPOSE 8000
# Call collectstatic (customize the following line with the minimal environment variables needed for manage.py to run):
#RUN if [ -f manage.py ]; then /venv/bin/python manage.py collectstatic --noinput; fi
# Start uWSGI
CMD [ "/venv/bin/uwsgi", "--ini", "/fec/uwsgi.ini"]
【问题讨论】:
-
您可以为您的数据库使用单独的 docker 映像,从而让您的“Django docker”连接到“database docker”。
-
对不起,我没看清楚,能否请您再澄清一下?
标签: python django postgresql docker