Ubuntu 安裝 Docker(譯)

Ubuntu

Docker 支持這些 Ubuntu 操做系統: linux

  • Ubuntu Wily 15.10
  • Ubuntu Vivid 15.04
  • Ubuntu Trusty 14.04 (LTS)
  • Ubuntu Precise 12.04 (LTS)

本頁指導您使用 Docker-managed 管理髮行包和安裝機制來安裝。使用這些安裝包能夠確保你獲取到的是最新發布的Docker。若是你但願使用 Ubuntu-managed 包安裝,查閱ubuntu相關文檔。 docker

: Ubuntu Utopic 14.10 已經在 Docker's apt 倉庫存在,可是它將不會再被官方支持。 shell

前提

Docker requires a 64-bit installation regardless of your Ubuntu version. Additionally, your kernel must be 3.10 at minimum. The latest 3.10 minor version or a newer maintained version are also acceptable. ubuntu

Kernels older than 3.10 lack some of the features required to run Docker containers. These older versions are known to have bugs which cause data loss and frequently panic under certain conditions. 安全

To check your current kernel version, open a terminal and use uname -r to display your kernel version: app

$ uname -r 3.11.0-15-generic

: 若是你以前使用 apt 安裝 的哦此可忍,確保你更新apt 到新的Docker 倉庫。 less

更新 apt 源

Docker’s apt 倉庫包含 Docker 1.7.1 或更高版本. 設置apt 使用重新的倉庫使用軟件包: socket

  1. If you haven’t already done so, log into your Ubuntu instance as a privileged user. tcp

  2. 打開一個終端窗口。 編輯器

  3. 增長新 gpg key.

    $ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
  4. 在你信息的編輯器中打開編輯這個  /etc/apt/sources.list.d/docker.list 文件。若是文件不存在,新建。

  5. 刪除全部存在的項。

  6. 爲你的 Ubuntu操做系統增長新項。

    或許有下面條目:

    • 在 Ubuntu Precise 12.04 (LTS)

      deb https://apt.dockerproject.org/repo ubuntu-precise main
    • 在 Ubuntu Trusty 14.04 (LTS)

      deb https://apt.dockerproject.org/repo ubuntu-trusty main
    • 在 Ubuntu Vivid 15.04

      deb https://apt.dockerproject.org/repo ubuntu-vivid main
    • Ubuntu Wily 15.10

      deb https://apt.dockerproject.org/repo ubuntu-wily main
  7. 保存且關閉 /etc/apt/sources.list.d/docker.list 這個文件。

  8. 更新 apt 軟件包索引。

    $ apt-get update
  9. 清除舊倉庫若是存在。

    $ apt-get purge lxc-docker
  10. 驗證 apt 是從 右庫來拉取軟件包。

    $ apt-cache policy docker-engine 

    從如今開始當使用 apt-get 升級,apt 將會重新的倉庫拉取軟件包。

Ubuntu 版本前提

  • Ubuntu Wily 15.10
  • Ubuntu Vivid 15.04
  • Ubuntu Trusty 14.04 (LTS)

對於 Ubuntu Trusty, Vivid, and Wily, it’s recommended to install the linux-image-extra kernel package. The linux-image-extra package allows you use theaufs storage driver.

To install the linux-image-extra package for your kernel version:

  1. Open a terminal on your Ubuntu host.

  2. 更新軟件包管理。

    $ sudo apt-get update
  3. 推薦安裝軟件包。

    $ sudo apt-get install linux-image-extra-$(uname -r)
  4. 來吧,安裝Ubuntu窗口。

Ubuntu Precise 12.04 (LTS)

For Ubuntu Precise, Docker requires the 3.13 kernel version. If your kernel version is older than 3.13, you must upgrade it. Refer to this table to see which packages are required for your environment:

linux-image-generic-lts-trusty Generic Linux kernel image. This kernel has AUFS built in. This is required to run Docker.
linux-headers-generic-lts-trusty Allows packages such as ZFS and VirtualBox guest additions which depend on them. If you didn’t install the headers for your existing kernel, then you can skip these headers for the」trusty」 kernel. If you’re unsure, you should include this package for safety.
xserver-xorg-lts-trusty Optional in non-graphical environments without Unity/Xorg.Required when running Docker on machine with a graphical environment. 

To learn more about the reasons for these packages, read the installation instructions for backported kernels, specifically the LTS Enablement Stack — refer to note 5 under each version.
libgl1-mesa-glx-lts-trusty
 

To upgrade your kernel and install the additional packages, do the following:

  1. Open a terminal on your Ubuntu host.

  2. Update your package manager.

    $ sudo apt-get update
  3. Install both the required and optional packages.

    $ sudo apt-get install linux-image-generic-lts-trusty

    Depending on your environment, you may install more as described in the preceding table.

  4. Reboot your host.

    $ sudo reboot
  5. After your system reboots, go ahead and install Docker.

安裝

確保已經安裝了前提的 Ubuntu 版本,接下來, 根據下面步驟安裝 Docker:

  1. 使用sudo 權限的用戶登陸到Ubuntu系統進行安裝。

  2. 更新 apt 軟件包索引。

    $ sudo apt-get update
  3. 安裝 Docker.

    $ sudo apt-get install docker-engine 
  4. Start the docker daemon.

    $ sudo service docker start
  5. Verify docker is installed correctly.

    $ sudo docker run hello-world

    This command downloads a test image and runs it in a container. When the container runs, it prints an informational message. Then, it exits.

可選項配置

