docker基础

docker越来越流行,这里介绍一点关于docker的基础知识。

Docker基础篇

  • [x] docker简介
  • [x] 安装docker服务

docker简介

Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。-- 百度百科

docker_vs_vm

上图来自docker官网截图,整体上说明了传统的虚拟机与docker的区别,传统的虚拟机包括应用程序,必要的二进制文件和库文件以及一个完整的操作系统,这样一个虚拟机文件会有上GB的大小。而docker容器则包含应用程序以及依赖包,但容器间共享操作系统的kernel,在宿主机上以一个进程体现一个docker容器,各个容器间是相互隔离的,容器不依赖任何特定的基础设施,这样方便于迁移。

已下所有操作基于Debain 8 x 64

docker服务器需要安装包:

dialog

perl-base

apt-utils

安装docker服务

先删除旧包

1
2
root@haproxy-01:~# apt-get purge lxc-docker*
root@haproxy-01:~# apt-get purge docker.io*

再使apt能工作在https方式及安装CA证书,如下

1
2
root@haproxy-01:~# apt-get update
root@haproxy-01:~# apt-get install apt-transport-https ca-certificates

增加新的GPG key

1
root@haproxy-01:~# apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

根据系统的发行版本增加相应的docker源,我这里使用的是Debian Jessie

1
2
3
root@haproxy-01:~#  vim /etc/apt/sources.list.d/docker.list  #增加下边的地址
deb https://apt.dockerproject.org/repo debian-jessie main
root@haproxy-01:~# apt-get update

可用以下命令验证docker-engine软件将会从哪个存储库下载及相应软件版本的列表

1
root@haproxy-01:~# apt-cache policy docker-engine

安装docker-engine,这里安装目前最新的版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@haproxy-01:~# apt-get install docker-engine=1.11.2-0~jessie
root@haproxy-01:~# docker version
Client:
Version: 1.11.2
API version: 1.23
Go version: go1.5.4
Git commit: b9f10c9
Built: Wed Jun 1 21:23:39 2016
OS/Arch: linux/amd64

Server:
Version: 1.11.2
API version: 1.23
Go version: go1.5.4
Git commit: b9f10c9
Built: Wed Jun 1 21:23:39 2016
OS/Arch: linux/amd64

以上操作可以用如下脚本完成:

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
#!/bin/bash
#Program: install_docker.sh
#Author: Neal
#E_mail: sky_551@163.com
#Date: 2016-06-16
#Version 1.0
# for debina jessie x64

version=1.11.2-0~jessie

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH


# del old pkg
apt-get purge lxc-docker*
apt-get purge docker.io*

# install apt-transport-https ca-certificates
apt-get update
apt-get install apt-transport-https ca-certificates

# add GPG key
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

# add docker.list for Debian Jessie
rm -f /etc/apt/sources.list.d/*
cat <<- EOF >> /etc/apt/sources.list.d/docker.list
deb https://apt.dockerproject.org/repo debian-jessie main
EOF
apt-get update

# install docker-engine
apt-get install docker-engine=${version}

docker version

在docker中进入一个已运行的容器

  • 首先确保系统已安装有nsenter命令,此命令由util-linux包安装得来,安装命令为apt-get install util-linux

  • 先得到container id

1
2
3
root@haproxy-01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d924adae5ff6 nginx:latest "nginx -g 'daemon of 18 hours ago Up 18 hours 80/tcp, 443/tcp r-WEB-Server_Nginx-cluster_1
  • 计算出指定container id容器的PID
1
2
root@haproxy-01:~# docker inspect --format {{.State.Pid}} d924adae5ff6
20272
  • 进入到容器中
1
2
root@haproxy-01:~# nsenter --target 20272 --mount --uts --ipc --net --pid
root@d924adae5ff6:/#

上边的命令似乎过于复杂,有前辈给我们准备好了便捷的工具,脚本如下:

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
# Some useful commands to use docker.
# Author: yeasy@github
# Created:2014-09-25

alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'"
alias docker-ip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'"

#the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter
function docker-enter() {
#if [ -e $(dirname "$0")/nsenter ]; then
#Change for centos bash running
if [ -e $(dirname '$0')/nsenter ]; then
# with boot2docker, nsenter is not in the PATH but it is in the same folder
NSENTER=$(dirname "$0")/nsenter
else
# if nsenter has already been installed with path notified, here will be clarified
NSENTER=$(which nsenter)
#NSENTER=nsenter
fi
[ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return

if [ -z "$1" ]; then
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
echo ""
echo "Enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
else
PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1")
if [ -z "$PID" ]; then
echo "WARN Cannot find the given container"
return
fi
shift

OPTS="--target $PID --mount --uts --ipc --net --pid"

if [ -z "$1" ]; then
# No command given.
# Use su to clear all host environment variables except for TERM,
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
# and start a login shell.
#sudo $NSENTER "$OPTS" su - root
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root
else
# Use env to clear all host environment variables.
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@
fi
fi
}

把上边的脚本内容保存在用户家目录下,文件名为.bashrc_docker,或直接运行wget -P ~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker命令把此脚本文件下载到用户家目录下,再运行如下命令:

1
echo "[ -f ~/.bashrc_docker ] && source ~/.bashrc_docker" >> ~/.bashrc; source ~/.bashrc

这样用户每次登陆时都会执行.bashrc_docker文件,此工具使用:

1
2
3
root@cst004:~# docker-pid <container>   # docker-pid 跟上容器的name即可获得容器的pid
root@cst004:~# docker-enter <container> # 直接进入容器
root@cst004:~# docker-enter <container> COMMAND # 直接进入容器执行一个命令
文章目录
  1. 1. Docker基础篇
    1. 1.1. docker简介
    2. 1.2. 安装docker服务
|