Docker 项目打包部署


Docker Compose

介绍

  1. Compose是一个定义和管理多容器的工具,使用Python语言编写。
  2. 使用Compose配置文件描述多个容器应用的架构,比如使用什么镜像、数据卷、网络、映射端口等;
  3. 然后一条命令管理所有服务,比如启动、停止、重启等。

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装 docker compose 单机编排
https://docs.docker.com/compose/install/
# 下载
curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 增加执行权限
[root@Docker opt]# chmod +x /usr/local/bin/docker-compose

# 测试
[root@Docker opt]# docker-compose version
docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018
1
2
yum install pip
pip install docker-compose
  1. 定义容器环境
  2. 根据yanml文件启动容器

YAML文件格式及编写注意事项

YAML是一种标记语言很直观的数据序列化格式,可读性高。类似于XML数据描述语言,语法比XML简单的很多。
YAML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。
YAML文件格式注意事项:

  1. 不支持制表符tab键缩进,需要使用空格缩进
  2. 通常开头缩进2个空格
  3. 字符后缩进1个空格,如冒号、逗号、横杆
  4. 用井号注释
  5. 如果包含特殊字符用单引号引起来
  6. 布尔值(true、false、yes、no、on、off)必须用引号括起来,这样分析器会将他们解释为字符串。

配置文件常用字段

1
官方文档:https://docs.docker.com/compose/compose-file

常用命令

Docker Compose 一键部署 LNMP 网站平台

dockerfile nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@Docker-2 nginx]# mkdir -p /opt/compose_lnmp/nginx/
[root@Docker-2 nginx]# cd /opt/compose_lnmp/nginx/

[root@Docker-2 nginx]# vim Dockerfile

# BASE
FROM centos:7

# WHO
MAINTAINER leo 365042337@qq.com

# YUM-REPO
COPY nginx.repo /etc/yum.repos.d/

