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

宇文灿
2023-12-01

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
 类似资料: