向您推薦
Dcoker入門與實踐系列文章
Ubuntu安裝docker
docker目前支援如下的一些ubuntu作業系統:
- Ubuntu Wily 15.10
- Ubuntu Trusty 14.04 (LTS)
- Ubuntu Precise 12.04 (LTS)
Note: Ubuntu Utopic 14.10 and 15.04 exist in Docker’s APT repository but are no longer officially supported.
限制條件
- 需要使用64位CPU
- 作業系統的核心最低是3.10
$ uname -r
3.11.0-15-generic
複制
- 各Host主機時間要同步
sudo ntpdate cn.pool.ntp.org
- 此安裝方法需要聯網
更新apt源
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
#添加秘鑰
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
複制
vi /etc/apt/sources.list.d/docker.list
根據不同的作業系統添加如下條目到檔案中
* On Ubuntu Precise 12.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-precise main
* On Ubuntu Trusty 14.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-trusty main
* Ubuntu Wily 15.10
deb https://apt.dockerproject.org/repo ubuntu-wily main
儲存退出
更新APT包index.
$ sudo apt-get update
Purge the old repo if it exists.
$ sudo apt-get purge lxc-docker
Verify that APT is pulling from the right repository.
$ sudo apt-cache policy docker-engine
From now on when you run apt-get upgrade, APT pulls from the new repository.
複制
Prerequisites by Ubuntu Version
- Ubuntu Wily 15.10
- Ubuntu Vivid 15.04
-
Ubuntu Trusty 14.04 (LTS)
For Ubuntu Trusty, Vivid, and Wily, it’s recommended to install the linux-image-extra kernel package. The linux-image-extra package allows you use the aufs storage driver.
$ sudo apt-get update
Install the recommended package.
$ sudo apt-get install linux-image-extra-$(uname -r)
Go ahead and install Docker.
複制
If you are installing on Ubuntu 14.04 or 12.04, apparmor is required. You can install it using:
sudo apt-get install apparmor
安裝Docker
$ sudo apt-get update
$ sudo apt-get install docker-engine
$ sudo service docker start
複制
- 安裝docker指定版本
sudo apt-get install docker-engine=1.10.1-0~trusty
複制
可選配置
This section contains optional procedures for configuring your Ubuntu to work better with Docker.
- Create a docker group(通常做完這步安裝就ok了,後面的可選,可供學習參考)
- Adjust memory and swap accounting
- Enable UFW forwarding
- Configure a DNS server for use by Docker
- Configure Docker to start on boot
- CreateCreateCreate a Docker group
建立Docker使用者組
$ sudo usermod -aG docker <YOUR_USERNAME>
複制
調整記憶體和交換空間(swap accounting)
當我們使用 Docker 運作一個鏡像的時候,我們可能會看到如下的資訊提示:
WARNING: Your kernel does not support cgroup swap limit. WARNING: Your
kernel does not support swap limit capabilities. Limitation discarded.
為了防止以上錯誤資訊提示的出現,我們需要在系統中啟用記憶體和交換空間。我們需要修改系統的 GUN GRUB (GNU GRand Unified Bootloader) 來啟用記憶體和交換空間。開啟方法如下:
1.使用具有sudo權限的使用者來登入你的Ubuntu。
2.編輯 /etc/default/grub 檔案
3.設定 GRUB_CMDLINE_LINUX 的值如下:
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
4.儲存和關閉檔案
5.更新 GRUB
$ sudo update-grub
6.重新開機你的系統。
允許UFW端口轉發
當你在運作 docker 的宿主主機上使用UFW(簡單的防火牆)。你需要做一些額外的配置。Docker 使用橋接的方式來管理網絡。預設情況下,UFW 過濾所有的端口轉發政策。是以,當在UFW啟用的情況下使用 docker ,你必須适當的設定UFW的端口轉發政策。
預設情況下UFW是過濾掉所有的入站規則。如果其他的主機能夠通路你的容器。你需要允許Docker的預設端口(2375)的所有連接配接。
設定 UFW 允許Docker 端口的入站規則:
1.使用具有sudo權限的使用者來登入你的Ubuntu
2.驗證UFW的安裝和啟用狀态
$ sudo ufw status
3.打開和編輯/etc/default/ufw檔案
$ sudo nano /etc/default/ufw
4.設定 DEFAULT_FORWARD_POLICY 如下:
DEFAULT_FORWARD_POLICY="ACCEPT"
5.儲存關閉檔案。
6.重新加載UFW來使新規則生效。
$ sudo ufw reload
7.允許 Docker 端口的入站規則
$ sudo ufw allow 2375/tcp
Docker 配置 DNS 服務
無論是Ubuntu還是Ubuntu 桌面繁衍版在系統運作的時候都是使用/etc/resolv.conf配置檔案中的127.0.0.1作為域名伺服器(nameserver)。NetworkManager設定dnsmasq使用真實的dns伺服器連接配接,并且設定 /etc/resolv.conf的域名服務為127.0.0.1。
在桌面環境下使用這些配置來運作 docker 容器的時候, Docker 使用者會看到如下的警告:
WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers
can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
複制
該警告是因為 Docker 容器不能使用本地的DNS服務。相反 Docker 使用一個預設的外部域名伺服器。
為了避免此警告,你可以給 Docker 容器指定一個DNS伺服器。或者你可以禁用 NetworkManager 的 dnsmasq。不過當禁止 dnsmasq 可能使某些網絡的DNS解析速度變慢。
為 Docker 指定一個DNS伺服器
1.使用具有sudo權限的使用者來登入你的Ubuntu。
2.打開并編輯 /etc/default/docker
$ sudo nano /etc/default/docker
3.添加設定
```DOCKER_OPTS="--dns 8.8.8.8"```
使用8.8.8.8替換如192.168.1.1的本地DNS伺服器。你可以指定多個DNS伺服器,多個DNS伺服器使用空格分割例如
```--dns 8.8.8.8 --dns 192.168.1.1```
警告:如果你正在使用的電腦需要連接配接到不同的網絡,一定要選擇一個公共DNS伺服器。
複制
4.儲存關閉檔案。
5.重新開機 Docker 程序
$ sudo restart docker
或者,作為替代先前的操作過程,禁止NetworkManager中的dnsmasq(這樣會使你的網絡變慢)
1.打開和編輯 /etc/default/docker
$ sudo nano /etc/NetworkManager/NetworkManager.conf
2.注釋掉 dns = dsnmasq:
dns=dnsmasq
3.儲存關閉檔案
4.重新開機NetworkManager 和 Docker
$ sudo restart network-manager
$ sudo restart docker
複制
開機啟動Docker(Configure Docker to start on boot)
Ubuntu uses systemd as its boot and service manager 15.04 onwards and upstart for versions 14.10 and below.
For 15.04 and up, to configure the docker daemon to start on boot, run
$ sudo systemctl enable docker
For 14.10 and below the above installation method automatically configures upstart to start the docker daemon on boot
更新Docker(Upgrade Docker)
To install the latest version of Docker with apt-get:
$ sudo apt-get upgrade docker-engine
解除安裝Docker(Uninstallation)
#To uninstall the Docker package:
$ sudo apt-get purge docker-engine
#To uninstall the Docker package and dependencies that are no longer needed:
$ sudo apt-get autoremove --purge docker-engine
#The above commands will not remove images, containers, volumes, or user created configuration files on your host. If you wish to delete all images, containers, and volumes run the following command:
$ rm -rf /var/lib/docker
#You must delete the user created configuration files manually.
複制