# PKG
RUN cd /etc/yum.repos.d;mkdir bak;mv *.repo bak/ && \
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum install pcre-devel zlib-devel openssl-devel autoconf nginx -y && \
echo "==> Clean up..." && \
yum clean all && \
rm -rf /var/cache/yum/*

# nginx-conf
COPY default.conf /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

# PROT
EXPOSE 80

# CMD
CMD ["nginx", "-g", "daemon off;"]
1
2
3
4
5
6
7
8
[root@Docker-2 compose_lnmp]# tree nginx/
nginx/
├── CentOS-Base.repo
├── default.conf
├── Dockerfile
├── epel.repo
├── nginx.conf
└── nginx.repo
1
2
3
[root@Docker-2 compose_lnmp]# tree wwwroot/
wwwroot/
└── index.html

dockerfile php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@Docker-2 php]# vim Dockerfile 

# BASE
FROM centos:7

# WHO
MAINTAINER leo 365042337@qq.com

# YUM
RUN cd /etc/yum.repos.d;mkdir bak;mv *.repo bak/ && \
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm && \
yum -y install php72w php72w-cli php72w-common php72w-devel \
php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache && \
echo "==> Clean up..." && \
yum clean all && \
rm -rf /var/cache/yum/*

RUN useradd nginx

# CONF
COPY php.ini /etc/
COPY php-fpm.conf /etc/
COPY www.conf /etc/php-fpm.d/

# PROT
EXPOSE 9000

# CMD
CMD ["php-fpm"]
1
2
3
4
5
6
[root@Docker-2 compose_lnmp]# tree  php
php
├── Dockerfile
├── php-fpm.conf
├── php.ini
└── www.conf
1
2
3
4
[root@Docker-2 compose_lnmp]# tree wwwroot/
wwwroot/
├── index.html
└── test.php

mysql

  1. mysql 使用官方镜像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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: 123456

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@Docker-2 compose_lnmp]# vim docker-compose.yml 

version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 88:80
networks:
- lnmp
volumes:
- ./wwwroot:/wwwroot

php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
networks:
- lnmp
volumes:
- ./wwwroot:/wwwroot

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: 123456

networks:
lnmp:

编排

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@Docker-2 compose_lnmp]# docker-compose -f docker-compose.yml up -d
Creating compose_lnmp_mysql_1 ... done
Creating compose_lnmp_nginx_1 ... done
Creating compose_lnmp_php_1 ... done

[root@Docker-2 compose_lnmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b0d94961312 compose_lnmp_php "php-fpm" 5 minutes ago Up 5 minutes 9000/tcp compose_lnmp_php_1
246c13ae9fef compose_lnmp_nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:88->80/tcp compose_lnmp_nginx_1
ba5bee314099 mysql:5.6 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp compose_lnmp_mysql_1

[root@Docker-2 compose_lnmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_lnmp_php latest d14d23830b03 5 minutes ago 371MB
compose_lnmp_nginx latest 372095ee6c06 12 minutes ago 310MB
mysql 5.6 b3983abaa3fe 2 weeks ago 302MB

部署 wordpress

  1. 上传 wordpress-4.7.4-zh_CN.tar
1
[root@Docker-2 wwwroot]# tar -zxvf wordpress-4.9.4-zh_CN.tar.gz
  1. 配置
1
2
http://39.106.100.108:88/wordpress/
# 数据库为容器hostname

部署 ngixn + tomcat 集群

dockerfile tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@Docker-2 tomcat]# vim Dockerfile 

# BASE
FROM centos:7

# WHO
MAINTAINER leo 365042337@qq.com

# tomcat
COPY apache-tomcat-8.5.47.tar.gz /opt/

RUN yum install java-1.8.0-openjdk wget curl unzip net-tools -y && \
yum clean all && \
rm -rf /var/cache/yum/* && \
cd /opt/ && \
tar zxf apache-tomcat-8.5.47.tar.gz -C /usr/local/ && \
mv /usr/local/apache-tomcat-8.5.47 /usr/local/tomcat && \
rm -rf apache-tomcat-8.5.47.tar.gz /usr/local/tomcat/webapps/* && \
sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh

ENV PATH $PATH:/usr/local/tomcat/bin

WORKDIR /usr/local/tomcat

EXPOSE 8080

CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
1
2
3
4
[root@Docker-2 compose_nginx_tomcat]# tree tomcat/
tomcat/
├── apache-tomcat-8.5.47.tar.gz
└── Dockerfile

nginx 动静分离

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@Docker-2 nginx]# vim Dockerfile 

# BASE
FROM centos:7

# WHO
MAINTAINER leo 365042337@qq.com

# YUM-REPO
COPY nginx.repo /etc/yum.repos.d/

# PKG
RUN cd /etc/yum.repos.d;mkdir bak;mv *.repo bak/ && \
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum install pcre-devel zlib-devel openssl-devel autoconf nginx -y && \
echo "==> Clean up..." && \
yum clean all && \
rm -rf /var/cache/yum/*

# nginx-conf
COPY default.conf /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
COPY proxy_params /etc/nginx/proxy_params

# PROT
EXPOSE 80

# CMD
CMD ["nginx", "-g", "daemon off;"]
1
2
3
4
5
6
7
8
9
[root@Docker-2 compose_nginx_tomcat]# tree nginx/
nginx/
├── CentOS-Base.repo
├── default.conf
├── Dockerfile
├── epel.repo
├── nginx.conf
├── nginx.repo
└── proxy_params

mysql

1
2
3
4
5
[root@Docker-2 compose_nginx_tomcat]# tree mysql/
mysql/
├── conf
│   └── my.cnf
└── data

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@Docker-2 compose_nginx_tomcat]# vim docker-compose.yml 

version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 88:80
networks:
- lnmt
volumes:
- ./www:/opt/www

tomcat01:
hostname: tomcat01
build: ./tomcat
networks:
- lnmt
volumes:
- ./app:/usr/local/tomcat/webapps

tomcat02:
hostname: tomcat02
build: ./tomcat
networks:
- lnmt
volumes:
- ./app02:/usr/local/tomcat/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:

目录结构 与 动态静态文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@Docker-2 opt]# tree compose_nginx_tomcat/
compose_nginx_tomcat/
├── app
│   └── test
│   └── java_test.jsp
├── app02
│   └── test
│   └── java_test.jsp
├── docker-compose.yml
├── mysql
│   ├── conf
│   │   └── my.cnf
│   └── data
├── nginx
│   ├── CentOS-Base.repo
│   ├── default.conf
│   ├── Dockerfile
│   ├── epel.repo
│   ├── nginx.conf
│   ├── nginx.repo
│   └── proxy_params
├── tomcat
│   ├── apache-tomcat-8.5.47.tar.gz
│   └── Dockerfile
└── www
├── images
│   └── nginx.png
├── index.html
└── mysite.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@Docker-2 test]# cat java_test.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP Test Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>Random number from tomcat01:</h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@Docker-2 www]# cat mysite.html 
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://39.106.100.108:88/test/java_test.jsp",
success: function(data) {
$("#get_data").html(data)
},
error: function() {
alert("fail!!,请刷新再试!");
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src="http://39.106.100.108:88/images/nginx.png">
<div id="get_data"></div>
</body>
</html>
1
[root@proxy ~]# wget -O /tmp/nginx.png http://nginx.org/nginx.png

编排与测试

1
2
3
4
5
6
[root@Docker-2 compose_nginx_tomcat]# docker-compose -f docker-compose.yml up -d
Creating network "compose_nginx_tomcat_lnmt" with the default driver
Creating compose_nginx_tomcat_tomcat02_1 ... done
Creating compose_nginx_tomcat_mysql_1 ... done
Creating compose_nginx_tomcat_tomcat01_1 ... done
Creating compose_nginx_tomcat_nginx_1 ... done
1
2
3
4
5
6
7
[root@Docker-2 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 nginx -g daemon off; Up 0.0.0.0:88->80/tcp
compose_nginx_tomcat_tomcat01_1 /usr/local/tomcat/bin/cata ... Up 8080/tcp
compose_nginx_tomcat_tomcat02_1 /usr/local/tomcat/bin/cata ... Up 8080/tcp
1
2
3
4
5
6
7
8
9
10
11
12
# docker-compose 命令 一定要进入 项目的目录里去执行

[root@Docker-2 compose_nginx_tomcat]# docker-compose down
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
Removing compose_nginx_tomcat_nginx_1 ... done
Removing compose_nginx_tomcat_mysql_1 ... done
Removing compose_nginx_tomcat_tomcat01_1 ... done
Removing compose_nginx_tomcat_tomcat02_1 ... done
Removing network compose_nginx_tomcat_lnmt

1
# 刷新整合页面测试 两台tomcat轮询显示