2024最新版Docker部署Django+MySQL+Redis+Nginx+uWSGI+Celery(超详细)
以下是一个使用Docker部署Django、MySQL、Redis和Nginx的示例。这个示例包括了uWSGI和Celery。
首先,创建一个docker-compose.yml
文件,内容如下:
version: '3.8'
services:
web:
build: .
command: uwsgi --ini /etc/uwsgi/uwsgi.ini
volumes:
- .:/app
depends_on:
- db
- redis
ports:
- "8000:8000"
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'mydatabase'
MYSQL_USER: 'myuser'
MYSQL_PASSWORD: 'mypassword'
MYSQL_ROOT_PASSWORD: 'myrootpassword'
volumes:
- my-db:/var/lib/mysql
redis:
image: redis:6.0
ports:
- "6379:6379"
celery:
build: .
command: celery -A myproject worker --loglevel=info
volumes:
- .:/app
depends_on:
- db
- redis
nginx:
image: nginx:1.19.0
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- web
volumes:
my-db:
在同一目录下创建一个Dockerfile,用于构建web应用:
FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY . /app
RUN pip install --upgrade pip && \
pip install -r requirements.txt
COPY ./uwsgi.ini /etc/uwsgi/uwsgi.ini
创建uWSGI配置文件uwsgi.ini
:
[uwsgi]
module = myproject.wsgi:application
master = true
processes = 4
socket = :8000
vacuum = true
创建Nginx配置文件nginx.conf
:
events {}
http {
server {
listen 80;
location /static/ {
alias /app/static/;
}
location / {
uwsgi_pass web:8000;
include /etc/nginx/uwsgi_params;
}
}
}
确保你的Django项目中有requirements.txt
和myproject/wsgi.py
文件。
最后,运行docker-compose up
命令启动所有服务。
注意:这个例子假设你的Django项目名为myproject
,MySQL数据库、用户和密码按需更改。同时,确保你的Django项目配置中有正确的MySQL、Redis和静态文件设置。
评论已关闭