docker-compose.yml
version: '2'
services:
db:
container_name: postgres
image: postgres
hostname: db
environment:
POSTGRES_PASSWORD: password123
# ports:
# - "5432:5432"
# (HOST:CONTAINER)
volumes:
- pgdata:/var/lib/postgresql/data/
redis:
image: redis
container_name: redis
hostname: redis
restart: always
# ports:
# - "6379:6379"
# extra_hosts:
# - "redis:0.0.0.0"
volumes:
- redisdata:/data
nginx:
container_name: nginx
build:
context: ./
dockerfile: ./nginx/Dockerfile
restart: always
ports:
- "8080:80"
volumes:
- api_data:/docker_api
- ./log:/var/log/nginx
depends_on:
- backend
backend:
container_name: backend
build: ./backend
restart: always
command: uwsgi --ini uwsgi.ini
volumes:
- api_data:/docker_api
links:
- db
- redis
depends_on:
- db
- redis
volumes:
api_data:
pgdata:
redisdata:
后端/ dockerfile
FROM python:latest
LABEL maintainer ugodubai
ENV PYTHONUNBUFFERED 1
RUN mkdir /docker_api
WORKDIR /docker_api
COPY . /docker_api
# RUN apt-get update && apt-get install vim
RUN pip install --upgrade pip && pip install -r requirements.txt
nginx.conf
upstream uwsgi{
server unix:/docker_api/app.sock;
}
server {
listen 80;
server_name localhost ugodubai.com;
root /usr/share/nginx/html;
location /media {
alias /docker_api/backend/media;
}
location /api {
include /etc/nginx/uwsgi_params;
uwsgi_pass uwsgi;
}
location / {
try_files $uri $uri/ @router;
index index.html;
}
index index.html;
location @router {
rewrite ^.*$ /index.html last;
}
}
我的项目使用Docker Compose部署了Django应用,当我更新代码并将其提交到GitHub时遇到问题,然后我在服务器上git pull
并使用docker-compose build --no-cache && docker-compose Up -d --force-recreate
(代码)容器中的容器没有更改。
我当前的想法是更改容器中的代码,并保持docker_api / backend / media中的图像不被清除。我怎样才能做到这一点? docker_api / backend / media保存用户上传的图像。
我想这个问题可能来自数据量,api_data:/ docker_api将整个应用程序用作数据量,因此每次重建代码都被数据量覆盖吗?但是我需要nginx才能将docker_api / app.sock数据用作反向代理。
问题解决了, docker-compose.yml
version: '3'
services:
db:
container_name: postgres
image: postgres
hostname: db
environment:
POSTGRES_PASSWORD: password123
volumes:
- pgdata:/var/lib/postgresql/data/
# networks:
# - database_network
redis:
image: redis
container_name: redis
hostname: redis
restart: always
volumes:
- redisdata:/data
nginx:
container_name: nginx
build:
context: ./
dockerfile: ./nginx/Dockerfile
restart: always
ports:
- "8080:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./frontend/dist:/usr/share/nginx/html
- static_volume:/backend/static
- media_volume:/backend/media
depends_on:
- backend
# networks:
# - nginx_network
backend:
container_name: backend
build: ./backend
restart: always
volumes:
- .:backend
- static_volume:/backend/static
- media_volume:/backend/media
links:
- db
- redis
depends_on:
- db
- redis
# networks:
# - nginx_network
# - database_network
# networks:
# nginx_network:
# driver: bridge
# database_network:
# driver: bridge
volumes:
pgdata:
redisdata:
static_volume:
media_volume:
nginx.conf
upstream backend_server{
# server unix:/docker_api/app.sock;
server backend:8000;
}
server {
listen 80;
server_name localhost ugodubai.com;
root /usr/share/nginx/html;
location /media {
alias /backend/media;
}
location /api {
# uwsgi_pass uwsgi;
proxy_pass http://backend_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location / {
try_files $uri $uri/ @router;
index index.html;
}
index index.html;
location @router {
rewrite ^.*$ /index.html last;
}
}