Docker

Docker
https://github.com/docker/docker
https://www.docker.com/products/docker
https://download.docker.com/mac/stable/Docker.dmg
https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe
https://docs.docker.com/engine/installation/linux/
https://docs.docker.com/engine/reference/builder/
Kitematic(图形界面工具)
https://download.docker.com/kitematic/Kitematic-Mac.zip
https://download.docker.com/kitematic/Kitematic-Windows.zip

环境信息
$ docker version
Client:
 Version:      1.12.0
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   8eab29e
 Built:        Thu Jul 28 21:15:28 2016
 OS/Arch:      darwin/amd64

Server:
 Version:      1.12.0
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   8eab29e
 Built:        Thu Jul 28 21:15:28 2016
 OS/Arch:      linux/amd64

详细信息
$ docker info

下载最新ubuntu镜像
$ docker pull ubuntu:latest

容器:交互式容器(-it)、守护式容器(-d)、自动重启容器(--restart=always|on-failure:NUM)

1)交互式容器(--name 容器命名,-i 开启STDIN,-t 伪tty终端,-rm 退出时自动删除此容器)
$ docker run --name ubuntu_bash -it ubuntu /bin/bash
安装vim
# apt-get update && apt-get install vim
# exit

查看容器列表(-a 也显示未运行的容器,-l 显示最近创建的容器,-q 只列出容器ID)
$ docker ps -a

重启容器(会按照run命令的参数start或者restart容器,然后用attach附加:回到ubuntu_bash命令行)
$ docker start ubuntu_bash
$ docker attach

2)守护式容器(-d 守护式)
$ docker run --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

查看容器日志(--tail 0 显示最后0条日志,-f 跟踪新日志,-t 显示时间,无参数 显示全部日志)
$ docker logs --tail 0 -ft daemon_dave

查看容器进程
$ docker top daemon_dave

容器内运行进程(-d 后台进程,-it STDIN+TTY)
$ docker exec -d daemon_dave touch /etc/new_config_file
$ docker exec -it daemon_dave /bin/bash
# ls -l /etc/new_config_file
# ps aux
# exit

停止守护式容器
$ docker stop daemon_dave

3)自动重启容器(--restart,always总是重启,on-failure失败重启,on-failure:5失败重启并且最多尝试5次)
$ docker run --restart=always --name daemon_dave2 -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
如果想改变重启策略,可以用update命令:
$ docker update --restart=no daemon_dave2 

查看容器详情(--format/-f,后面是Go模版,不加参数显示全部信息)
$ docker inspect --format '{{ .Name }} {{ .State.Running }}' daemon_dave

删除容器(删除前应该stop或者kill容器运行状态,-f 强制删除)
$ docker rm daemon_dave2

删除全部容器
$ docker rm `docker ps -a -q`

导出容器(-o 导出到文件)
$ docker export -o ~/Desktop/ubuntu_bash_snapshot1.tar ubuntu_bash

导入容器(导入为mybash:latest镜像,其中tag名是可选的)
$ docker import ~/Desktop/ubuntu_bash_snapshot1.tar my_bash:latest

进入容器bash
$ docker exec -it ubuntu_bash /bin/bash

数据容器
旧的方式:
$ docker create -v /挂载点 --name 容器名 镜像名 /bin/true
也可以直接建立并运行容器
$ docker run --name=容器名 --entrypoint /bin/true 镜像名
Docker 1.9之后更好的方式:
$ docker volume create --name vol1
$ docker volume inspect vol1
$ ls /var/lib/docker/volumes/vol1/_data
使用volume
$ docker run -d --name 容器名 -v vol1:/容器内挂载点 镜像名

清理未使用的数据容器
$ docker volume prune

本机挂载点映射(更建议用数据容器)
$ docker run -d --name 容器名 -v /本机挂载点:/容器内挂载点 镜像名
本机挂载点建议是/var下面
-v 后面参数区别是,路径全名为本机映射,否则为docker的卷映射

