docker-compose安装与部署djnago+uwsgi+nginx+mysql+redis

一、安装docker-compose(docker安装请参与https://www.cnblogs.com/konglingxi/p/12378295.html

1.curl -L "https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2.chmod +x /usr/local/bin/docker-compose

3.docker-compose --version

4.yum install -y gcc libffi-devel python-devel openssl-devel

  

二、.部署django+uwsgi+nginx+mysql+redis

1、文件结构

 2、cgy_docker/build/kcnet/Dockerfile

#定义该镜像使用的基础镜像为:python:3.7.4-stretch
FROM python:3.7.4-stretch
#定义容器的工作目录
WORKDIR /wwwroot
#拷贝pip安装项目所需要的包名称
COPY requirements.txt ./
#更新pip
RUN pip install --upgrade pip -i https://pypi.douban.com/simple/
#安装依赖包
RUN pip install -r requirements.txt -i https://pypi.douban.com/simple/
#开放的端口
EXPOSE 8000

3、cgy_docker/work/components/mysql/config/my.cnf

[client]
port    = 3306
socket  = /var/run/mysqld/mysqld.sock

[mysql]
prompt="\u@mysqldb \R:\m:\s [\d]> "
no-auto-rehash

[mysqld]
##########################################NEED SET#################################################
#主数据库的id必须小于从数据库的id
server-id = 1
###########################################SET END################################################
user    = mysql
port    = 3306
basedir = /usr
datadir = /var/lib/mysql/
socket  = /var/run/mysqld/mysqld.sock
pid-file = mysqldb.pid
character-set-server = utf8mb4
skip_name_resolve = 1

#sql模式
sql_mode='strict_trans_tables,no_zero_in_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution'

#若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = "+8:00"

open_files_limit    = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
log_timestamps = SYSTEM
slow_query_log_file = /var/lib/mysql/slow.log
log-error = /var/lib/mysql/error.log
long_query_time = 0.1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 3306
log-bin = /var/lib/mysql/mybinlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G

#注意:MySQL 8.0开始,binlog_expire_logs_seconds选项也存在的话,会忽略expire_logs_days选项
#expire_logs_days = 7

master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
binlog_format = row
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30

transaction_isolation = REPEATABLE-READ
#innodb_additional_mem_pool_size = 16M
##########################################NEED SET#################################################
#其他全部使用默认,这个地方的大小设置为内存的70%即可
innodb_buffer_pool_size = 350M
###########################################SET END################################################
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G
innodb_undo_directory = /var/lib/mysql/undolog
innodb_undo_tablespaces = 95

# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0

#注意:MySQL 8.0.16开始删除该选项
internal_tmp_disk_storage_engine = InnoDB

# some var for MySQL 5.7
innodb_checksums = 1
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
query_cache_size = 0
query_cache_type = 0
innodb_undo_logs = 128

innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 0

#performance_schema
performance_schema = 1
performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"

[mysqldump]
quick
max_allowed_packet = 32M

4、cgy_docker/work/components/nginx/nginx.conf

user  nginx;
worker_processes  1;

pid        /var/run/nginx.pid;
error_log  /var/log/nginx/error.log warn;

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 /dev/null;
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

5、cgy_docker/work/components/nginx/config/conf.d/localhost.conf

server {
    client_max_body_size 1024m;
    listen       80;
    server_name  localhost;
    charset  utf-8;
    
    location / {
        include /etc/nginx/uwsgi_params;
        #定义uwsgi的socket,即django项目容器的地址和端口
        uwsgi_pass 10.127.2.5:8000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
    #定义项目静态文件目录,注意alias和root的区别
    location /static {
        alias /wwwroot/kcnet/static;
        autoindex on;
    }

    location /media {
        alias /wwwroot/kcnet/media;
        autoindex on;
    }

    error_page 404 /404.html;
            location = /40x.html {
        }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

6、cgy_docker/work/components/redis/config/redis.conf

#可以根据自己的需要加配置 ,比如requirepass:''

7、cgy_docker/work/components/uwsgi/kcnet/uwsgi.ini

[uwsgi]
uid = www-data
gid = www-data
master = true

;django项目目录路径
chdir     = /wwwroot/kcnet/

;项目的wsgi测试服务器配置文件位置
module = kcnet.wsgi

;定义socket的地址和端口
socket    = 10.127.2.5:8000
processes = 8
threads   = 1
enable-threads = true
chmod-socket = 664
chown-socket = kcnet:www-data
vacuum = true

;定义静态文件
static-map=/static=/wwwroot/kcnet/static
buffer-size  = 65535

8、.env

PROJECT_FOLDER=./work/wwwroot
MYSQL_PASSWORD=root
MYSQL_PORT=3306
REDIS_PORT=6379

9、docker-compose.yml

version: '3'
services:

  ### Django Project-1 container #########################################
  kcnet:
      build:
        #构建django项目kcnet容器的路径
        context: ./build/kcnet
      volumes: #项目数据持久化配置---包括项目、uwsgi配置文件
        - ${PROJECT_FOLDER}/kcnet:/wwwroot/kcnet
        - ./work/components/uwsgi/kcnet/uwsgi.ini:/etc/uwsgi/kcnet/uwsgi.ini
      restart: always  #系统重启后容器自动启动
      privileged: true  #容器启动后是否有系统权限
      networks:   #定义该容器的网桥名称和IP地址
          net-django:
            ipv4_address: 10.127.2.5
      #依赖
      depends_on:
        - redis
        - mysql
      #容器启动后执行的命令,修改容器目录权限、启动uwsgi、迁移数据库、收集静态
      command:
        - /bin/bash
        - -c
        - |
          cd /wwwroot/kcnet
          python manage.py collectstatic --noinput
          python manage.py migrate
          chmod -R 777 /wwwroot/kcnet
          uwsgi --ini /etc/uwsgi/kcnet/uwsgi.ini

  ### Nginx container #########################################
  nginx: 
    image: nginx  #定义nginx使用的镜像,直接从docker hub上获取
    ports:    #定义nginx开放的web端口 80和8080
      - "80:80"
      - "8080:8080"
    volumes:   #nginx数据持久化配置---包括项目、项目nginx配置文件和nginx日志文件
      - ${PROJECT_FOLDER}/kcnet:/wwwroot/kcnet
      - ./work/components/nginx/config/nginx.conf:/etc/nginx/nginx.conf  #windows需要rw权限才能开启nginx服务
      - ./work/components/nginx/config/conf.d:/etc/nginx/conf.d
      - ./work/components/nginx/log:/var/log/nginx
    restart: always   #系统重启后容器自动启动
    privileged: true  #容器启动后是否有系统权限
    networks:     #定义nginx容器的网桥名称和IP地址
      net-django:
        ipv4_address: 10.127.2.4

  ### mysql container #########################################
  mysql:
      image: mysql:5.7   #定义mysql使用的镜像,直接从docker hub上获取
      ports:   #定义mysql开放的数据库端口 根据.env配置变量获取 ${MYSQL_PORT}端口
        - "${MYSQL_PORT}:3306"  # ${MYSQL_PORT}表示地址映射后的端口,3306表示mysql容器内的端口
      volumes:   #mysql数据持久化配置---包括数据库存储位置、数据库配置文件和数据库日志文件路径
        - ./work/components/mysql/data:/var/lib/mysql
        - ./work/components/mysql/config/my.cnf:/etc/mysql/conf.d/my.cnf
        - ./work/components/mysql/log:/var/log/mysql
      restart: always   #系统重启后容器自动启动
      privileged: true   #容器启动后是否有系统权限
      environment:   
        MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}   #通过系统变量${MYSQL_PASSWORD} 设置mysql镜像定义的变量MYSQL_ROOT_PASSWORD
      networks:    #定义mysql容器的网桥名称和IP地址
        net-django:  
          ipv4_address: 10.127.2.3

  ### redis container #########################################
  redis:
      image: redis   #定义redis使用的镜像,直接从docker hub上获取
      ports:   #定义redis开放的数据库端口 根据.env配置变量获取 ${REDIS_PORT}端口
        - "${REDIS_PORT}:6379"  # ${REDIS_PORT}表示地址映射后的端口,6379表示redis容器内的端口
      volumes:   #redis数据持久化配置---包括存储位置、配置文件
        - ./work/components/redis/data:/data
        - ./work/components/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
      restart: always   #系统重启后容器自动启动
      privileged: true   #容器启动后是否有系统权限
      environment:
        - TZ=Asia/Shanghai   #时区
      networks:    #定义redis容器的网桥名称和IP地址
        net-django:
          ipv4_address: 10.127.2.2
      command: redis-server /usr/local/etc/redis/redis.conf # 启动redis服务

### Add Network Bridge info #########################################
networks:   #定义整个docker-compose编排的容器使用的网桥名称和IP地址网段,注意 x.x.x.1不能使用
  net-django:
    ipam:
      config:
        - subnet: 10.127.2.0/24

ps:

1.访问过程

 

2.容器间通讯通过网桥子网

# 查看全部子网
docker network ls
#删除子网
docker network rm 子网名称

  

3.构建、启动、关闭和重启命令(在docker-compose.yml目录下进行)

#构建
docker-compose build
#启动
docker-compose up
#后台启动
docker-compose up -d
#关闭
docker-compose stop
#重启
docker-compose restart
#更多功能:docker-compose --help

4.其他命令

#停止全部容器
docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
#删除全部容器
docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
#删除全部tag为none镜像
docker images|grep none|awk '{print $3}'|xargs docker rmi
#停止全部已开启的容器
docker ps -a | grep 'Up' | awk '{print $1}'| xargs docker container stop
#删除全部已停止的容器
docker ps -a | grep 'Exited' | awk '{print $1}'| xargs docker container rm

 5.更多容器部署参数daocloud

 

posted @ 2020-03-15 14:07  詹姆溪  阅读(490)  评论(0编辑  收藏  举报