将 react-typescript + django 部署到 nginx 容器(docker)

简介: 将 react-typescript + django 部署到 nginx 容器(docker)

git 添加 origin

镜像地址 git@github.com:mengkaibulusili/centos7.git

git remote add  origin git@github.com:mengkaibulusili/centos7.git

dockerfile

容器运行时应该尽量保持容器存储层不发生写操作,对于数据库类需要保存动态数据的应用,其数据库文件应该保存于卷(volume)中


volume 挂载卷的位置


令 容器2 挂载 容器1 的数据内容

docker run --name <容器2> --volumes-from <容器1名称>

封装脚本,包装自启动容器

先创建一个数据卷 容器

cat << EOF > DockerfileDataVolum 
FROM alpine
COPY shopServer /root/shopServer
VOLUME /root/shopServer
CMD ["/bin/bash"]
docker build -f DockerfileDataVolum  -t registry.cn-hangzhou.aliyuncs.com/mkmk/centos:VolumeData .

创建应用容器 基于 nginx django 容器

阻塞 容器推出的小细节

/root/shopServer/startServer.sh 内容

cd /root/shopServer && \
source /root/.bashrc && \
pip3 install -r requirements.txt && \
uwsgi -x /root/shopServer/shopServer.xml && \
nginx -t  -c /root/shopServer/nginx/myNginx.conf && \
nginx  -c /root/shopServer/nginx/myNginx.conf 
# tail -f 可以阻塞 容器执行网命令退出
tail -f /dev/null
cat << EOF > shopServerDF
FROM registry.cn-hangzhou.aliyuncs.com/mkmk/centos:Py36Nginx
CMD ["/bin/bash","/root/shopServer/startServer.sh"]
docker build -f shopServerDF -t registry.cn-hangzhou.aliyuncs.com/mkmk/centos:shopServer .

使用数据容器

//新建数据容器但是不用运行
docker create --name djangoVolume  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:VolumeData
docker stop centos7base  | docker rm centos7base 
//运行并 挂载数据容器
docker run -d --name centos7base -p 13000:3000 -p 15000:5000  --volumes-from djangoVolume    --privileged=true  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:shopServer


配置 nginx uwsgi

nginx -t -c  /root/shopServer/myNginx.conf
nginx -c  /root/shopServer/myNginx.conf


查看服务是否正常启动
ps -ef|grep uwsgi 

暂停服务器脚本

nginx -s stop
pkill -9 uwsgi
pkill -9 python3

详解前后端 分离部署 nginx 配置

user root;
 # 容器内设置root 权限启动 应用, 否则部分文件夹无法访问
events { 
#最大连接数
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    # 扩大请求头 的体积上限, 默认 4k
    client_header_buffer_size 16k;
    large_client_header_buffers 10 1m;
    server {
        listen 5000;
        server_name  _; #改为自己的域名,没域名修改为127.0.0.1:80
        charset utf-8;
    # django 后端 api 服务
        location /api/ {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8999;  #端口要和uwsgi里配置的一样
           uwsgi_param UWSGI_SCRIPT shopServer.wsgi;  #wsgi.py所在的目录名+.wsgi
           uwsgi_param UWSGI_CHDIR /root/shopServer/; #项目路径
        }
        # 静态资源路径 也可以不配置, 
        #因为我们使用的是 webpack 打包的应用程序
        # 只要部署了 build 文件即可
        location /static/ {
        alias /root/shopServer/templates/static/; #静态资源路径
        }
    # 部署前台 资源 的 打包路径
        location / {
           index index.html;
           alias /root/shopServer/build/;
        }
        access_log  /root/shopServer/server.log;
        error_log  /root/shopServer/server.error.log;
    }
}

-------------重点结束~~~~~~~~~~~~~~~~~~~~~~

centos7 镜像

配置中文编码

  && yum -y install gcc automake autoconf libtool make wget \
  && yum -y install kde-l10n-Chinese telnet  \
  && yum -y install glibc-common \
  && yum clean all \
  && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8   \
  && echo -e 'export LANG="zh_CN.UTF-8"\nexport LC_ALL="zh_CN.UTF-8"' > /etc/locale.conf \
  && source /etc/locale.conf \
ENV LC_ALL  zh_CN.UTF-8
ENV LANG    zh_CN.UTF-8
cd centos7
docker build -t  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:base7 .

使用

 docker run -d --name centos7base -p 13000:3000 -p 15000:5000  --privileged=true registry.cn-hangzhou.aliyuncs.com/mkmk/centos:base7 init

挂载数据卷容器

先创建一个数据卷容器

构建python3612镜像

docker build -f DockerfilePy3612  -t  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:base7Py3612 .


使用 python3612

docker stop centos7base  | docker rm centos7base 
 docker run -d --name centos7base -p 13000:3000 -p 15000:5000  --privileged=true registry.cn-hangzhou.aliyuncs.com/mkmk/centos:base7Py3612 init 


设置代理

export http_proxy="http://192.168.1.6:7890/"
export https_proxy="http://192.168.1.6:7890/"

