(1)centos上下載docker + docker-compose
(2)基礎目錄


(3)首先創建一個純凈的python+django+uwsgi的鏡像,便於后期使用(也可不用創建,后期docker-compose的時候再創建python鏡像,這里我們先創建,后期直接把項目放進去,不用每次都下載環境)
創建python+django+uwsgi的純凈鏡像,命名鏡像名為django:
#Dockerfile 這個dockrfile不是基礎目錄中的Dockerfile,需要在其他目錄中創建 FROM python RUN mkdir /code WORKDIR /code ADD requirements.txt . /code RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt RUN rm -f requirements.txt
#requirements.txt
django==2.2.2 psycopg2 uwsgi
(4)基礎目錄中的Dockerfile
#使用剛剛創建的基礎鏡像django FROM django WORKDIR /code RUN mkdir hello #創建項目目錄 ADD . /code/hello
(5)創建uwsgi配置文件
創建conf/uwsgi.ini配置文件
#conf/uwsgi.ini [uwsgi] socket = 0.0.0.0:8000 chdir = /code/hello #注意:在這里的路徑是容器內的項目所在的路徑,而不是宿主機放項目的路徑 module = hello.wsgi #daemonize = uwsgi.log master = True processes = 4
(6)創建nginx配置文件
創建nginx/nginc.conf
#nginx/nginx.conf
events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; charset utf-8; location / { include uwsgi_params; uwsgi_pass 10.127.2.3:8000; #端口要和uwsgi里配置的一樣 #uwsgi_param UWSGI_SCRIPT hello.wsgi; #wsgi.py所在的目錄名+.wsgi #uwsgi_param UWSGI_CHDIR /opt/deploy/hello; #項目路徑 } location /static/ { alias /code/hello/static/; #靜態資源路徑 #注意:在這里的路徑是容器內的項目所在的路徑,而不是宿主機放項目的路徑 } }
(7)創建nginx的Dockerfile
創建nginx/Dockerfile
#nginx/Dockerfile FROM nginx WORKDIR /etc/nginx/ RUN cp nginx.conf ./nginx.conf.bak COPY nginx.conf ./ CMD ["nginx", "-g", "daemon off;"]
(8)創建docker-compose.yml
version: '3'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: 12345
networks:
net-django:
ipv4_address: 10.127.2.4
web:
build: .
#command: python3 manage.py runserver 0.0.0.0:8000
privileged: true
#ports:
# - 8000:8000
depends_on:
- db
networks:
net-django:
ipv4_address: 10.127.2.3
#command: uwsgi --chdir=/code/hello --module=hello.wsgi --master --socket 0.0.0.0:8000
command: uwsgi --ini /code/hello/conf/uwsgi.ini #注意:在這里的路徑是容器內的項目所在的路徑,而不是宿主機放項目的路徑
nginx:
container_name: nginx-container
restart: always
depends_on:
- web
links:
- "web:web"
build: ./nginx
ports:
- 8080:80
networks:
net-django:
ipv4_address: 10.127.2.2
networks:
net-django:
ipam:
config:
- subnet: 10.127.2.0/24
(9)最后創建容器
docker-compose build docker-compose up -d
(10)容器創建啟動后,登錄x.x.x.x:8080即可