注:如果是macOS,需用screen进入VirtualBox虚拟机进行操作(Ctrl+A,K退出当前screen)
$ screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
按下Return键盘,会出现#输入提示符。



镜像(公共镜像Docker Hub,私有镜像)

查看镜像列表(-q 只列出镜像ID)
$ docker images

备份镜像(-o 导出到文件)
$ docker save -o ~/Desktop/ubuntu.tar ubuntu

载入镜像
$ docker load -i ~/Desktop/ubuntu.tar

删除镜像(删除前要删除相应的容器,-f 强制删除)
$ docker rmi ubuntu

查找公共镜像
$ docker search fedora

登录Docker Hub(认证信息会保存在~/.docker/config.json)
$ docker login

提交commit一个apache2环境的镜像流程(假设已经login到Docker Hub,用户名m2nlight)
$ docker run -it ubuntu /bin/bash
# apt-get -yqq update
# apt-get -y install apache2
# exit
$ docker commit -m="A new custom image" --author="Bob" `docker ps -lq` m2nlight/apache2:webserver
其中:`docker ps -lq` 是获得最新创建的容器ID,-m和--author以及仓库的tag是可选

使用Dockerfile构建一个新镜像流程
$ mkdir static_web
$ cd static_web
$ touch Dockerfile
文件内容输入:
# Version: 0.0.1
FROM ubuntu:latest
MAINTAINER Bob "bob@gmail.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hi, I am in your container' > /usr/share/nginx/html/index.html
EXPOSE 80
文件帮助:https://docs.docker.com/engine/reference/builder/
$ docker build -t m2nlight/static_web .
其中.表示当前路径,-t 指定镜像(默认TAG为latest),可以使用 --no-cache 参数跳过使用缓存

查看构建历史
$ docker history m2nlight/static_web

从镜像启动容器(-p [宿主机端口:]容器端口[/udp],缺省宿主机端口会被随机分配,若使用大写的 -P 可自动使用EXPOSE指定的端口)
$ docker run -d -p 8080:80 --name static_web m2nlight/static_web nginx -g "daemon off;"
其中nginx -g "daemon off;"是前端运行nginx

查看端口映射
$ docker port static_web
80/tcp -> 0.0.0.0:8080

推送镜像到服务器
$ docker push m2nlight/static_web

私有Docker Hub
官方:https://hub.docker.com/_/registry/
参考:https://docs.docker.com/registry/deploying/
$ docker run -d -p 5000:5000 --restart=always --name registry registry
存储选项(-v 宿主机全路径:容器全路径[:ro|rw],在类UNIX系统中`pwd`返回当前目录)
-v `pwd`/data:/var/lib/registry
这行意思是将当前目录下的data目录,作为容器/var/lib/registry的映射目录。
注意:Windows系统,路径要用/代替,并且在Docker配置Shared Drivers里开启了对应盘符的共享。

假设私有Docker Hub主机名是RegistryServer,当然IP也可以,用tag命令打一个新标签,然后用push推送到服务器:
$ docker tag m2nlight/static_web RegistryServer:5000/m2nlight/static_web
$ docker images
$ docker push RegistryServer:5000/m2nlight/static_web
如果push失败:
Get https://RegistryServer:5000/v1/_ping: http: server gave HTTP response to HTTPS client
需要在客户机的Docker加入对服务器的信任配置:
mac: 进入Docker配置的Advanced页,在Insecure registries列表里加入如:RegistryServer:5000
win: 进入Docker设置的Docker Daemon选项,在右边的json格式文本框的insecure-registries后加入:RegistryServer:5000
linux: 
$ vim /etc/docker/daemon.json
{
  "insecure-registries": ["RegistryServer:5000"]
}
$ systemctl restart docker
重启客户机Docker生效。

被tag的“镜像”,同样适用其他镜像操作,比如rmi“删除”。

国内的Docker镜像:
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn/",
    "https://hub-mirror.c.163.com"
  ]
}

