Docker
安装:
CentOS 6.x
# yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm
# yum install docker-io
CentOS 7.x
# yum install -y yum-utils
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
或
# yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
# yum install docker-ce
Debian
# apt-get install docker.io
** 说明: ** 如果需要非root用户运行docker的话,请将该用户增加到docker组中。
启动(run)
$ docker run -t -i ubuntu:14.04 /bin/bash
启用REST API (CentOS7)
修改文件 /usr/lib/systemd/system/docker.service
#ExecStart=/usr/bin/dockerd
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:5555 -H unix:///var/run/docker.sock
或者修改文件/etc/sysconfig/docker
#OPTIONS='--selinux-enabled -H=0.0.0.0:5555 --log-driver=journald --signature-verification=false'
OPTIONS='-H=unix:///var/run/docker.sock -H=0.0.0.0:5555 --log-driver=journald --signature-verification=false'
执行:
# systemctl daemon-reload
# systemctl restart docker
设置代理服务器
修改文件/usr/lib/systemd/system/docker.service,将:
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
修改为:
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin HTTP_PROXY=http://192.168.137.1:1080/
或(socks5)
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin HTTP_PROXY=socks5://192.168.137.1:1080/
然后重新启动docker服务:
# systemctl daemon-reload
# systemctl restart docker
# systemctl show --property=Environment docker
参考: https://docs.docker.com/config/daemon/systemd/
获取Image
从官方网站下载:
# docker pull ubuntu:12.04
或,从国内镜像下载:
# docker pull dl.dockerpool.com:5000/ubuntu:12.04
查看本机已经安装的镜像:
# docker images
自定义Image
如果即不能访问官方网站,也不能访问国内镜像,或者有其它原因需要从零开始创建自己的镜像的话,可按如下方式创建
定制CentOS镜像(centos65)
# export c_root=/home/chroot/centos65
# mkdir -p $c_root
# mkdir -p $c_root/var/lib/rpm
# rpm --root $c_root --initdb
# rpm --root $c_root -ivh /media/Packages/centos-release-6-5.el6.centos.11.1.x86_64.rpm
# yum --installroot=$c_root install yum rpm
# chroot $c_root /bin/bash
(如果需要进行其它配置的话,请在chroot中进行)
# tar -C $c_root -c . | docker import - lixf/centos65
定制Debian镜像(debian 8)
# export $c_root=/home/chroot/sid
# mkdir -p $c_root
# debootstrap --arch amd64 sid $c_root http://mirror.dlut.edu.cn/debian/
# chroot $c_root /bin/bash
(如果需要进行其它配置的话,请在chroot中进行)
# tar -C $c_root -c . | docker import - lixf/debian_sid
对于许多从官方网站下载的以dockfile形式提供的镜像(zip包)往往会依赖scratch,可通过如下方式先创建scratch
创建scratch(空)镜像
# mkdir scratch
# tar -C scratch -c . | docker import - scratch
然后就可以根据下载的dockfile建立镜像
# docker build
垃圾回收
Cleanup exited processes:
docker rm $(docker ps -q -f status=exited)
Cleanup dangling volumes:
docker volume rm $(docker volume ls -qf dangling=true)
Cleanup dangling images:
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
容器跨主机通信(macvlan)
*第1台服务上执行:
docker network create -d macvlan --subnet=10.17.0.0/16 --ip-range=10.17.211.0/24 --gateway=10.17.211.254 -o parent=eth0 macv0
docker run --network macv0 ...
*第2台服务上执行:
docker network create -d macvlan --subnet=10.17.0.0/16 --ip-range=10.17.212.0/24 --gateway=10.17.212.254 -o parent=eth0 macv0
docker run --network macv0 ...
两台主机上的容器通过macv0连接。
设置ulimits参数
修改文件/etc/docker/daemon.json,如下:
{
...
"default-ulimits": {
"nofile": {
"name": "nofile",
"hard": 655350,
"soft": 655350
},
"nproc": {
"name": "nproc",
"hard": 655350,
"soft": 655350
},
"stack": {
"name": "stack",
"hard": -1,
"soft": -1
},
"memlock": {
"name": "memlock",
"hard": -1,
"soft": -1
}
}
}
Misc
change runtime & cgroup driver, 修改文件:docker.service
ExecStart=/usr/bin/dockerd-current \
--add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
--default-runtime=docker-runc \
--exec-opt native.cgroupdriver=systemd \
--userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
$OPTIONS \
$DOCKER_STORAGE_OPTIONS \
$DOCKER_NETWORK_OPTIONS \
$ADD_REGISTRY \
$BLOCK_REGISTRY \
$INSECURE_REGISTRY
set storage from per-container
$ docker run -it --storage-opt size=120G fedora /bin/bash