Deploy react-typescript + django to nginx container (docker)

git add origin

Mirror address [email protected]:mengkaibulusili/centos7.git

 git remote add  origin [email protected]:mengkaibulusili/centos7.git

dockerfile

When the container is running, it should try to keep the container storage layer from writing operations. For database applications that need to store dynamic data, the database files should be stored in a volume.

volume The location of the mounted volume

Let container 2 mount the data content of container 1
docker run --name <container 2> --volumes-from <container 1 name>

Packaging script, packaging self-starting container

Create a data volume container first

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 .

Create application container based on nginx django container

Small details blocking the launch of the container

/root/shopServer/startServer.sh content

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 .

Use data container

//新建数据容器但是不用运行
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

Configure nginx uwsgi

nginx -t -c  /root/shopServer/myNginx.conf
nginx -c  /root/shopServer/myNginx.conf
查看服务是否正常启动
ps -ef|grep uwsgi 

Pause server script

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

Explain the separate deployment of nginx configuration between front and back ends

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;
    }
}

-------------End of focus~~~~~~~~~~~~~~~~~~~~~~

centos7 mirror

Configure Chinese encoding

  && 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 .

use

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

Mount data volume container

Create a data volume container first

Build python3612 image

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

Use 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 

Set up proxy

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

Build nginx + py36 directly

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"]

Build command

Ensure that your network is available

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

use

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 

Mount the data volume

Create a data volume container first

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 .

New application container mount data

//新建数据容器但是不用运行
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 

Build the container

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

Start the service after entering the container

 pip3 install -r requirements.txt && python3 manage.py runserver 0.0.0.0:5000

Guess you like

Origin blog.csdn.net/qq_43373608/article/details/108173826