----------------------------
Control and configure Docker with systemd
https://docs.docker.com/engine/admin/systemd/#start-automatically-at-system-boot

Linux 下非root账户管理docker,因为只有root和docker组才能操作/var/run/docker.sock,
因此只要将当前用户加入到docker组,就可以不必sudo使用docker命令了
如果没有docker组,可手动创建
$ sudo groupadd docker
添加当前用户到docker组
$ sudo usermod -aG docker $USER
将当前用户加入到docker组,然后重新登入。

其他一些特殊文件
$ sudo ls -al /var/run/docker.sock
srw-rw---- 1 root docker 0 Jan 17 09:33 /var/run/docker.sock
$ sudo ls -al /etc/docker/daemon.json
-rw-r--r-- 1 root root 150 Jan 17 09:32 /etc/docker/daemon.json
$ sudo ls -al /etc/docker/key.json
-rw------- 1 root root 244 Nov 10 18:11 /etc/docker/key.json
$ sudo ls -al ~/.docker/config.json
-rw------- 1 ec2-user ec2-user 188 Jan 16 19:41 /home/ec2-user/.docker/config.json
保存了当前用户用docker login的授权信息和HttpHeader

环境变量
DOCKER_HOST
如果配置了证书,可以手动指定DOCKER_HOST和DOCKER_TLS_VERIFY
https://docs.docker.com/engine/security/https/#connecting-to-the-secure-docker-port-using-curl
$ export DOCKER_HOST=tcp://$HOST:2376 DOCKER_TLS_VERIFY=1

如果连接不了Docker Daemon,可能是DOCKER_HOST错误,unset即可连接到本机的。
$ env | grep DOCKER_HOST
$ unset DOCKER_HOST


daemon.json在linux下的可配置例子:
{
	"authorization-plugins": [],
	"data-root": "",
	"dns": [],
	"dns-opts": [],
	"dns-search": [],
	"exec-opts": [],
	"exec-root": "",
	"experimental": false,
	"storage-driver": "",
	"storage-opts": [],
	"labels": [],
	"live-restore": true,
	"log-driver": "",
	"log-opts": {},
	"mtu": 0,
	"pidfile": "",
	"cluster-store": "",
	"cluster-store-opts": {},
	"cluster-advertise": "",
	"max-concurrent-downloads": 3,
	"max-concurrent-uploads": 5,
	"default-shm-size": "64M",
	"shutdown-timeout": 15,
	"debug": true,
	"hosts": [],
	"log-level": "",
	"tls": true,
	"tlsverify": true,
	"tlscacert": "",
	"tlscert": "",
	"tlskey": "",
	"swarm-default-advertise-addr": "",
	"api-cors-header": "",
	"selinux-enabled": false,
	"userns-remap": "",
	"group": "",
	"cgroup-parent": "",
	"default-ulimits": {},
	"init": false,
	"init-path": "/usr/libexec/docker-init",
	"ipv6": false,
	"iptables": false,
	"ip-forward": false,
	"ip-masq": false,
	"userland-proxy": false,
	"userland-proxy-path": "/usr/libexec/docker-proxy",
	"ip": "0.0.0.0",
	"bridge": "",
	"bip": "",
	"fixed-cidr": "",
	"fixed-cidr-v6": "",
	"default-gateway": "",
	"default-gateway-v6": "",
	"icc": false,
	"raw-logs": false,
	"allow-nondistributable-artifacts": [],
	"registry-mirrors": [],
	"seccomp-profile": "",
	"insecure-registries": [],
	"disable-legacy-registry": false,
	"no-new-privileges": false,
	"default-runtime": "runc",
	"oom-score-adjust": -500,
	"runtimes": {
		"runc": {
			"path": "runc"
		},
		"custom": {
			"path": "/usr/local/bin/my-runc-replacement",
			"runtimeArgs": [
				"--debug"
			]
		}
	}
}
原文地址:https://www.cnblogs.com/Bob-wei/p/5788019.html