This section contains optional procedures for configuring your Ubuntu to work better with Docker.

建立 Docker 用戶組

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user.

To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

警告: docker 用戶組等效於root用戶;對於您的系統上安全的細節的影響,查看 Docker 守護進程攻擊點  詳細.

建立 Docker 用戶組增長用戶:

  1. 使用 sudo 權限用戶登陸 Ubuntu.

    This procedure assumes you log in as the ubuntu user.

  2. 建立docker 組並新增用戶

    $ sudo usermod -aG docker ubuntu
  3. Log out and log back in.

    This ensures your user is running with the correct permissions.

  4. Verify your work by running docker without sudo.

    $ docker run hello-world

    若是失敗並有相似這樣的信息提示:

    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?

    Check that the DOCKER_HOST environment variable is not set for your shell. If it is, unset it.

Adjust memory and swap accounting

When users run Docker, they may see these messages when working with an image:

WARNING: Your kernel does not support cgroup swap limit. WARNING: Your
kernel does not support swap limit capabilities. Limitation discarded.

To prevent these messages, enable memory and swap accounting on your system. Enabling memory and swap accounting does induce both a memory overhead and a performance degradation even when Docker is not in use. The memory overhead is about 1% of the total available memory. The performance degradation is roughly 10%.

To enable memory and swap on system using GNU GRUB (GNU GRand Unified Bootloader), do the following:

  1. Log into Ubuntu as a user with sudo privileges.

  2. Edit the /etc/default/grub file.

  3. Set the GRUB_CMDLINE_LINUX value as follows:

    GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
  4. Save and close the file.

  5. Update GRUB.

    $ sudo update-grub
  6. Reboot your system.

Enable UFW forwarding

If you use UFW (Uncomplicated Firewall) on the same host as you run Docker, you’ll need to do additional configuration. Docker uses a bridge to manage container networking. By default, UFW drops all forwarding traffic. As a result, for Docker to run when UFW is enabled, you must set UFW’s forwarding policy appropriately.

Also, UFW’s default set of rules denies all incoming traffic. If you want to reach your containers from another host allow incoming connections on the Docker port. The Docker port defaults to 2376 if TLS is enabled or 2375 when it is not. If TLS is not enabled, communication is unencrypted. By default, Docker runs without TLS enabled.

To configure UFW and allow incoming connections on the Docker port:

  1. Log into Ubuntu as a user with sudo privileges.

  2. Verify that UFW is installed and enabled.

    $ sudo ufw status
  3. Open the /etc/default/ufw file for editing.

    $ sudo nano /etc/default/ufw
  4. Set the DEFAULT_FORWARD_POLICY policy to:

    DEFAULT_FORWARD_POLICY="ACCEPT"
  5. Save and close the file.

  6. Reload UFW to use the new setting.

    $ sudo ufw reload
  7. Allow incoming connections on the Docker port.

    $ sudo ufw allow 2375/tcp

Configure a DNS server for use by Docker

Systems that run Ubuntu or an Ubuntu derivative on the desktop typically use127.0.0.1 as the default nameserver in /etc/resolv.conf file. The NetworkManager also sets up dnsmasq to use the real DNS servers of the connection and sets up nameserver 127.0.0.1 in /etc/resolv.conf.

When starting containers on desktop machines with these configurations, Docker users see this warning:

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]

The warning occurs because Docker containers can’t use the local DNS nameserver. Instead, Docker defaults to using an external nameserver.

To avoid this warning, you can specify a DNS server for use by Docker containers. Or, you can disable dnsmasq in NetworkManager. Though, disabling dnsmasqmight make DNS resolution slower on some networks.

To specify a DNS server for use by Docker:

  1. Log into Ubuntu as a user with sudo privileges.

  2. Open the /etc/default/docker file for editing.

    $ sudo nano /etc/default/docker
  3. Add a setting for Docker.

    DOCKER_OPTS="--dns 8.8.8.8"

    Replace 8.8.8.8 with a local DNS server such as 192.168.1.1. You can also specify multiple DNS servers. Separated them with spaces, for example:

    --dns 8.8.8.8 --dns 192.168.1.1

    Warning: If you’re doing this on a laptop which connects to various networks, make sure to choose a public DNS server.

  4. Save and close the file.

  5. Restart the Docker daemon.

    $ sudo restart docker

   

Or, as an alternative to the previous procedure, disable dnsmasq in NetworkManager (this might slow your network).

  1. Open the /etc/NetworkManager/NetworkManager.conf file for editing.

    $ sudo nano /etc/NetworkManager/NetworkManager.conf
  2. Comment out the dns=dsnmasq line:

    dns=dnsmasq
  3. Save and close the file.

  4. Restart both the NetworkManager and Docker.

    $ sudo restart network-manager $ sudo restart docker

Configure Docker to start on boot

Ubuntu uses systemd as its boot and service manager 15.04 onwards andupstart 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 configuresupstart to start the docker daemon on boot

升級 Docker

To install the latest version of Docker with apt-get:

$ apt-get upgrade docker-engine

卸載

卸載Docker 軟件包:

$ sudo apt-get purge docker-engine 

卸載Docker軟件包和依賴,它們不在使用:

$ sudo apt-get autoremove --purge docker-engine

以下命令將不會移除鏡像、容器、數據卷,或用戶在主機建立的配置文件。若是但願刪除全部鏡像、容器和數據卷運行以下命令:

$ rm -rf /var/lib/docker

你必須手動刪除用戶建立的配置文件。

相關文章
相關標籤/搜索