Docker搭建前后端部署环境

Docker搭建前后端部署环境

Scroll Down

自定义IP网段

可以不用自定义,博主习惯自己定义一个,不自定义默认为17网段

1.创建一个network 
docker network create --subnet=172.18.0.0/16 myNetwork
2.查看创建的network
docker network ls

基本环境搭建

Nginx安装

1. 创建文件夹

1.在/usr/local文件夹下新增docker文件夹,进入docker文件夹新增nginx文件夹
2.在nginx文件夹新增cert、conf、logs文件夹

  • cert文件夹:存放ssl证书文件
  • conf文件夹:存放配置文件
  • logs文件夹:存放日志文件

2. 在conf文件夹新建nginx.conf文件,根据情况修改,内容如下:

#user  nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    proxy_buffer_size 128k;
    proxy_buffers 32 128k;
    proxy_busy_buffers_size 128k;


    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    upstream test.linchaokun.cn {
        server 172.18.0.10:8009;
    }

    server {
        listen       80;
        server_name  test.linchaokun.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://test.linchaokun.cn;
            proxy_redirect off;
            index index.html index.htm;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-Port $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
    }
    server {
        listen 443 ssl;
        server_name test.linchaokun.cn;
        ssl_certificate /etc/nginx/cert/test.linchaokun.cn.pem;
        ssl_certificate_key /etc/nginx/cert/test.linchaokun.cn.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        location / {
            proxy_pass http://test.linchaokun.cn;
            proxy_redirect off;
            index index.html index.htm;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-Port $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

3. 启动nginx

docker run -d -p 80:80 -p 443:443 --name nginx -v /usr/local/docker/nginx/cert:/etc/nginx/cert -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/logs:/var/log/nginx -e TZ=Asia/Shanghai --network myNetwork --ip 172.18.0.2 nginx

Redis安装

1. 创建文件夹

1.进入docker文件夹新增redis文件夹
2.在redis文件夹新增conf、data文件夹

  • conf文件夹:存放配置文件
  • data文件夹:存放数据文件

2. 在conf文件夹新建nginx.conf文件,根据情况修改,内容如下:

#bind 127.0.0.1
protected-mode no
appendonly yes
requirepass redis!@#linchaokun
notify-keyspace-events Ex

requirepass 就是redis的密码

3.启动redis

docker run -d --name redis -p 6379:6379 -v /usr/local/docker/redis/data:/data -v /usr/local/docker/redis/conf/redis.conf:/etc/redis/redis.conf -e TZ=Asia/Shanghai --network myNetwork --ip 172.18.0.3 redis redis-server /etc/redis/redis.conf

Rabbitmq安装

1. 创建文件夹

1.进入docker文件夹新增rabbitmq文件夹
2.在rabbitmq文件夹新增logs、data文件夹

  • logs文件夹:存放日志文件
  • data文件夹:存放数据文件

2.启动rabbtimq

docker run -d --name rabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=jianyu2019 -p 15672:15672 -p 5672:5672 -v /usr/local/docker/rabbitmq/data:/var/lib/rabbitmq -v /usr/local/docker/rabbitmq/logs:/var/log/rabbitmq -e TZ=Asia/Shanghai --network myNetwork --ip 172.18.0.4 rabbitmq:management

MySQL安装

1. 创建文件夹

1.进入docker文件夹新增mysql文件夹
2.在mysql文件夹新增conf,logs、data文件夹

  • conf文件夹:存放配置文件
  • logs文件夹:存放日志文件
  • data文件夹:存放数据文件

2. 在conf文件夹新建my.cnf文件,根据情况修改,内容如下:

[mysql]
default-character-set=utf8

socket=/var/lib/mysql/mysql.sock



[mysqld]

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

max_connections=200

character-set-server=utf8

default-storage-engine=INNODB

lower_case_table_names=1

max_allowed_packet=16M
 
#default-time_zone='+8:00'

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid


#

# include all files from the config directory

#

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

3.启动MySQL

docker run --name mysql -p 3306:3306 --privileged --restart=always -d -v /usr/local/docker/mysql/logs:/logs -v /usr/local/docker/mysql/data:/var/lib/mysql -v  /usr/local/docker/mysql/conf/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -e MYSQL_ROOT_PASSWORD='linchaokun!@#test' -e TZ=Asia/Shanghai --network myNetwork --ip 172.18.0.5 mysql:5.7

MYSQL_ROOT_PASSWORD为mysql密码,自行修改

Nginx部署前端项目

1. 创建文件夹

1.进入docker文件夹新增h5文件夹
2.在h5文件夹新增conf,logs、www文件夹

  • conf文件夹:存放配置文件
  • logs文件夹:存放日志文件
  • www文件夹:存放前端部署文件

2. 在conf文件夹新建nginx.conf文件,根据情况修改,内容如下:

user  nginx;
worker_processes  1;

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

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        access_log  /var/log/nginx/host.access.log  main;
        error_log  /var/log/nginx/error.log  error;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

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

3.启动h5前端项目

docker run -d --name h5 -v /usr/local/docker/h5/www:/usr/share/nginx/html -v /usr/local/docker/h5/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/h5/logs:/var/log/nginx -e TZ=Asia/Shanghai --network myNetwork --ip 172.18.0.6 nginx

完结

搭建中有任何问题,或上面没有列举的环境,欢迎留言。