天天看點

Docker compose---安裝1. 簡介2. 安裝Docker compose3. 檢視版本

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