docker安装mysql、tomcat、nginx,容器互联,应用部署

1.首先下载镜像:docker pull mysql:5.7.22, docker pull tomcat:8(这里我用的是tomcat8做演示)。

2.工程的sql脚本文件gznb_college.sql放在目录/usr/local/sql_scripts下,运行mysql的容器,

 执行命令:docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d --name mysql -v /data/mysql:/data -v /data/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf mysql:5.7.22

 -p暴露容器端口,-v数据卷目录,-e设置mysql环境变量,这里设置root用户密码为root。

-p(小写)则可以指定要映射的IP和端口,但是在一个指定端口上只可以绑定一个容器。支持的格式有 hostPort:containerPortip:hostPort:containerPort、 ip::containerPort。

/data/mysql目录为数据库脚本所在目录。

-v /data/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf 用于配置mysql字符集为utf8。

注意:数据库字符集设置,不然出现中文乱码。在服务器上创建mysql配置文件,在目录/data/conf下创建文件mysqld.cnf,内容如下:

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

default-time-zone = '+08:00'
lower_case_table_names=1
character-set-server=utf8 
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8

3.进入容器,首先执行docker ps查看容器id,执行:docker exec -it containid /bin/bash,此时我们进入mysql容器的内部,mysql -u root -p 输入密码,连接到mysql,创建数据库并使用该数据库,执行命令source /data/gznb_college.sql导入数据。

4.运行tomcat容器:docker run --name tomcat -p 8080:8080 -v /usr/local/apps/工程war包:/usr/local/tomcat/webapps/工程war包 --link mysql:mysql tomcat:8

 如图所示:

5.进入tomcat容器,修改解压后的war包文件夹,修改连接mysql的连接:

 docker安装mysql、tomcat、nginx,容器互联,应用部署_第1张图片

 --link mysql:db mysql是步骤2中创建的mysql容器的名字,可以通过其访问数据库。db是别名,web程序访问mysq数据可以通过其访问,如:jdbc_url=jdbc:mysql://db:3306/database?useUnicode=true&characterEncoding=utf8

注意:3306为mysql数据库容器的端口,不是host映射主机后的端口。

 此时通过tomcat的8080端口即可正常访问应用。接下来我们配置nginx作为跳转服务器。

6.nginx镜像:docker pull nginx ,并在/usr/local创建文件夹目录:nginx_config,创建文件nginx.conf,其内容为:

user  root;
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    use epoll;
    worker_connections  65535;
}


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

    log_format log_access '$remote_addr - $remote_user [$time_local] "$request" $http_host '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          '"$upstream_addr" "$upstream_status" $upstream_cache_status "$upstream_http_content_type" "$upstream_response_time" > $request_time ' ;

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    client_body_timeout  20;
    client_header_timeout 20;
    keepalive_timeout  3000;
    send_timeout       20;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml application/javascript  application/octet-stream;
    gzip_vary on;



    upstream px-exam-web {
        server px-exam:8080 weight=1;
    }
 
    server {
        listen       80;
        server_name  localhost 10.66.30.224;	
        location ^~/api/exam {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://px-exam-web;
          client_max_body_size 10m;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   html;
        }
    }

}
error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    use epoll;
    worker_connections  65535;
}


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

    log_format log_access '$remote_addr - $remote_user [$time_local] "$request" $http_host '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          '"$upstream_addr" "$upstream_status" $upstream_cache_status "$upstream_http_content_type" "$upstream_response_time" > $request_time ' ;

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    client_body_timeout  20;
    client_header_timeout 20;
    keepalive_timeout  3000;
    send_timeout       20;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml application/javascript  application/octet-stream;
    gzip_vary on;



    upstream px-exam-web {
        server px-exam:8080 weight=1;
    }
 
    server {
        listen       80;
        server_name  localhost 10.66.30.224;
		
        location ^~/api/exam {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://10.66.30.223:8080;
          client_max_body_size 10m;
        }
	   }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
           root   html;
        }

    }

}

    日志和pid目录要对应容器启动成功后的目录。

 

7.执行nginx容器启动命令:

  docker run --name nginx --link px-exam:px-exam -p 80:80  -d -v /usr/local/nginx_config/nginx.conf:/etc/nginx/nginx.conf  nginx

注意:px-exam为工程在tomcat部署后的容器名称,link后实现nginx和该tomcat连接,跳转到tomcat,容器之间互联可以使用映射后的主机ip:端口号,也可以是容器名称:容器端口 。

如:

docker安装mysql、tomcat、nginx,容器互联,应用部署_第2张图片

这里nginx配置使用的就是主机ip:端口号

也可以使用容器名称:容器端口 ,配置如下:

docker安装mysql、tomcat、nginx,容器互联,应用部署_第3张图片

如图所示实现nginx反向代理:

docker安装mysql、tomcat、nginx,容器互联,应用部署_第4张图片

 

  

你可能感兴趣的:(docker)