# 容器中止後就自動刪除: docker run --rm centos /bin/echo "One"; # 殺死全部正在運行的容器: docker kill $(docker ps -a -q) # 刪除全部未打標籤的鏡像: docker rmi $(docker images -q -f dangling=true) # 刪除全部已經中止的容器: docker rm $(docker ps -a -q) # 顯示鏡像名字:tag docker images --format "{{.Repository}}:{{.Tag}}" alias tag='docker images --format "{{.Repository}}:{{.Tag}}"' alias bb='docker run -it --rm busybox' alias dp='docker ps -a' alias dt='docker images --format "{{.Repository}}:{{.Tag}}"' alias dc='docker rm $(docker ps -a -q)' alias ds='docker stats' alias kk='kubectl get pod --all-namespaces -o wide --show-labels' alias ks='kubectl get svc --all-namespaces -o wide' alias kss='kubectl get svc --all-namespaces -o wide --show-labels' alias kd='kubectl get deploy --all-namespaces -o wide' alias wk='watch kubectl get pod --all-namespaces -o wide --show-labels' alias kv='kubectl get pv -o wide' alias kvc='kubectl get pvc -o wide --all-namespaces --show-labels' alias kbb='kubectl run -it --rm --restart=Never busybox --image=busybox sh' alias kbbc='kubectl run -it --rm --restart=Never curl --image=appropriate/curl sh' alias kd='kubectl get deployment --all-namespaces --show-labels' alias kcm='kubectl get cm --all-namespaces -o wide' alias kin='kubectl get ingress --all-namespaces -o wide'
yum install bash-com* -y wget https://raw.githubusercontent.com/lannyMa/scripts/master/docker mv docker /etc/bash_completion.d/ $ wget https://get.docker.com/builds/Linux/x86_64/docker-17.04.0-ce.tgz $ tar -xvf docker-17.04.0-ce.tgz $ cp docker/docker* /root/local/bin $ cp docker/completion/bash/docker /etc/bash_completion.d/
參考:
https://mirrors.aliyun.com/help/docker-ce
https://yq.aliyun.com/articles/110806html
yum install -y libnetfilter_conntrack-devel libmnl-devel conntrack-tools socat echo "net.netfilter.nf_conntrack_acct=1" >> /etc/sysctl.conf echo "net.netfilter.nf_conntrack_timestamp=1" >> /etc/sysctl.conf sysctl -p /etc/sysctl.conf yum install -y yum-utils device-mapper-persistent-data lvm2 #若是docker-ce.repo內容爲空,則參考下面附. yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo sudo yum makecache fast sudo yum install docker-ce -y
mkdir -p /etc/docker cat >>/etc/docker/daemon.json<<EOF { "registry-mirrors": ["https://registry.docker-cn.com"], "hosts": [ "tcp://0.0.0.0:2375", "unix:///var/run/docker.sock" ] } EOF
systemctl daemon-reload systemctl restart docker && systemctl enable docker
sysctl -w net.ipv6.conf.all.disable_ipv6=1 sysctl -w net.ipv6.conf.default.disable_ipv6=1 sysctl -w net.ipv4.ip_forward=1 echo 'iptables -P FORWARD ACCEPT' >> /etc/rc.local iptables -P FORWARD ACCEPT sysctl -p docker pull busybox echo "alias bb='docker run -it --rm busybox'" >> /etc/bashrc source /etc/bashrc sudo systemctl stop firewalld sudo systemctl disable firewalld sudo iptables -F && sudo iptables -X && sudo iptables -F -t nat && sudo iptables -X -t nat iptables -t nat -L -n
ln -s /var/run/docker/netns /var/run/netns
journalctl -u docker -f
cd yum install python-pip -y mkdir ~/.pip cat >pip.conf<<EOF [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com EOF mv ./pip.conf ~/.pip/ pip install docker-compose pip install --upgrade pip
下載:
https://download.docker.com/linux/static/stable/x86_64/node
tar -xvf docker-17.04.0-ce.tgz cp docker/docker* /user/local/bin cp docker/completion/bash/docker /etc/bash_completion.d/ dockerd --log-level=error --bip=10.2.20.1/24 --mtu=1500 --ipmasq=true
$ cat docker.service [Unit] Description=Docker Application Container Engine Documentation=http://docs.docker.io [Service] Environment="PATH=/root/local/bin:/bin:/sbin:/usr/bin:/usr/sbin" EnvironmentFile=-/run/flannel/docker ## 這裏比較關鍵 ExecStart=/root/local/bin/dockerd --log-level=error $DOCKER_NETWORK_OPTIONS ExecReload=/bin/kill -s HUP $MAINPID Restart=on-failure RestartSec=5 LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Delegate=yes KillMode=process [Install] WantedBy=multi-user.target
兩臺機器間容器經過flannel網絡沒法互pingpython
systemctl stop firewalld && systemctl disable firewalld sysctl -w net.ipv6.conf.all.disable_ipv6=1 sysctl -w net.ipv6.conf.default.disable_ipv6=1 sysctl -w net.ipv4.ip_forward=1 iptables -P FORWARD ACCEPT sysctl -p echo 'iptables -P FORWARD ACCEPT' >> /rc.local
下面詳解了解下flannel如何和docker聯動linux
1.下載flannel後,壓縮包裏有個生成docker啓動參數(網絡)的腳本git
2.flannel啓動會執行這個腳本(flannel的services實現),動態生成docker參數github
/usr/local/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
$ cat > flanneld.service << EOF [Unit] Description=Flanneld overlay address etcd agent After=network.target After=network-online.target Wants=network-online.target After=etcd.service Before=docker.service [Service] Type=notify ExecStart=/root/local/bin/flanneld ExecStartPost=/root/local/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker Restart=on-failure [Install] WantedBy=multi-user.target RequiredBy=docker.service EOF
mk-docker-opts.sh 腳本將分配給 flanneld 的 Pod 子網網段信息寫入到 /run/flannel/docker 文件中,後續 docker 啓動時使用這個文件中參數值設置 docker0 網橋;
-iface 選項值指定 flanneld 和其它 Node 通訊的接口,若是機器有內、外網,則最好指定爲內網接口;web
$ cat docker.service [Unit] Description=Docker Application Container Engine Documentation=http://docs.docker.io [Service] Environment="PATH=/root/local/bin:/bin:/sbin:/usr/bin:/usr/sbin" EnvironmentFile=-/run/flannel/docker ExecStart=/root/local/bin/dockerd --log-level=error $DOCKER_NETWORK_OPTIONS ExecReload=/bin/kill -s HUP $MAINPID Restart=on-failure RestartSec=5 LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Delegate=yes KillMode=process [Install] WantedBy=multi-user.target
flannel自動生成docker參數腳本docker
/usr/local/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
#!/bin/sh usage() { echo "$0 [-f FLANNEL-ENV-FILE] [-d DOCKER-ENV-FILE] [-i] [-c] [-m] [-k COMBINED-KEY] Generate Docker daemon options based on flannel env file OPTIONS: -f Path to flannel env file. Defaults to /run/flannel/subnet.env -d Path to Docker env file to write to. Defaults to /run/docker_opts.env -i Output each Docker option as individual var. e.g. DOCKER_OPT_MTU=1500 -c Output combined Docker options into DOCKER_OPTS var -k Set the combined options key to this value (default DOCKER_OPTS=) -m Do not output --ip-masq (useful for older Docker version) " >&2 exit 1 } flannel_env="/run/flannel/subnet.env" docker_env="/run/docker_opts.env" combined_opts_key="DOCKER_OPTS" indiv_opts=false combined_opts=false ipmasq=true while getopts "f:d:icmk:?h" opt; do case $opt in f) flannel_env=$OPTARG ;; d) docker_env=$OPTARG ;; i) indiv_opts=true ;; c) combined_opts=true ;; m) ipmasq=false ;; k) combined_opts_key=$OPTARG ;; [\?h]) usage ;; esac done if [ $indiv_opts = false ] && [ $combined_opts = false ]; then indiv_opts=true combined_opts=true fi if [ -f "$flannel_env" ]; then . $flannel_env fi if [ -n "$FLANNEL_SUBNET" ]; then DOCKER_OPT_BIP="--bip=$FLANNEL_SUBNET" fi if [ -n "$FLANNEL_MTU" ]; then DOCKER_OPT_MTU="--mtu=$FLANNEL_MTU" fi if [ -n "$FLANNEL_IPMASQ" ] && [ $ipmasq = true ] ; then if [ "$FLANNEL_IPMASQ" = true ] ; then DOCKER_OPT_IPMASQ="--ip-masq=false" elif [ "$FLANNEL_IPMASQ" = false ] ; then DOCKER_OPT_IPMASQ="--ip-masq=true" else echo "Invalid value of FLANNEL_IPMASQ: $FLANNEL_IPMASQ" >&2 exit 1 fi fi eval docker_opts="\$${combined_opts_key}" if [ "$docker_opts" ]; then docker_opts="$docker_opts "; fi echo -n "" >$docker_env for opt in $(set | grep "DOCKER_OPT_"); do OPT_NAME=$(echo $opt | awk -F "=" '{print $1;}'); OPT_VALUE=$(eval echo "\$$OPT_NAME"); if [ "$indiv_opts" = true ]; then echo "$OPT_NAME=\"$OPT_VALUE\"" >>$docker_env; fi docker_opts="$docker_opts $OPT_VALUE"; done if [ "$combined_opts" = true ]; then echo "${combined_opts_key}=\"${docker_opts}\"" >>$docker_env fi
#!/bin/bash image_list=`docker images --format "{{.Repository}}:{{.Tag}}"` for image in $image_list;do file_name=`echo $image | sed "s#[/:]#_#g"` echo "Saveing image '$image' into '$file_name.tar.gz'" docker save -o "$file_name.tar" $image gzip $file_name.tar done # gzip把tar搞成了tar.gz, docker load -i tar.gz便可. 若是空間夠,能夠不gzip
參考: https://docs.docker.com/engine/admin/logging/json-file/#usage
默認一個日誌文件.不自動切割.
json
能夠指定bridge啓動(默認的docker0有nat)centos
dockerd -b=br0
能夠啓動後不讓修改iptables
dockerd -b=br0 --iptables=false #默認修改的,iptables -t nat -L -n
$ dockerd --help Usage: dockerd COMMAND A self-sufficient runtime for containers. Options: --add-runtime runtime Register an additional OCI compatible runtime (default []) --allow-nondistributable-artifacts list Allow push of nondistributable artifacts to registry --api-cors-header string Set CORS headers in the Engine API --authorization-plugin list Authorization plugins to load --bip string Specify network bridge IP -b, --bridge string Attach containers to a network bridge --cgroup-parent string Set parent cgroup for all containers --cluster-advertise string Address or interface name to advertise --cluster-store string URL of the distributed storage backend --cluster-store-opt map Set cluster store options (default map[]) --config-file string Daemon configuration file (default "/etc/docker/daemon.json") --containerd string Path to containerd socket --cpu-rt-period int Limit the CPU real-time period in microseconds --cpu-rt-runtime int Limit the CPU real-time runtime in microseconds --data-root string Root directory of persistent Docker state (default "/var/lib/docker") -D, --debug Enable debug mode --default-gateway ip Container default gateway IPv4 address --default-gateway-v6 ip Container default gateway IPv6 address --default-ipc-mode string Default mode for containers ipc ("shareable" | "private") (default "shareable") --default-runtime string Default OCI runtime for containers (default "runc") --default-shm-size bytes Default shm size for containers (default 64MiB) --default-ulimit ulimit Default ulimits for containers (default []) --disable-legacy-registry Disable contacting legacy registries (default true) --dns list DNS server to use --dns-opt list DNS options to use --dns-search list DNS search domains to use --exec-opt list Runtime execution options --exec-root string Root directory for execution state files (default "/var/run/docker") --experimental Enable experimental features --fixed-cidr string IPv4 subnet for fixed IPs --fixed-cidr-v6 string IPv6 subnet for fixed IPs -G, --group string Group for the unix socket (default "docker") --help Print usage -H, --host list Daemon socket(s) to connect to --icc Enable inter-container communication (default true) --init Run an init in the container to forward signals and reap processes --init-path string Path to the docker-init binary --insecure-registry list Enable insecure registry communication --ip ip Default IP when binding container ports (default 0.0.0.0) --ip-forward Enable net.ipv4.ip_forward (default true) --ip-masq Enable IP masquerading (default true) --iptables Enable addition of iptables rules (default true) --ipv6 Enable IPv6 networking --label list Set key=value labels to the daemon --live-restore Enable live restore of docker when containers are still running --log-driver string Default driver for container logs (default "json-file") -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --log-opt map Default log driver options for containers (default map[]) --max-concurrent-downloads int Set the max concurrent downloads for each pull (default 3) --max-concurrent-uploads int Set the max concurrent uploads for each push (default 5) --metrics-addr string Set default address and port to serve the metrics api on --mtu int Set the containers network MTU --network-control-plane-mtu int Network Control plane MTU (default 1500) --no-new-privileges Set no-new-privileges by default for new containers --node-generic-resources string user defined resources (e.g. fpga=2;gpu={UUID1,UUID2,UUID3}) --oom-score-adjust int Set the oom_score_adj for the daemon (default -500) -p, --pidfile string Path to use for daemon PID file (default "/var/run/docker.pid") --raw-logs Full timestamps without ANSI coloring --registry-mirror list Preferred Docker registry mirror --seccomp-profile string Path to seccomp profile --selinux-enabled Enable selinux support --shutdown-timeout int Set the default shutdown timeout (default 15) -s, --storage-driver string Storage driver to use --storage-opt list Storage driver options --swarm-default-advertise-addr string Set default address or interface for swarm advertised address --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote --userland-proxy Use userland proxy for loopback traffic (default true) --userland-proxy-path string Path to the userland proxy binary --userns-remap string User/Group setting for user namespaces -v, --version Print version information and quit
## 開啓流數據包統計(packets和bytes) echo "net.netfilter.nf_conntrack_acct=1" >> /etc/sysctl.conf ## 開啓流持續時間統計(delta-time) echo "net.netfilter.nf_conntrack_timestamp=1" >> /etc/sysctl.conf sysctl -p /etc/sysctl.conf conntrack -L -o ktimestamp
RUN yum -y install kde-l10n-Chinese && \ yum -y reinstall glibc-common &&\ yum clean all && \ localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 ENV LC_ALL "zh_CN.UTF-8"
kubespray安裝k8s集羣時候,自動給docker配置的
[root@n1 ~]# ps -ef|grep docker root 14289 1 2 23:18 ? 00:00:32 /usr/bin/dockerd --insecure-registry=10.233.0.0/18 --graph=/var/lib/docker --log-opt max-size=50m --log-opt max-file=5 --iptables=false --dns 10.233.0.3 --dns 114.114.114.114 --dns-search default.svc.cluster.local --dns-search svc.cluster.local --dns-opt ndots:2 --dns-opt timeout:2 --dns-opt attempts:2 [root@n1 ~]# cat /etc/systemd/system/docker.service [Unit] Description=Docker Application Container Engine Documentation=http://docs.docker.com After=network.target docker-storage-setup.service Wants=docker-storage-setup.service [Service] Type=notify Environment=GOTRACEBACK=crash ExecReload=/bin/kill -s HUP $MAINPID Delegate=yes KillMode=process ExecStart=/usr/bin/dockerd \ $DOCKER_OPTS \ $DOCKER_STORAGE_OPTIONS \ $DOCKER_NETWORK_OPTIONS \ $DOCKER_DNS_OPTIONS \ $INSECURE_REGISTRY LimitNOFILE=1048576 LimitNPROC=1048576 LimitCORE=infinity TimeoutStartSec=1min Restart=on-abnormal [Install] WantedBy=multi-user.target [root@n1 ~]# ll /etc/systemd/system/docker.service.d/ total 12 -rw-r--r-- 1 root root 234 Dec 27 23:18 docker-dns.conf -rw-r--r-- 1 root root 158 Dec 27 23:18 docker-options.conf -rw-r--r-- 1 root root 288 Dec 27 23:18 http-proxy.conf [root@n1 ~]# cat /etc/systemd/system/docker.service.d/docker-options.conf [Service] Environment="DOCKER_OPTS=--insecure-registry=10.233.0.0/18 --graph=/var/lib/docker --log-opt max-size=50m --log-opt max-file=5 \ --iptables=false" [root@n1 ~]# cat /etc/systemd/system/docker.service.d/docker-dns.conf [Service] Environment="DOCKER_DNS_OPTIONS=\ --dns 10.233.0.3 --dns 114.114.114.114 \ --dns-search default.svc.cluster.local --dns-search svc.cluster.local \ --dns-opt ndots:2 --dns-opt timeout:2 --dns-opt attempts:2 \ [root@n1 ~]# cat /etc/systemd/system/docker.service.d/http-proxy.conf [Service] Environment="HTTP_PROXY=http://192.168.1.88:1080/" "NO_PROXY=192.168.2.14,node4,node4.cluster.local,192.168.2.15,node5,node5.cluster.local,192.168.2.11,node1,node1.cluster.local,192.168.2.12,node2,node2.cluster.local,192.168.2.13,node3,node3.cluster.local,127.0.0.1,localhost"
參考:https://docs.docker.com/engine/admin/systemd/
修改docker的svc便可.
[Service] Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" 或者: docker -H LOCAL_IP:1028 測試(才700多K的東西): docker pull gcr.io/google_containers/pause-amd64:3.0
與Supervisord相似的工具包括monit, daemontools和runit。
參考:
https://hub.docker.com/r/faisyl/alpine-runit/~/dockerfile/
image: registry.cn-shenzhen.aliyuncs.com/rancher_cn/heapster-grafana-amd64:v4.4.3 image: registry.cn-hangzhou.aliyuncs.com/outman_google_containers/heapster-amd64:v1.4.0 vi /etc/docker/daemon.json { "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] } [root@n1 influxdb]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "hosts": [ "tcp://0.0.0.0:2375", "unix:///var/run/docker.sock" ] }
echo '{ "insecure-registries":["registry-srv:5000"] }' >/etc/docker/daemon.json
/usr/bin/dockerd --insecure-registry=10.233.0.0/18 --graph=/var/lib/docker --log-opt max-size=50m --log-opt max-file=5 --iptables=false --dns 10.233.0.3 --dns 114.114.114.114 --dns-search default.svc.cluster.local --dns-search svc.cluster.local --dns-opt ndots:2 --dns-opt timeout:2 --dns-opt attempts:2 docker run -it --rm busybox / # cat /etc/resolv.conf search default.svc.cluster.local svc.cluster.local nameserver 10.233.0.3 nameserver 114.114.114.114
[docker-ce-stable] name=Docker CE Stable - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-stable-debuginfo] name=Docker CE Stable - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-stable-source] name=Docker CE Stable - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-edge] name=Docker CE Edge - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-edge-debuginfo] name=Docker CE Edge - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-edge-source] name=Docker CE Edge - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-test] name=Docker CE Test - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-test-debuginfo] name=Docker CE Test - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-test-source] name=Docker CE Test - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
FLANNEL_MTU = 1450 是 vxlan FLANNEL_MTU = 1500 是 host-gw
- docker存儲 docker提供了2種存儲資源: 鏡像(由 storage driver 管理): 1.基礎鏡像層 2.可寫層 storage driver overlay2(devicemapper) autfs Data Volume: 1.bind mount 支持f/d ro/rw 目的任意指定 docker run -v的方式 2.docker managed volume 不支持ro /var/lib/docker/volumes/ 1.--volumes-from: 先啓一個數據容器,後引用容器容器名 data-packed volume container FROM busybox VOLUME ["/data1","/data2"] docker build . -t busybox1 docker run -itd --name b1 busybox1 1.先在/var/lib/docker/volumes/建立目錄 2.掛載到容器/data1 /data2(容器啓動會自動建立這兩個) docker run -itd --volumes-from b1 busybox 2.docker create volume docker volume create hello docker run -d -v hello:/world busybox ls /world
[root@n1 ~]# docker run -itd -v /data:/data --name b4 busybox 1ce96b5f4f135e1e98b33997b95e682efa6287ff744e1613aaacab2e159c353b [root@n1 ~]# docker exec -it b4 sh / # df -h Filesystem Size Used Available Use% Mounted on overlay 37.8G 1.8G 35.9G 5% / tmpfs 487.3M 0 487.3M 0% /dev tmpfs 487.3M 0 487.3M 0% /sys/fs/cgroup /dev/sda3 37.8G 1.8G 35.9G 5% /data
今天覆習瞭如下docker, 發現居然有這玩意.
https://docs.docker.com/engine/reference/commandline/dockerd/#options-per-storage-driver
devicemapper存儲設置 http://blog.51cto.com/welcomeweb/1696121