2-docker 安裝
Ubuntu 安裝
由于 apt 源使用 HTTPS 以確保軟件下載過程中不被篡改。因此,我們首先需要添加使用 HTTPS 傳輸的軟件包以及 CA 證書。
$ sudo apt-get update$ sudo apt-get install \apt-transport-https \ca-certificates \curl \gnupg-agent \software-properties-common
為了確認所下載軟件包的合法性,需要添加軟件源的 GPG 密鑰。
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
然后,我們需要向 source.list 中添加 Docker 軟件源
$ sudo add-apt-repository \"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \$(lsb_release -cs) \stable"# 官方源
# $ sudo add-apt-repository \
# "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
# $(lsb_release -cs) \
# stable"
以上命令會添加穩定版本的 Docker CE APT 鏡像源,如果需要最新或者測試版本的 Docker CE 請將 stable 改為 edge 或者 test。從 Docker 17.06 開始,edge test 版本的 APT 鏡像源也會包含穩定版本的 Docker。
安裝 Docker CE
$ sudo apt-get update$ sudo apt-get install docker-ce
Centos 安裝
- 安裝依賴
$ sudo yum install -y yum-utils \device-mapper-persistent-data \lvm2
- 添加穩定版本的倉庫
$ sudo yum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.repo
- 安裝
sudo yum install docker-ce
會提示你 Is this OK? 輸入 y 然后回車。之后會再次提示確認指紋信息,看一下是否是 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
(不區分大小寫),如果是,再次輸入 y 然后回車,一般都沒問題。
使用腳本自動安裝
在測試或開發環境中 Docker 官方為了簡化安裝流程,提供了一套便捷的安裝腳本,Ubuntu 系統上可以使用這套腳本安裝:
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
如果 get-docker.sh
不能下載,點擊下載。
執行這個命令后,腳本就會自動的將一切準備工作做好,并且把 Docker CE 的 Edge 版本安裝在系統中。
啟動 Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
Ubuntu 14.04 請使用以下命令啟動:
$ sudo service docker start
建立 docker 用戶組
默認情況下,docker 命令會使用 Unix socket 與 Docker 引擎通訊。而只有 root 用戶和 docker 組的用戶才可以訪問 Docker 引擎的 Unix socket。出于安全考慮,一般 Linux 系統上不會直接使用 root 用戶。因此,更好地做法是將需要使用 docker 的用戶加入 docker 用戶組。
建立 docker 組:
$ sudo groupadd docker
將當前用戶加入 docker 組:
$ sudo usermod -aG docker $USER
退出當前終端并重新登錄,進行如下測試。
測試 Docker 是否安裝正確
$ docker run hello-worldUnable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latestHello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://cloud.docker.com/For more examples and ideas, visit:https://docs.docker.com/engine/userguide/
若能正常輸出以上信息,則說明安裝成功。
鏡像加速
鑒于國內網絡問題,后續拉取 Docker 鏡像十分緩慢,強烈建議安裝 Docker 之后配置 國內鏡像加速
Ubuntu 16.04+、Debian 8+、CentOS 7
對于使用 systemd 的系統,請在 /etc/docker/daemon.json 中寫入如下內容(如果文件不存在請新建該文件)
{"registry-mirrors": ["https://registry.docker-cn.com"]
}
注意,一定要保證該文件符合 json 規范,否則 Docker 將不能啟動。
配置Docker DNS
第一種方式:
在 /etc/docker/daemon.json
配置全部容器的 DNS,文件中增加以下內容來設置。
"dns" : ["114.114.114.114","8.8.8.8"]
然后重啟 docker 服務,并驗證:
sudo service docker restart
vincent@scijet_server_1:~$ docker exec -it gitlab-runner bash
root@5d4b8905c983:/# cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.1
root@5d4b8905c983:/# exit
第二種方式:
所有 Docker 容器的 DNS 配置通過 /etc/resolv.conf
文件立刻得到更新。
# 在容器中使用 mount 命令可以看到掛載信息:$ mount
/dev/disk/by-uuid/1fec...ebdf on /etc/hostname type ext4 ...
/dev/disk/by-uuid/1fec...ebdf on /etc/hosts type ext4 ...
tmpfs on /etc/resolv.conf type tmpfs ...
有關daemon.json文件
以下是完整的daemon.json文件可配置的參數表,我們在配置的過程中,只需要設置我們需要的參數即可,不必全部寫出來。詳細參考,官方文檔。
{"api-cors-header": "","authorization-plugins": [],"bip": "","bridge": "","cgroup-parent": "","cluster-store": "","cluster-store-opts": {},"cluster-advertise": "","debug": true,"default-gateway": "","default-gateway-v6": "","default-runtime": "runc","default-ulimits": {},"disable-legacy-registry": false,"dns": [],"dns-opts": [],"dns-search": [],"exec-opts": [],"exec-root": "","fixed-cidr": "","fixed-cidr-v6": "","graph": "","group": "","hosts": [],"icc": false,"insecure-registries": [],"ip": "0.0.0.0","iptables": false,"ipv6": false,"ip-forward": false,"ip-masq": false,"labels": [],"live-restore": true,"log-driver": "","log-level": "","log-opts": {},"max-concurrent-downloads": 3,"max-concurrent-uploads": 5,"mtu": 0,"oom-score-adjust": -500,"pidfile": "","raw-logs": false,"registry-mirrors": [],"runtimes": {"runc": {"path": "runc"},"custom": {"path": "/usr/local/bin/my-runc-replacement","runtimeArgs": ["--debug"]}},"selinux-enabled": false,"storage-driver": "","storage-opts": [],"swarm-default-advertise-addr": "","tls": true,"tlscacert": "","tlscert": "","tlskey": "","tlsverify": true,"userland-proxy": false,"userns-remap": ""
}