本文将采用编写各模块dockerfile的方式,利用docker-compose创建镜像和容器
这里以前端为Vue,后端为Django进行演示
安装docker以及docker-compose
1 2 3 4 5
| # 安装Docker curl -fsSL https://get.docker.com | sh
# 安装docker-compose apt-get install docker-compose -y
|
域名申请证书
添加DNS记录
域名申请证书
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #安装证书工具: curl https://get.acme.sh | sh; apt install socat -y || yum install socat -y; ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
#添加软链接: ln -s /root/.acme.sh/acme.sh /usr/local/bin/acme.sh
#切换CA机构: acme.sh --set-default-ca --server letsencrypt
#申请证书方式1: acme.sh --issue -d 替换为你的域名 --standalone -k ec-256 #申请证书方式2: acme.sh --register-account -m "${RANDOM}@chacuo.net" --server buypass --force --insecure && ~/.acme.sh/acme.sh --issue -d 你的域名 --standalone -k ec-256 --force --insecure --server buypass #申请证书方式3: acme.sh --register-account -m "${RANDOM}@chacuo.net" --server zerossl --force --insecure && ~/.acme.sh/acme.sh --issue -d 你的域名 --standalone -k ec-256 --force --insecure --server zerossl
#安装证书: acme.sh --install-cert -d 你的域名 --ecc --key-file /opt/wendy/nginx/cert/wendy-server.key --fullchain-file /opt/wendy/nginx/cert/wendy-server.crt
|
创建部署目录
在/opt目录下创建自己的项目文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| mkdir -p /opt/wendy; cd /opt/wendy touch mysql-dockerfile python-dockerfile nginx-dockerfile docker-compose.yml mkdir mysql-db; mkdir python mkdir -p nginx/cert mkdir -p nginx/web touch nginx/nginx.conf
# 防火墙开启端口 ufw allow from any to any port 80,8000,443,3306 proto tcp
# 将项目所需的文件放在该目录下 root@vultr:ls /opt/wendy docker-compose.yml mysql-db mysql-dockerfile python python-dockerfile nginx nginx-dockerfile
|
ps:
mysql-db目录下存放‘.sql’文件
python目录下存放 python后端文件
nginx目录下存放 nginx.conf文件、证书、前端文件
编辑docker-compose.yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| version: '3' services: python: build: context: . dockerfile: python-dockerfile image: wendy-python-image:latest container_name: wendy-python-container ports: - "8000:8000" volumes: - ./python:/opt/wendy/python depends_on: - mysql
mysql: build: context: . dockerfile: mysql-dockerfile image: wendy-mysql-image:latest container_name: wendy-mysql-container ports: - "3306:3306" volumes: - ./mysql-db:/docker-entrypoint-initdb.d
nginx: build: context: . dockerfile: nginx-dockerfile image: wendy-nginx-image:latest container_name: wendy-nginx-container ports: - "80:80" - "443:443" volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./nginx/cert:/opt/wendy/nginx/cert - ./nginx/web:/opt/wendy/nginx/web
|
编辑mysql-dockerfile,填入以下内容
1 2 3 4 5 6 7 8 9 10 11
|
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=123456 ENV MYSQL_DATABASE=wendy_django_db
EXPOSE 3306
|
编辑python-dockerfile,填入以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
FROM python:3.6
RUN pip install --upgrade pip
WORKDIR /opt/wendy/python
COPY ./python /opt/wendy/python
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python3 manage.py runserver 0.0.0.0:8000
|
因为django项目需要内映射mysql容器,需要修改数据库的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
ALLOWED_HOSTS = ['43.136.62.137', '0.0.0.0', '*', 'wendy-mysql-container']
ATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'wendy_django_db', 'USER': 'root', 'PASSWORD': '123456', 'HOST': 'wendy-mysql-container', 'PORT': '3306', } }
|
编辑nginx-dockerfile,填入以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
FROM nginx:latest
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./nginx/cert /opt/wendy/nginx/cert
COPY ./nginx/web /opt/wendy/nginx/web
EXPOSE 80 EXPOSE 443
CMD ["nginx", "-g", "daemon off;"]
|
编辑nginx.conf文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { server { listen 80; server_name wendy.xxx.com; return 301 https://$server_name$request_uri; }
server { listen 443 ssl http2; server_name wendy.xxx.com;
add_header Access-Control-Allow-Origin '*'; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE'; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,X-Total-Count'; add_header Access-Control-Expose-Headers 'X-Total-Count';
ssl_certificate /opt/wendy/nginx/cert/wendy-server.crt; ssl_certificate_key /opt/wendy/nginx/cert/wendy-server.key;
charset utf-8;
location / { include mime.types; default_type application/octet-stream; root /opt/wendy/web/; index index.html; }
error_page 404 /404.html; location = /404.html {}
error_page 500 502 503 504 /50x.html; location = /50x.html {}
include /etc/nginx/default.d/*.conf; } }
|
docker-compose建立镜像并启动容器
运行成功