成功最有效的方法就是向有经验的人学习!

自己构建django的docker镜像

适用于新手学习,生产环境需要优化

FROM centos:7#继承centos

ADD epel.repo /etc/yum.repos.d#将epel源文件,拷贝到容器/etc/yum.repos.d,ADD相比COPY,还带压缩解压

RUN yum update -y#执行yum更新
RUN yum install -y python36#下载python36
RUN yum install -y python36-pip#下载pip工具
RUN pip3 install setuptools#下载setuptools
ADD Django-2.1.7.tar.gz /opt/#将当前tar.gz包解压并复制到容器opt下

WORKDIR /opt/#更换工作目录
RUN mv Django-2.1.7 django#将解压后的Django-2.1.7移动到django文件夹下

WORKDIR /opt/django
RUN python3 setup.py install#执行安装命令

WORKDIR /opt
RUN django-admin.py startproject mypro#创建django项目mypro
ADD run.sh /opt/mypro/run.sh#将run.sh拷贝容器内
RUN sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['\*'\]/g" /opt/mypro/mypro/settings.py#更改ALLOWED_HOSTS让其他IP能访问

WORKDIR /opt/mypro
RUN chmod 777 run.sh

EXPOSE 8000#暴露端口8000
CMD ["/bin/sh","run.sh"]#执行run.sh

实例2
使用ubuntu系统

FROM ubuntu:16.04
MAINTAINER Dockerfiles

# Install required packages and remove the apt packages cache when done.

RUN apt-get update && \
     apt-get upgrade -y && \
     apt-get install -y \
     git \
     python3 \
     python3-dev \
     python3-setuptools \
     python3-pip \
     nginx \
     supervisor \
     sqlite3 && \
     pip3 install -U pip setuptools && \
  rm -rf /var/lib/apt/lists/*

# install uwsgi now because it takes a little while
RUN pip3 install uwsgi

# RUN source /etc/profile

# setup all the configfiles
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY nginx-app.conf /etc/nginx/sites-available/default
COPY supervisor-app.conf /etc/supervisor/conf.d/

COPY app/requirements.txt /home/docker/code/app/
RUN pip3 install -r /home/docker/code/app/requirements.txt

# add (the rest of) our code
COPY . /home/docker/code/
RUN chmod 777 -R /home/docker/code

# 容器对外暴露8081断开(对应内部是nginx的监听端口)
EXPOSE 8081
CMD ["/usr/bin/supervisord", "-n"]

supervisor配置 supervisor-app.conf

# 启动uwsgi进程
[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini
stdout_logfile=/var/log/uwsgi.log
stdout_logfile_maxbytes=512KB
stderr_logfile=/var/log/uwsgi_error.log
stderr_logfile_maxbytes=512KB
logfile_backups=3

# 启动nginx进程
[program:nginx-app]
command = /usr/sbin/nginx

nginx配置 nginx-app.conf

# nginx-app.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:/home/docker/code/app.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on, default_server indicates that this server block
    # is the block to use if no blocks match the server_name
    listen      8081 default_server;

    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/docker/persistent/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/docker/volatile/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/docker/code/uwsgi_params; # the uwsgi_params file you installed
    }
}

uwsgi配置文件 uwsgi.ini

[uwsgi]
# this config will be loaded if nothing specific is specified
# load base config from below
ini = :base

# %d is the dir this configuration file is in
socket = %dapp.sock
master = true
processes = 4
enable-threads=true

[base]
# chdir to the folder of this config file, plus app/website
chdir = %dapp/
# load the module from wsgi.py, it is a python path from 
# the directory above.
module={project_dir}.wsgi:application
# allow anyone to connect to the socket. This is very permissive
chmod-socket=666

uwsgi参数文件,uwsgi_params

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;
赞(2) 打赏
未经允许不得转载:陈桂林博客 » 自己构建django的docker镜像
分享到

大佬们的评论 抢沙发

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册