赞
踩
安全组中要配置端口,之后才能访问
src/api下的api.js中host根据需要修改
let host = 'http://127.0.0.1:8000';
检查确保vue.config.js文件存在,且配置正确
module.exports = {
lintOnSave: process.env.NODE_ENV !== 'production',
configureWebpack: {
devtool: 'source-map',
},
publicPath: '/',
assetsDir: 'static',
productionSourceMap: false,
devServer: {
historyApiFallback: true
}
};
执行以下命令生成已dist命名的打包文件
npm run build
创建网络后,在同一网络下可以直接只用docker容器名进行调用
docker network create django_net
可以使用命令查看创建的网络列表和详情
# 查看网络列表
docker network ls
# 查看网络详情
docker inspect django_net
使用mysql的5.7版本
docker pull mysql:5.7
注意:指定相关参数需与django的settings配置数据库的信息保持一致(host/password/database),网络选择之前创建的网络
sudo docker run -p 3306:3306 --name db_mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql --restart=always --privileged=true -e MYSQL_ROOT_PASSWORD=123456789 -e MYSQL_DATABASE=django_test -d --network django_net mysql:5.7
检验容器是否成功
# 查看容器状态,应该为up状态
docker ps
# 查看容器日志
docker logs db_mysql
# 进入容器
docker exec -it db_mysql bash
# 使用以下命令输入密码后能登录成功
mysql -uroot -p
# 查看数据库是否有django_test
show databases;
编写Dockerfile文件
FROM python:3.7-alpine LABEL maintainer='ydli' LABEL description='Deploying Django project' # cd /usr/src/app WORKDIR /usr/src/app # /usr/src/app/dev07/ COPY ./django_test ./django_test/ # /usr/src/app/configs/ COPY ./gunicorn_config.py ./configs/ # cd /usr/src/app/dev07/ WORKDIR django_test/ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \ apk upgrade && \ apk add --allow-untrusted build-base mariadb-connector-c-dev curl iputils && \ apk update && apk upgrade && \ pip install --no-cache-dir -i https://pypi.douban.com/simple -r requirements.txt && \ pip install -i https://pypi.douban.com/simple gunicorn && \ apk add ca-certificates bash && \ rm -rf /var/cache/apk/* VOLUME /usr/src/app/logs/ VOLUME /usr/src/app/django_test/ EXPOSE 8000 CMD ["/usr/local/bin/gunicorn", "-c", "/usr/src/app/configs/gunicorn_config.py", "django_test.wsgi"]
gunicorn_config.py
bind = '0.0.0.0:8000'
reload = True
pidfile = '/usr/src/app/logs/gunicorn.pid'
accesslog = '/usr/src/app/logs/gunicorn_acess.log'
errorlog = '/usr/src/app/logs/gunicorn_error.log'
将django项目文件夹放在当前目录下
最后执行命令
docker build -t django_app:v1 .
–name的名字要与nginx中的配置一致,-v时的logs,不是绝对路径,而是dockerfile时要指定VOLUME,然后-v时指定的logs为docker路径下(/var/lib/docker/volumes)
docker run --name django_app -v logs:/usr/src/app/logs/ -v django_code:/usr/src/app/django_test/ -p 8000:8000 --restart=always -d --network django_net django_app:v1
进入容器,在项目目录(/usr/src/app/django_test/)下执行以下命令:
# 建表
python manage.py makemigrations
python manage.py migrate
# 新建用户
echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('test', 'test00@qq.com', '1234')" | python manage.py shell &> /dev/null
基础镜像地址:https://download.csdn.net/download/qq_45499910/85751587
Dockerfile文件
FROM alpine_base:v1 LABEL maintainer='ydli' ARG SERVER_ADDR='ip地址' ENV SERVER_ADDR=${SERVERADDR} COPY docker-entrypoint.sh / RUN apk add --update supervisor nginx tzdata && \ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone && \ apk del tzdata && \ rm -rf /var/cache/apk/* && \ mkdir /tmp/nginx && \ mkdir -p /var/www/html && \ chown -R nginx:nginx /var/www/html && \ chmod u+x /docker-entrypoint.sh COPY dist/ /var/www/html/ COPY configs/default.conf /etc/nginx/conf.d COPY configs/nginx.conf /etc/nginx/nginx.conf COPY configs/supervisord.conf /etc/supervisord.conf VOLUME /var/log/nginx/ VOLUME /var/www/html/ EXPOSE 80 8000 443 ENTRYPOINT ["/docker-entrypoint.sh"]
docker-entrypoint.sh
替换ip地址
#!/bin/sh
sed -i 's/\[SERVER_ADDR\]/${SERVER_ADDR}/gi' /etc/nginx/conf.d/default.conf
supervisord
default.conf
upstream app_server { server django_app:8000; } server { listen 8800; server_name [SERVER_ADDR]; access_log /var/log/nginx/access.log main; error_log off; autoindex_localtime on; location /static { alias /var/www/html/static; } location / { try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } } server { listen 80; server_name [SERVER_ADDR]; root /var/www/html; access_log /var/log/nginx/access.log main; error_log off; location / { try_files $uri $uri/ /index.html; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ /\.ht { deny all; } }
nginx.conf
user nginx; worker_processes 1; error_log off; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; autoindex_localtime on; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; index index.html index.htm; include /etc/nginx/conf.d/*.conf; }
supervisord.conf
[unix_http_server] file=/tmp/supervisor.sock ; (the path to the socket file) [supervisord] logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=5MB ; (max main logfile bytes b4 rotation;default 50MB) pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) user=root nodaemon=true stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 [rpcinterface:supervisor] supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock [program:nginx] command=/usr/sbin/nginx stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stdout_events_enabled=true stderr_events_enabled=true
生成镜像
docker build -t nginx_test:v1 .
docker run --name nginx_test -p 8866:80 -p 8800:8000 --restart=always -d --network django_net nginx_test:v1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。