【问题标题】:updating website without deleting database在不删除数据库的情况下更新网站
【发布时间】: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


【解决方案1】:

Docker compose 有自己的命令来处理docker-compose.yaml 中使用的图像。不用docker rm $(docker ps -aq)

试试

docker-compose --help

我怀疑只有您的web 服务正在改变。所以这应该足以重建和启动web 服务:

docker-compose stop web
docker-compose rm web
docker-compose up --no-deps --build -d

【讨论】:

  • 我试过你的指南。首先我得到这个错误PermissionError: [Errno 13] Permission denied: '/path/pgdb' [20149] Failed to execute script docker-compose。 pgdb 是使用 root 用户创建的
  • 你在哪里执行这些命令?它们必须在 docker-compose.yml 所在目录中的host 上执行。你的操作系统是什么?
  • 是的,我在 docker-compose.yml 所在的目录中执行它们。操作系统是 Ubuntu 18.04
  • 嗯。我在一个类似的系统上,我从来没有遇到过这个错误。对不起。我的远程调试技能在这一点上是有限的。我怀疑 docker 与 docker-compose 版本不匹配。确保它们是从 Docker 存储库而不是官方 Ubuntu 存储库安装的。
猜你喜欢
  • 2013-03-19
  • 2011-11-20
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多