1. Docker Compose介绍与使用
1.1 Compose介绍
Compose是一个定义和管理多容器的工具,使用Python语言编写。使用Compose配置文件描述多个容器应用的架构,比如使用什么镜像、数据卷、网络、映射端口等;然后一条命令管理所有服务,比如启动、停止、重启等。
1.2 Compose安装
所需工具下载链接:https://pan.baidu.com/s/1o-Vkr0HM6coj3jRkqBG6ag
提取码:5qj7
curl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
或者
pip install docker-compose
示例:
[root@aliyun ~]# unzip docker-compose-linux-x86_64.zip
Archive: docker-compose-linux-x86_64.zip
inflating: docker-compose
[root@aliyun ~]# chmod +x docker-compose
[root@aliyun ~]# mv docker-compose /usr/bin/
1.3 YAML文件格式及编写注意事项
YAML是一种标记语言很直观的数据序列化格式,可读性高。类似于XML数据描述语言,语法比XML简单的很多。
YAML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。
YAML文件格式注意事项:
1.不支持制表符tab键缩进,需要使用空格缩进
2.通常开头缩进2个空格
3.字符后缩进1个空格,如冒号、逗号、横杆
4.用井号注释
5.如果包含特殊字符用单引号引起来
6.布尔值(true、false、yes、no、on、off)必须用引号括起来,这样分析器会将他们解释为字符串。
[root@aliyun ~]# unzip Compose.zip
[root@aliyun ~]# cd compose_lnmp/
[root@aliyun compose_lnmp]# cat docker-compose.yml
version: '3' ---版本号
services: ---定义服务的标签
nginx: ---服务名
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 81:80
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
mysql:
hostname: mysql
image: mysql:5.6
ports:
- 3306:3306
networks:
- lnmp
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
command: --character-set-server=utf8
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wordpress
MYSQL_USER: user
MYSQL_PASSWORD: user123
networks:
lnmp:
2. 一键部署LNMP网站平台
具体结构:
[root@aliyun compose_lnmp]# tree .
.
├── docker-compose.yml
├── mysql
│?? ├── conf
│?? │?? └── my.cnf
│?? └── data
├── nginx
│?? ├── Dockerfile
│?? ├── nginx-1.12.1.tar.gz
│?? └── nginx.conf
├── php
│?? ├── Dockerfile
│?? ├── php-5.6.31.tar.gz
│?? └── php.ini
└── wwwroot
└── index.php
6 directories, 9 files
---查看nginx配置---
[root@aliyun compose_lnmp]# ls
docker-compose.yml mysql nginx php wwwroot
[root@aliyun compose_lnmp]# cd nginx/
[root@aliyun nginx]# ls
Dockerfile nginx-1.12.1.tar.gz nginx.conf
[root@aliyun nginx]# cat Dockerfile
FROM centos:7
MAINTAINER www.aliangedu.com
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
ADD nginx-1.12.1.tar.gz /tmp
RUN cd /tmp/nginx-1.12.1 && \
./configure --prefix=/usr/local/nginx && \
make -j 2 && \
make install
RUN rm -rf /tmp/nginx-1.12.1* && yum clean all
COPY nginx.conf /usr/local/nginx/conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["./sbin/nginx", "-g", "daemon off;"]
---查看php配置---
[root@aliyun nginx]# cd ../php/
[root@aliyun php]# ls
Dockerfile php-5.6.31.tar.gz php.ini
[root@aliyun php]# cat Dockerfile
FROM centos:7
MAINTAINER www.aliangedu.com
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
ADD php-5.6.31.tar.gz /tmp/
RUN cd /tmp/php-5.6.31 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql --with-mysqli \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-iconv \
--enable-fpm --enable-zip --enable-mbstring && \
make -j 4 && \
make install && \
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.conf && \
sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf
COPY php.ini /usr/local/php/etc
RUN rm -rf /tmp/php-5.6.31* && yum clean all
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["./sbin/php-fpm", "-c", "/usr/local/php/etc/php-fpm.conf"]
[root@aliyun compose_lnmp]# ls
docker-compose.yml mysql nginx php wwwroot
[root@aliyun compose_lnmp]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a45aa24cbcb8 bridge bridge local
66ca00f90d0e host host local
e076ddacb9eb none null local
[root@aliyun compose_lnmp]# docker-compose -f docker-compose.yml up
[root@aliyun ~]# cd compose_lnmp/
[root@aliyun compose_lnmp]# docker-compose ps ---lnmp部署完成
Name Command State Ports
-------------------------------------------------------------------------------------
composelnmp_mysql_1 docker-entrypoint.sh --cha ... Up 0.0.0.0:3306->3306/tcp
composelnmp_nginx_1 ./sbin/nginx -g daemon off; Up 0.0.0.0:81->80/tcp
composelnmp_php_1 ./sbin/php-fpm -c /usr/loc ... Up 9000/tcp
[root@aliyun compose_lnmp]# docker-compose -f docker-compose.yml up -d ---放到后台登录
composelnmp_mysql_1 is up-to-date
composelnmp_nginx_1 is up-to-date
composelnmp_php_1 is up-to-date
安装wordpress
[root@aliyun wwwroot]# tar zxf wordpress-4.7.4-zh_CN.tar.gz
3. 一键部署Nginx反向代理Tomcat集群
nginx配置:
[root@aliyun ~]# cd compose_nginx_tomcat/
[root@aliyun compose_nginx_tomcat]# ls
docker-compose.yml mysql nginx tomcat webapps
[root@aliyun compose_nginx_tomcat]# tree *
docker-compose.yml [error opening dir]
mysql
├── conf
│ └── my.cnf
└── data
nginx
├── Dockerfile
├── nginx-1.12.1.tar.gz
└── nginx.conf
tomcat
├── apache-tomcat-8.0.46.tar.gz
├── Dockerfile
└── server.xml
webapps
└── ROOT
└── index.html
3 directories, 8 files
[root@aliyun compose_nginx_tomcat]# cat webapps/ROOT/index.html
Hello world!
[root@aliyun compose_nginx_tomcat]# cd nginx/
[root@aliyun nginx]# cat nginx.conf ---查看nginx的配置文件
user root;
worker_processes auto;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
}
http {
include 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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream www.example.com {
#ip_hash;
server tomcat01:8080;
server tomcat02:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://www.example.com;
}
location ~ \.(html|css|js|jpg|png|gif)$ {
root /opt/webapps/ROOT;
}
}
}
tomcat配置:
[root@aliyun nginx]# cd ../tomcat/
[root@aliyun tomcat]# ls
apache-tomcat-8.0.46.tar.gz Dockerfile server.xml
[root@aliyun tomcat]# cat Dockerfile ---查看tomcat的Dockerfile文件
FROM centos:7
MAINTAINER www.aliangedu.com
ENV JAVA_HOME /usr/local/jdk1.8.0_45
ADD apache-tomcat-8.0.46.tar.gz /usr/local
COPY server.xml /usr/local/apache-tomcat-8.0.46/conf
RUN sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/apache-tomcat-8.0.46/bin/catalina.sh
RUN rm -f /usr/local/*.tar.gz
WORKDIR /usr/local/apache-tomcat-8.0.46
EXPOSE 8080
ENTRYPOINT ["./bin/catalina.sh", "run"]
docker-compose.yml配置:
[root@aliyun tomcat]# cd ../
[root@aliyun compose_nginx_tomcat]# ls
docker-compose.yml mysql nginx tomcat webapps
[root@aliyun compose_nginx_tomcat]# cat docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 82:80
networks:
- lnmt
volumes:
- ./webapps:/opt/webapps
tomcat01:
hostname: tomcat01
build: ./tomcat
networks:
- lnmt
volumes:
- /usr/local/jdk1.8.0_45:/usr/local/jdk1.8.0_45
- ./webapps:/usr/local/apache-tomcat-8.0.46/webapps
tomcat02:
hostname: tomcat02
build: ./tomcat
networks:
- lnmt
volumes:
- /usr/local/jdk1.8.0_45:/usr/local/jdk1.8.0_45
- ./webapps:/usr/local/apache-tomcat-8.0.46/webapps
mysql:
hostname: mysql
image: mysql:5.6
ports:
- 3307:3306
networks:
- lnmt
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: user123
networks:
lnmt:
配置jdk:
[root@docker compose_nginx_tomcat]# ls ---上传jdk
docker-compose.yml jdk-8u45-linux-x64.tar.gz mysql nginx tomcat webapps
[root@docker compose_nginx_tomcat]# tar zxvf jdk-8u45-linux-x64.tar.gz ---解压
[root@docker compose_nginx_tomcat]# mv jdk1.8.0_45 /usr/local/
[root@docker compose_nginx_tomcat]# rm jdk-8u45-linux-x64.tar.gz ---删除安装包
rm: remove regular file ‘jdk-8u45-linux-x64.tar.gz’? y
[root@docker compose_nginx_tomcat]# docker-compose up ---构建
-------------省略------------
mysql_1 | 2020-06-01 14:21:08+00:00 [Note] [Entrypoint]: Stopping temporary server
mysql_1 | 2020-06-01 14:21:10+00:00 [Note] [Entrypoint]: Temporary server stopped
mysql_1 |
mysql_1 | 2020-06-01 14:21:10+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
mysql_1 |
mysql_1 | 2020-06-01 14:21:14 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_1 | 2020-06-01 14:21:14 0 [Note] mysqld (mysqld 5.6.48) starting as process 1 ...
[root@docker compose_nginx_tomcat]# docker-compose ps ---如下所示说明构建成功
Name Command State Ports
----------------------------------------------------------------------------------------------
compose_nginx_tomcat_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3307->3306/tcp
compose_nginx_tomcat_nginx_1 ./sbin/nginx -g daemon off; Up 0.0.0.0:82->80/tcp
compose_nginx_tomcat_tomcat01_1 ./bin/catalina.sh run Up 8080/tcp
compose_nginx_tomcat_tomcat02_1 ./bin/catalina.sh run Up 8080/tcp
mysql_1 | 2020-06-01 14:21:14 0 [Note] mysqld (mysqld 5.6.48) starting as process 1 ...
^CGracefully stopping... (press Ctrl+C again to force)
Stopping compose_nginx_tomcat_nginx_1 ... done
Stopping compose_nginx_tomcat_mysql_1 ... done
Stopping compose_nginx_tomcat_tomcat01_1 ... done
Stopping compose_nginx_tomcat_tomcat02_1 ... done
[root@docker compose_nginx_tomcat]# docker-compose up -d ---一般是改成后台执行
Starting compose_nginx_tomcat_nginx_1 ... done
Starting compose_nginx_tomcat_mysql_1 ... done
Starting compose_nginx_tomcat_tomcat01_1 ... done
Starting compose_nginx_tomcat_tomcat02_1 ... done
[root@docker compose_nginx_tomcat]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------
compose_nginx_tomcat_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3307->3306/tcp
compose_nginx_tomcat_nginx_1 ./sbin/nginx -g daemon off; Up 0.0.0.0:82->80/tcp
compose_nginx_tomcat_tomcat01_1 ./bin/catalina.sh run Up 8080/tcp
compose_nginx_tomcat_tomcat02_1 ./bin/catalina.sh run Up 8080/tcp