Ubuntu安装docker

时间:2022-07-25
本文章向大家介绍Ubuntu安装docker,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

向您推荐

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.