直接构建 nginx + py36

FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
COPY  nginx.repo /etc/yum.repos.d/nginx.repo 
COPY  get-pip.py /usr/local/python36/get-pip.py
COPY  sqlite-autoconf-3330000.tar.gz  /root/sqlite-autoconf-3330000.tar.gz
ENV   GPG_KEY 0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
ENV   PYTHON_VERSION 3.6.12
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 20.2.2
# https://github.com/pypa/get-pip
ENV PYTHON_GET_PIP_URL https://github.com/pypa/get-pip/raw/5578af97f8b2b466f4cdbebe18a3ba2d48ad1434/get-pip.py
ENV PYTHON_GET_PIP_SHA256 d4d62a0850fe0c2e6325b2cc20d818c580563de5a2038f917e3cb0e25280b4d1
RUN  export http_proxy="http://192.168.1.6:7890/" \
  && export https_proxy="http://192.168.1.6:7890/" \
  && sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo \
  && sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo \
  && sed -i "s@http://mirror.centos.org@https://mirrors.huaweicloud.com@g" /etc/yum.repos.d/CentOS-Base.repo \
  && yum clean all \
  && yum makecache \
  && yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel  \
  && yum -y install gcc automake autoconf libtool make wget \
  && yum -y install kde-l10n-Chinese telnet  \
  && yum -y install glibc-common \
  && yum clean all \
  && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8   \
  && echo -e 'export LANG="zh_CN.UTF-8"\nexport LC_ALL="zh_CN.UTF-8"' > /etc/locale.conf \
  && source /etc/locale.conf \
  && cd /root \
  && tar -xf sqlite-autoconf-3330000.tar.gz \
  && cd sqlite-autoconf-3330000 \
  &&  ./configure --enable-optimizations \
  && make && make install \
  && ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3 \
  && cd /root \
  && rm -rf /root/sqlite-autoconf-3330000* \
  && set -ex \
  && wget -O python.tar.xz "https://mirrors.huaweicloud.com/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
  && wget -O python.tar.xz.asc "https://mirrors.huaweicloud.com/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
  && export GNUPGHOME="$(mktemp -d)" \
  && { command -v gpgconf > /dev/null && gpgconf --kill all || :; } \
  && rm -rf "$GNUPGHOME" python.tar.xz.asc \
  && mkdir -p /usr/src/python \
  && tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
  && rm python.tar.xz \
  \
  && cd /usr/src/python \
  && LD_RUN_PATH=/usr/local/lib ./configure \
    --prefix=/usr/local/python36 \
    --enable-loadable-sqlite-extensions \
    --enable-optimizations \
    --enable-option-checking=fatal \
    --enable-shared \
    --with-system-expat \
    --with-system-ffi \
    --without-ensurepip \
  && LD_RUN_PATH=/usr/local/lib make -j "$(nproc)" \
# setting PROFILE_TASK makes "--enable-optimizations" reasonable: https://bugs.python.org/issue36044 / https://github.com/docker-library/python/issues/160#issuecomment-509426916
      PROFILE_TASK='-m test.regrtest --pgo \
      test_array \
      test_base64 \
      test_binascii \
    ' \
  && LD_RUN_PATH=/usr/local/lib make install \
  && cd /usr/local/python36 \
  && rm -rf /usr/src/python \
  && ln -s /usr/local/python36/lib/libpython3.6m.so.1.0 /usr/lib/libpython3.6m.so.1.0  \
  && ldconfig \
  && ln -s /usr/local/python36/bin/idle3 /usr/bin/idle3 \
  && ln -s /usr/local/python36/bin/pydoc3 /usr/bin/pydoc3 \
  && ln -s /usr/local/python36/bin/python3 /usr/bin/python3 \
  && ln -s /usr/local/python36/bin/python3-config /usr/bin/python3-config \
  && /usr/bin/python3 --version \
  && /usr/bin/python3 get-pip.py \
    --disable-pip-version-check \
    --no-cache-dir \
    "pip==$PYTHON_PIP_VERSION" \
  && rm -f get-pip.py \
  && ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3 \
  && /usr/bin/pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
  && yum-config-manager --enable nginx-stable \
  && yum install nginx -y \
  && echo 'export PATH=/usr/local/python36/bin:$PATH' >> /root/.bashrc \
  && /usr/bin/pip3 install uwsgi  
LABEL \
  org.label-schema.schema-version="1.0" \
  org.label-schema.name="CentOS Base Image" \
  org.label-schema.vendor="CentOS" \
  org.label-schema.license="GPLv2" \
  org.label-schema.build-date="20200809" \
  org.opencontainers.image.title="CentOS Base Image" \
  org.opencontainers.image.vendor="CentOS" \
  org.opencontainers.image.licenses="GPL-2.0-only" \
  org.opencontainers.image.created="2020-08-09 00:00:00+01:00"
ENV LC_ALL  zh_CN.UTF-8
ENV LANG    zh_CN.UTF-8
CMD ["init"]

构建命令

保证自己的网络可用

