在Docker上运行Django和Vue
Run Django and Vue on Docker
Django Dockerfile
生成django项目依赖包.
Generate Django project dependencies.
代码语言:javascript
复制pip freeze > requirements.txt
编写django启动脚本 run.sh
Write a Django startup script run.sh
代码语言:javascript
复制python3 manage.py runserver 0.0.0.0:8000
编写 Django Dockerfile
Write a Dockerfile for django
代码语言:javascript
复制FROM python:alpine3.8
ADD ./bthlt_backend /bthlt_backend/bthlt_backend
ADD ./requirements.txt /bthlt_backend/
ADD ./manage.py /bthlt_backend/
ADD ./run.sh /bthlt_backend/
WORKDIR /bthlt_backend
RUN pip install -r ./requirements.txt
# Define default command.
CMD ["/bin/sh", "run.sh"]
# Expose ports.
EXPOSE 8000
编写构建脚本 build_run.sh
Write a script for build_run.sh
代码语言:javascript
复制#!/bin/bash
docker build -t 123.bthlt.com/bthlt_backend:$1 .
docker push 123.bthlt.com/bthlt_backend:$1
代码语言:javascript
复制bash build_run.sh v0.0.1
Vue Dockerfile
编写nginx.conf
Write nginx.conf
代码语言:javascript
复制server {
listen 80;
server_name localhost;
location /api/ {
proxy_pass http://bthlt-backend:8000/;
}
location / {
root /usr/src/app/;
index index.html index.htm;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;
gzip on;
if_modified_since off;
etag off;
add_header Last-Modified "";
if (!-e $request_filename) {
rewrite ^/[^.] $ /index.html break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
编写Dockerfile
Write Dockerfile
代码语言:javascript
复制FROM nginx:mainline-alpine-perl
ADD ./nginx.conf /etc/nginx/conf.d/default.conf
ADD ./dist/ /usr/src/app/
# Define default command.
CMD ["nginx", "-g", "daemon off;"]
# Expose ports.
EXPOSE 80
编写Vue构建脚本
Write Vue build script
代码语言:javascript
复制#!/bin/bash
yarn build
docker build -t 123.bthlt.com/bthlt_front:$1 .
docker push 123.bthlt.com/bthlt_front:$1
代码语言:javascript
复制bash build_run.sh v0.0.1
在Docker上运行Django和Vue
Run Django and Vue on Docker
代码语言:javascript
复制docker run --name bthlt-backend -d -p 8000:8000 123.bthlt.com/bthlt_backend:v0.0.1
docker run --name bthlt-front -d -p 80:80 --link bthlt-backend:bthlt-backend 123.bthlt.com/bthlt_front:v0.0.1
葫芦的运维日志