最近在學習用docker部署Django項目,通過百折不撓的鼓搗,終於將項目部署成功,爬過好多坑,也發現不少技能須要提升。特此寫下隨筆與小夥伴們分享,但願能對你們有所啓發。html
docker的理論我就不贅述了,直接上乾菜吧。python
本人django項目目錄結構:mysql
xxx_project:nginx
appsweb
app1sql
app2docker
app3數據庫
extra_appsdjango
xadminapi
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 部署成功,但願對你們有所啓發,若是在部署中遇到問題能夠留言,一塊兒討論。同時也但願大神評論指教。