docker build -f DockerfilePy3612Nginx  -t  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:Py36Nginx .

使用

docker stop centos7base  | docker rm centos7base 
docker run -d --name centos7base -p 13000:3000 -p 15000:5000  --privileged=true  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:Py36Nginx  init 

进行数据卷的挂载

先创建一个数据卷 容器

cat << EOF > DockerfileDataVolum 
FROM alpine
COPY shopServer /root/shopServer
VOLUME /root/shopServer
CMD ["/bin/bash"]
docker build -f DockerfileDataVolum  -t registry.cn-hangzhou.aliyuncs.com/mkmk/centos:VolumeData .

新建应用容器挂载数据

//新建数据容器但是不用运行
docker create --name djangoVolume  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:VolumeData
docker stop centos7base  | docker rm centos7base 
//运行并 挂载数据容器
docker run -d --name centos7base -p 13000:3000 -p 15000:5000  --volumes-from djangoVolume    --privileged=true  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:Py36Nginx  init 

构建容器

 docker build -f DockerfilePy3612Nginx  -t  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:Py36Nginx .

进入容器后启动服务

 pip3 install -r requirements.txt && python3 manage.py runserver 0.0.0.0:5000
相关实践学习
基于Hologres轻量实时的高性能OLAP分析
本教程基于GitHub Archive公开数据集,通过DataWorks将GitHub中的项⽬、行为等20多种事件类型数据实时采集至Hologres进行分析,同时使用DataV内置模板,快速搭建实时可视化数据大屏,从开发者、项⽬、编程语⾔等多个维度了解GitHub实时数据变化情况。
阿里云实时数仓实战 - 用户行为数仓搭建
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求:熟练掌握 SQL 语法熟悉 Linux 命令,对 Hadoop 大数据体系有一定的了解 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
相关文章
|
7月前
|
应用服务中间件 网络安全 nginx
手把手教你使用 Docker 部署 Nginx 教程
本文详解Nginx核心功能与Docker部署优势,涵盖镜像拉取、容器化部署(快速、挂载、Compose)、HTTPS配置及常见问题处理,助力高效搭建稳定Web服务。
3103 4
|
10月前
|
关系型数据库 应用服务中间件 nginx
Docker一键安装中间件(RocketMq、Nginx、MySql、Minio、Jenkins、Redis)
本系列脚本提供RocketMQ、Nginx、MySQL、MinIO、Jenkins和Redis的Docker一键安装与配置方案,适用于快速部署微服务基础环境。
|
应用服务中间件 PHP nginx
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
简介: 本教程介绍如何基于 Dragonwell 的 Ubuntu 镜像创建一个运行 Nginx 的 Docker 容器。首先从阿里云容器镜像服务拉取基础镜像,然后编写 Dockerfile 确保 Nginx 作为主进程运行,并暴露 80 端口。最后,在包含 Dockerfile 的目录下构建自定义镜像并启动容器,确保 Nginx 在前台运行,避免容器启动后立即退出。通过 `docker build` 和 `docker run` 命令完成整个流程。
548 25
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
|
7月前
|
应用服务中间件 Linux nginx
在虚拟机Docker环境下部署Nginx的步骤。
以上就是在Docker环境下部署Nginx的步骤。需要注意,Docker和Nginix都有很多高级用法和细节需要掌握,以上只是一个基础入门级别的教程。如果你想要更深入地学习和使用它们,请参考官方文档或者其他专业书籍。
334 5
|
12月前
|
应用服务中间件 Linux 网络安全
技术指南:如何把docsify项目部署到基于CentOS系统的Nginx中。
总结 与其他部署方法相比,将docsify项目部署到基于CentOS系统的Nginx中比较简单。以上步骤应当帮助你在不花费太多时间的情况下,将你的项目顺利部署到Nginx中。迈出第一步,开始部署你的docsify项目吧!
443 14
|
前端开发 应用服务中间件 nginx
docker安装nginx,前端项目运行
通过上述步骤,你可以轻松地在Docker中部署Nginx并运行前端项目。这种方法不仅简化了部署流程,还确保了环境的一致性,提高了开发和运维的效率。确保按步骤操作,并根据项目的具体需求进行相应的配置调整。
1455 25
|
7月前
|
缓存 前端开发 JavaScript
React Hooks深度解析与最佳实践:提升函数组件能力的终极指南
🌟蒋星熠Jaxonic,前端探索者。专注React Hooks深度实践,从原理到实战,分享状态管理、性能优化与自定义Hook精髓。助力开发者掌握函数组件的无限可能,共赴技术星辰大海!
React Hooks深度解析与最佳实践:提升函数组件能力的终极指南
|
12月前
|
缓存 前端开发 数据安全/隐私保护
如何使用组合组件和高阶组件实现复杂的 React 应用程序?
如何使用组合组件和高阶组件实现复杂的 React 应用程序?
398 68
|
12月前
|
缓存 前端开发 Java
在 React 中,组合组件和高阶组件在性能方面有何区别?
在 React 中,组合组件和高阶组件在性能方面有何区别?
346 67