用docker-compose部署构建django+uwsgi+redis+mysql项目
时间: 2023-08-28 19:07:57 浏览: 174
好的,下面是具体步骤:
1. 编写docker-compose.yml文件
```yaml
version: '3'
services:
web:
build: .
command: uwsgi --ini uwsgi.ini
ports:
- "8000:8000"
depends_on:
- db
- redis
environment:
- DJANGO_SETTINGS_MODULE=project.settings.production
- DB_HOST=db
- DB_PORT=3306
- DB_NAME=project
- DB_USER=root
- DB_PASSWORD=password
- REDIS_HOST=redis
- REDIS_PORT=6379
db:
image: mysql:5.7
volumes:
- ./mysql/data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=project
redis:
image: redis:6
```
2. 编写uwsgi.ini文件
```ini
[uwsgi]
module = project.wsgi:application
master = true
processes = 4
socket = :8000
vacuum = true
harakiri = 30
max-requests = 5000
buffer-size = 32768
```
3. 编写Dockerfile文件
```dockerfile
FROM python:3.8.5-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app
RUN apt-get update && \
apt-get install -y --no-install-recommends gcc python3-dev default-libmysqlclient-dev && \
rm -rf /var/lib/apt/lists/*
COPY requirements.txt /app/
RUN pip install --upgrade pip && \
pip install -r requirements.txt && \
rm -rf /root/.cache/pip
COPY . /app/
EXPOSE 8000
CMD ["uwsgi", "--ini", "uwsgi.ini"]
```
4. 将以上文件放在项目根目录下,并执行以下命令启动:
```
docker-compose up --build
```
这样就可以在本地8000端口访问你的django uwsgi redis mysql项目了。
阅读全文