5. Docker Compose 安装使用
概况
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.
Using Compose is basically a three-step process.
- Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
- Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
- Lastly, run docker-compose up and Compose will start and run your entire app.
Docker Compose 是定义和运行多容器 Docker 应用的工具。何谓多容器?Gogs 应用需要gogs 和 mysql这两个容器。如果没有Compose 工具,就必须分别安装好Mysql镜像和Gogs镜像,再分别启动。Compose 工具提供了一种能力,一个命令就可以创建和启动Gogs 应用
参考网页: Docker Compose Overview
Docker Compose 的安装
前置条件
- 桌面操作系统: Win 10 64位家庭中文版
- 虚拟机软件 : Oracle VirtualBox 5.1.6
- 虚拟机操作系统 : CentOS Linux release 7.2.1511 (Core)
- Linux 内核 : 3.10.0-327.36.2.el7.x86_64
- docker 安装版本:docker 1.12.2
安装过程
参考页面: Compose Install
- 下载 docker-compose 并安装
[[email protected] ~]# curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 600 0 600 0 0 363 0 --:--:-- 0:00:01 --:--:-- 363
0 7798k 0 69186 0 0 304 0 7:17:50 0:03:47 7:14:03 0
[[email protected] ~]# chmod +x /usr/local/bin/docker-compose
[[email protected] ~]# ll /usr/local/bin/docker-compose
-rwxr-xr-x. 1 root root 7986086 10月 19 13:04 /usr/local/bin/docker-compose
[[email protected] ~]# docker-compose --version
docker-compose version 1.8.1, build 878cff1
- 编写Docker Compose 服务配置文件 yml
[[email protected] ~]# mkdir compose
[[email protected] ~]# cd compose
[[email protected] compose]# vi docker-compose.yml
# mysql service
mydb:
image: mysql:latest
ports:
- "13307:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- /opt/mydata2/:/var/lib/mysql
# gogs service
mygogs:
image: gogs/gogs:latest
ports:
- "10033:22"
- "10090:3000"
volumes:
- /var/gogs2/:/data
links:
- mydb
[[email protected] compose]# docker-compose up
- 使用Compose 启动应用
[[email protected] compose]# docker-compose up
[[email protected] compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07a5e03d6353 gogs/gogs:latest "docker/start.sh /bin" 4 minutes ago Up 4 minutes 0.0.0.0:10033->22/tcp, 0.0.0.0:10090->3000/tcp compose_mygogs_1
fd10f315ad68 mysql:latest "docker-entrypoint.sh" 4 minutes ago Up 4 minutes 0.0.0.0:13307->3306/tcp compose_mydb_1
说明:
- 默认情况,
docker-compose
会找当前目录下的docker-compose.yml
文件,工程名使用目录名 - 所以
docker ps
看到的容器名称是[工程名]_[服务名]_1
- docker-compose 命令参数
[[email protected] compose]# mv docker-compose.yml gogs_app.yml
[[email protected] compose]# ls
gogs_app.yml
[[email protected] compose]# docker-compose -f gogs_app.yml -p GogsApp up -d
Creating gogsapp_mydb_1
Creating gogsapp_mygogs_1
[[email protected] compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5c85165cb29 gogs/gogs:latest "docker/start.sh /bin" About a minute ago Up About a minute 0.0.0.0:10033->22/tcp, 0.0.0.0:10090->3000/tcp gogsapp_mygogs_1
43e0a6d11c5a mysql:latest "docker-entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:13307->3306/tcp gogsapp_mydb_1
命令 docker-compose -f gogs_app.yml -p GogsApp up -d
说明如下:
-
-f gogs_app.yml
指定读取的 yml 文件 -
-p GogsApp
指定工程名 -
-d
指定是docker-compose 以后台进程的方式启动
- Compose 配置文件格式有两个版本,推荐使用Version 2.0
here are two versions of the Compose file format:
- Version 1, the legacy format. This is specified by omitting a version key at the root of the YAML.
- Version 2, the recommended format. This is specified with a version: '2' entry at the root of the YAML.
我们把上述例子修改成版本2.0,然后演示了启动应用、列出容器、停止容器、启动容器、卸载应用
[[email protected] compose]# vi gogs_app.yml
# compose demo
version: '2'
services:
# mysql service
mydb:
image: mysql:latest
ports:
- "13307:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- /opt/mydata2/:/var/lib/mysql
# gogs service
mygogs:
image: gogs/gogs:latest
ports:
- "10033:22"
- "10090:3000"
volumes:
- /var/gogs2/:/data
links:
- mydb
[[email protected] compose]# docker-compose -f gogs_app.yml -p gapp up -d
Creating network "gapp_default" with the default driver
Creating gapp_mydb_1
Creating gapp_mygogs_1
[[email protected] compose]# docker-compose -f gogs_app.yml -p gapp ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------
gapp_mydb_1 docker-entrypoint.sh mysqld Up 0.0.0.0:13307->3306/tcp
gapp_mygogs_1 docker/start.sh /bin/s6-sv ... Up 0.0.0.0:10033->22/tcp, 0.0.0.0:10090->3000/tcp
[[email protected] compose]# docker-compose -f gogs_app.yml -p gapp stop
Stopping gapp_mygogs_1 ... done
Stopping gapp_mydb_1 ... done
[[email protected] compose]# docker-compose -f gogs_app.yml -p gapp start
Starting mydb ... done
Starting mygogs ... done
[[email protected] compose]# docker-compose -f gogs_app.yml -p gapp down
Stopping gapp_mygogs_1 ... done
Stopping gapp_mydb_1 ... done
Removing gapp_mygogs_1 ... done
Removing gapp_mydb_1 ... done
Removing network gapp_default
参考
上一篇: 5.使用bind() 和 connect() 函数
下一篇: 5. 使用通配符进行过滤(%,_)
推荐阅读
-
5. Docker Compose 安装使用
-
VATIC 视频标注工具安装与使用(非 docker)—— 2020-01-27 实现安装
-
基于docker 的 elasticstrach安装和基本使用
-
docker的基本使用,安装和常用命令
-
docker的安装和基本的使用
-
docker-compose安装&创建容器1
-
CentOS 7 上安装使用 Docker 博客分类: linux
-
CentOS 7 上安装使用 Docker 博客分类: linux
-
【赵强老师】使用Docker Compose进行服务编排 博客分类: 云计算容器虚拟化Kubernetesk8s 云计算容器虚拟化Kubernetesk8s
-
docker compose linux tomcat 安装(多容器docker) 博客分类: linuxdockercompose dockercomposelinuxtomcat安装