1. 简介
Compose 是 docker 提供的一个命令行工具,用来定义和运行由多个容器组成的应用。
使用 compose,我们可以通过 YAML 文件声明式的定义应用程序的各个服务,并由单个命令完成应用的创建和启动。
Compose 的使用方式非常简单,基本上就是下面的三板斧:
- 定义 Dockerfile
- 定义 docker-compose.yml
-
运行 docker-compose up
其实 compose 提供的命令可以管理应用的整个生命周期:
- Start, stop, rebuild services
- 查看运行中 service 的状态
- 输出运行中 service 的日志
- 在 service 中执行一次性的命令
2. 安装Docker compose
安装 Docker Compose 前请先在本地安装 Docker
安装dockercompose的方式有两种,curl和pip两种,本文直接附上安装脚本,脚本直接安装docker和dockercompose
#!/bin/bash
DEBIAN=$(uname -v |grep -o Debian)
CENTOS=$( uname -r |grep -o el7)
read -p "please enter hostname: " hostname
hostnamectl set-hostname $hostname
#debian 相关函数
DEBIAN_SOURCE() {
rm -rf /etc/apt/sources.list
cat > /etc/apt/sources.list << TEST_END
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
TEST_END
apt-get update
}
DEBIAN_BASHRC() {
mv /root/.bashrc /root/.bashrc.bak
cat > /root/.bashrc << 'BASH_END'
# ~/.bashrc: executed by bash(1) for non-login shells.
# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022
# You may uncomment the following lines if you want `ls' to be colorized:
export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
if [ "$SSH_CONNECTION" != '' -a "$TERM" != 'linux' ]; then
declare -a HOSTIP
HOSTIP=`echo $SSH_CONNECTION |awk '{print $3}'`
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@$HOSTIP:[${HOSTNAME%%.*}]:${PWD/#$HOME/~} \007"'
fi
BASH_END
cat > /root/.vimrc << 'VIMRC_END'
syntax on
set mouse-=a
set nonu
set nocursorline
set noautoindent
set nosmartindent
set nocindent
set ignorecase
set laststatus=2
set ruler
set hlsearch
syntax enable
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set ts=2
set expandtab
%retab!
VIMRC_END
}
#centos 相关函数
CENTOS_bashrc() {
mv /root/.bashrc /root/.bashrc.bak
cat > /root/.bashrc << 'YUM_END'
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
if [ "$SSH_CONNECTION" != '' -a "$TERM" != 'linux' ]; then
declare -a HOSTIP
HOSTIP=`echo $SSH_CONNECTION |awk '{print $3}'`
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@$HOSTIP:[${HOSTNAME%%.*}]:${PWD/#$HOME/~} \007"'
fi
YUM_END
cat > /root/.vimrc << 'VIMRC_END'
syntax on
set mouse-=a
set nonu
set nocursorline
set noautoindent
set nosmartindent
set nocindent
set ignorecase
set laststatus=2
set ruler
set hlsearch
syntax enable
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set ts=2
set expandtab
%retab!
VIMRC_END
}
CENTOS_INSTALL() {
yum install wget
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum install -y yum-utils device-mapper-persistent-data lvm2 ntp ntpdate smartmontools \
psmisc net-tools unzip zip lm-sensors rsync dnsutils sysstat tcpdump \
netcat-traditional vim curl bash-argsparse bash-completion bash-completion-extras
}
#开始安装
if [ $DEBIAN ];
then
# step 1: 安装必要的一些系统工具
DEBIAN_SOURCE #apt源
DEBIAN_BASHRC #环境变量
apt-get install -y napt-transport-https ca-certificates curl software-properties-commo \
ntpdate smartmontools psmisc net-tools unzip zip lm-sensors rsync wget \
dnsutils sysstat tcpdump netcat-traditional vim ntp curl sudo
sed -i '[email protected]_CMDLINE_LINUX="@GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1 @' /etc/default/grub
update-grub
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
apt-get -y update
apt-get -y install docker-ce
elif [ $CENTOS ];
then
# step 1: 安装必要的一些系统工具
CENTOS_bashrc #环境变量
CENTOS_INSTALL #yum源,系统工具安装
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
sed -i '[email protected]_CMDLINE_LINUX="@GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1 @' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
# Step 2: 添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
yum makecache fast
yum -y install docker-ce
# Step 4: 开启Docker服务
service docker start
systemctl enable docker
else
echo "unkown release"
exit 1
fi
if [ -f /usr/lib/systemd/system/docker.service ]
then
#镜像资源加速
#curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | bash -s http://89357acc.m.daocloud.io
#私有仓库认证
#sed -i '[email protected]=/usr/bin/[email protected]=/usr/bin/dockerd --insecure-registry 192.168.0.229:8888 --insecure-registry harbor.neunb.rd @' /usr/lib/systemd/system/docker.service
#systemctl daemon-reload
#systemctl restart docker
wget -O /usr/bin/docker-compose https://github.com/docker/compose/releases/download/1.22.0/docker-compose-Linux-x86_64
chmod +x /usr/bin/docker-compose
fi
3. 查看版本
docker-compose --version