本环境是基于 Centos 7.8 系统构建Docker-19.03.13环境
具体构建,请参考 构建Docker-19.03.13
- Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用。
- Compose 定位是“defining and running complex applications with Docker”,前身是 Fig,兼容 Fig 的模板文件。较新的版本已经淘汰 .fig 文件,开始使用yaml格式的文件
- Dockerfile 可以让用户管理一个单独的应用容器;而 Compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器等。
- Docker Compose 目前代码是托管在Github 平台上的
- 该项目由 Python 编写,实际上调用了 Docker 提供的 API 来实现。
#默认安装最新版本
[root@node01 ~]# install -U docker-compose
#配置命docker-compose 令补全
[root@node01 ~]# curl -L https://raw.githubusercontent.com/docker/compose/1.27.4/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
步骤:
1、下载二进制包,链接: https://github.com/docker/compose/releases
2、下载后直接放到执行路径即可。
上传安装包
[root@node01 bin]# cd /usr/local/bin/
[root@node01 bin]# ll
total 11936
-rw-r--r--. 1 root root 12218968 Nov 7 10:59 docker-compose
[root@node01 bin]# chmod a+x docker-compose
[root@node01 bin]# ll
total 11936
-rwxr-xr-x. 1 root root 12218968 Nov 7 10:59 docker-compose
查看安装docker-compose 版本信息
[root@node01 bin]# docker-compose --version
docker-compose version 1.27.4, build 40524192
$ docker-compose -h
Fast, isolated development environments using Docker.
Usage:
docker-compose [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
--verbose Show more output
--version Print version and exit
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
Commands:
build Build or rebuild services
help Get help on a command
kill Kill containers
logs View output from containers
port Print the public port for a port binding
ps List containers
pull Pulls service images
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
restart Restart services
up Create and start containers
编写 docker-compose 文件
[root@node01 ~]# mkdir docker-compose
[root@node01 ~]# cd docker-compose/
[root@node01 docker-compose]# vim docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root@wordoress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
restart: always
volumes:
- wp_site:/var/www/html
ports:
- '8888:80'
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data:
wp_site:
启动 docker-compose
[root@node01 docker-compose]# docker-compose up -d
查看容器运行
[root@node01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5612704e2c97 wordpress:latest "docker-entrypoint.s…" 29 seconds ago Up 19 seconds 0.0.0.0:8888->80/tcp docker-compose_wordpress_1
1d76033331a6 mysql:5.7 "docker-entrypoint.s…" 36 seconds ago Up 29 seconds 3306/tcp, 33060/tcp docker-compose_db_1
浏览器登录,进一步安装 wordpress
http:// 192.168.5.11:8888
点击,继续
编写 docker-compose 文件
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root@wordoress
MYSQL_DATABASE: owncloud
MYSQL_USER: owncloud
MYSQL_PASSWORD: owncloud
owncloud:
depends_on:
- db
image: owncloud:latest
restart: always
volumes:
- wp_site:/var/www/html
ports:
- '8989:80'
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: owncloud
WORDPRESS_DB_PASSWORD: owncloud
WORDPRESS_DB_NAME: owncloud
volumes:
db_data:
wp_site:
启动 docker-compose
[root@node01 docker-compose]# docker-compose up -d
查看容器运行
[root@node01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e7588046a04 owncloud:latest "docker-entrypoint.s…" 29 seconds ago Up 27 seconds 0.0.0.0:8989->80/tcp owncloud_owncloud_1
c07dad8e294c mysql:5.7 "docker-entrypoint.s…" 34 seconds ago Up 29 seconds 3306/tcp, 33060/tcp owncloud_db_1
5612704e2c97 wordpress:latest "docker-entrypoint.s…" 28 minutes ago Up 28 minutes 0.0.0.0:8888->80/tcp docker-compose_wordpress_1
1d76033331a6 mysql:5.7 "docker-entrypoint.s…" 29 minutes ago Up 28 minutes 3306/tcp, 33060/tcp docker-compose_db_1
浏览器登录,进一步安装 owncloud
设置管理信息,进行安装
登录 owncloud
编写 dockerfile 文件
#创建目录
[root@node01 ~]# mkdir /compose_nginx/nginx/ -p
#上传nginx源码包
rz
[root@node01 nginx]# ls
nginx-1.12.1.tar.gz
#编写dockerfile文件
[root@node01 nginx]# vim Dockerfile
FROM centos:7
RUN yum -y install pcre-devel zlib-devel make gcc gcc-c++
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.1.tar.gz /opt/
WORKDIR /opt/nginx-1.12.1
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
WORKDIR /root/nginx
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
提供nginx启动的配置文件
[root@node01 nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
编写docker-compose文件
[root@node01 compose_nginx]# vim docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 8908:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
提供nginx网页文件
[root@node01 compose_nginx]# mkdir wwwroot
[root@node01 compose_nginx]# echo "This is nginx test page." > wwwroot/index.html
启动dockercompose
[root@node01 compose_nginx]# docker-compose up -d
查案容器运行情况
[root@node01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5019d145057c compose_nginx_nginx "/run.sh" 11 minutes ago Up 11 minutes 0.0.0.0:8908->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
5e7588046a04 owncloud:latest "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:8989->80/tcp owncloud_owncloud_1
c07dad8e294c mysql:5.7 "docker-entrypoint.s…" About an hour ago Up About an hour 3306/tcp, 33060/tcp owncloud_db_1
5612704e2c97 wordpress:latest "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp docker-compose_wordpress_1
1d76033331a6 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp, 33060/tcp docker-compose_db_1
浏览器测试