最近在学习用docker部署Django项目,经过百折不挠的鼓捣,终于将项目部署成功,爬过好多坑,也发现很多技能需要提高。特此写下随笔与小伙伴们分享,希望能对大家有所启发。
docker的理论我就不赘述了,直接上干菜吧。
本人django项目目录结构:
xxx_project:
apps
app1
app2
app3
extra_apps
xadmin
ueditor
xxx_project
settings.py
urls.py
wsgi.py
templates
xxx.html
requirements.txt
manage.py
static
media
conf
uwsgi.ini
nginx
Dockerfile
nginx.conf
uc_nginx.conf
Dockerfile
docker-compose.yml
docker-entrypoins.sh
1.首先上docker-compose.yml配置文件
version: '3'
services:
db:
image: mysql:5.7
expose:
- 3306
environment:
MYSQL_DATABASE: xxxxxx
MYSQL_ROOT_PASSWORD: xxxxxx
MYSQL_USER: root
volumes:
- ./mycustom.cnf:/etc/mysql/conf.d/custom.cnf
- ~/containers/mysql/data:/var/lib/mysql
web:
build: .
restart: always
command: uwsgi -i ./conf/uwsgi.ini
volumes:
- .:/docker_api
ports:
- 8000:8000
depends_on:
- db
links:
- db
nginx:
container_name: nginx-container
restart: always
depends_on:
- web
build: ./nginx
ports:
- 8080:80
volumes:
- .:/docker_api ~
注:db 数据库
web 项目
nginx 代理服务器
2.现在上web的Dockerfile配置文件
FROM python:2.7.12
LABEL maintainer 0x0101010
ENV PYTHONUNBUFFERED 1
RUN mkdir /docker_api
WORKDIR /docker_api
ADD . /docker_api
RUN pip install --upgrade pip
RUN pip install -i https://pypi.douban.com/simple -r requirements.txt
3.现在上nginx的Dockerfile
FROM nginx:latest
COPY uc_nginx.conf /etc/nginx/sites-available/ RUN mkdir -p /etc/nginx/sites-enabled/\
&& ln -s /etc/nginx/sites-available/uc_nginx.conf /etc/nginx/sites-enabled/ CMD ["nginx", "-g", "daemon off;"]
4. nginx 和uwsgi的配置就不在赘述,本人前一篇随笔有详细讲解,可参考
5.项目启动
sudo docker-compose up --build
部署成功
至此,项目已通过docker-compose 部署成功,希望对大家有所启发,如果在部署中遇到问题可以留言,一起讨论。同时也希望大神评论指教。