ubuntu下安装docker
2016-04-28 15:07
731 查看
官方原文:https://docs.docker.com/engine/installation/linux/ubuntulinux/
Ubuntu
Docker is supported on these Ubuntu operating systems:Ubuntu Xenial 16.04 (LTS)
Ubuntu Wily 15.10
Ubuntu Trusty 14.04 (LTS)
Ubuntu Precise 12.04 (LTS)
This page instructs you to install using Docker-managed release packages and installation mechanisms. Using these packages ensures you get the latest release of Docker. If you wish to install using Ubuntu-managed packages, consult your Ubuntu documentation.
Note: Ubuntu Utopic 14.10 and 15.04 exist in Docker’s
APTrepository
but are no longer officially supported.
Prerequisites
Docker requires a 64-bit installation regardless of your Ubuntu version. Additionally, yourkernel must be 3.10 at minimum. The latest 3.10 minor version or a newer maintained version are also acceptable.
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 -rto display your kernel version:
$ uname -r 3.11.0-15-generic
Note: If you previously installed Docker using
APT,
make sure you update your
APTsources
to the new Docker repository.
Update your apt sources
Docker’s APTrepository contains
Docker 1.7.1 and higher. To set
APTto
use packages from the new repository:
Log into your machine as a user with
sudoor
rootprivileges.
Open a terminal window.
Update package information, ensure that APT works with the
httpsmethod,
and that CA certificates are installed.
$ sudo apt-get update $ sudo apt-get install apt-transport-https ca-certificates
Add the new
GPGkey.
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Open the
/etc/apt/sources.list.d/docker.listfile
in your favorite editor.
If the file doesn’t exist, create it.
Remove any existing entries.
Add an entry for your Ubuntu operating system.
The possible entries are:
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
Ubuntu Xenial 16.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-xenial main
Note: Docker does not provide packages for all architectures. You can find nightly built binaries inhttps://master.dockerproject.org.
To install docker on a multi-architecture system, add an
[arch=...]clause
to the entry. Refer to the Debian Multiarch wiki for details.
Save and close the
/etc/apt/sources.list.d/docker.listfile.
Update the
APTpackage index.
$ sudo apt-get update
Purge the old repo if it exists.
$ sudo apt-get purge lxc-docker
Verify that
APTis pulling from
the right repository.
$ apt-cache policy docker-engine
From now on when you run
apt-get upgrade,
APTpulls from the new repository.
Prerequisites by Ubuntu Version
Ubuntu Xenial 16.04 (LTS)Ubuntu Wily 15.10
Ubuntu Trusty 14.04 (LTS)
For Ubuntu Trusty, Wily, and Xenial, it’s recommended to install the
linux-image-extrakernel
package. The
linux-image-extrapackage
allows you use the
aufsstorage driver.
To install the
linux-image-extrapackage
for your kernel version:
Open a terminal on your Ubuntu host.
Update your package manager.
$ 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,
apparmoris
required. You can install it using:
apt-get install apparmor
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:
Open a terminal on your Ubuntu host.
Update your package manager.
$ sudo apt-get update
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.
Reboot your host.
$ sudo reboot
After your system reboots, go ahead and install Docker.
Install
Make sure you have installed the prerequisites for your Ubuntu version.Then, install Docker using the following:
Log into your Ubuntu installation as a user with
sudoprivileges.
Update your
APTpackage index.
$ sudo apt-get update
Install Docker.
$ sudo apt-get install docker-engine
Start the
dockerdaemon.
$ sudo service docker start
Verify
dockeris 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.
Optional configurations
This section contains optional procedures for configuring your Ubuntu to work better with Docker.Create
a docker group
Adjust
memory and swap accounting
Enable
UFW forwarding
Configure
a DNS server for use by Docker
Configure
Docker to start on boot
Create a Docker group
The dockerdaemon binds to a Unix
socket instead of a TCP port. By default that Unix socket is owned by the user
rootand
other users can access it with
sudo.
For this reason,
dockerdaemon
always runs as the
rootuser.
To avoid having to use
sudowhen
you use the
dockercommand, create
a Unix group called
dockerand
add users to it. When the
dockerdaemon
starts, it makes the ownership of the Unix socket read/writable by the
dockergroup.
Warning: The
dockergroup
is equivalent to the
rootuser;
For details on how this impacts security in your system, seeDocker
Daemon Attack Surface for details.
To create the
dockergroup and
add your user:
Log into Ubuntu as a user with
sudoprivileges.
This procedure assumes you log in as the
ubuntuuser.
Create the
dockergroup.
$ sudo groupadd docker
Add your user to
dockergroup.
$ sudo usermod -aG docker ubuntu
Log out and log back in.
This ensures your user is running with the correct permissions.
Verify your work by running
dockerwithout
sudo.
$ docker run hello-world
If this fails with a message similar to this:
Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
Check that the
DOCKER_HOSTenvironment
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:
Log into Ubuntu as a user with
sudoprivileges.
Edit the
/etc/default/grubfile.
Set the
GRUB_CMDLINE_LINUXvalue
as follows:
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
Save and close the file.
Update GRUB.
$ sudo update-grub
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
2376if
TLS is enabled or
2375when 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:
Log into Ubuntu as a user with
sudoprivileges.
Verify that UFW is installed and enabled.
$ sudo ufw status
Open the
/etc/default/ufwfile
for editing.
$ sudo nano /etc/default/ufw
Set the
DEFAULT_FORWARD_POLICYpolicy
to:
DEFAULT_FORWARD_POLICY="ACCEPT"
Save and close the file.
Reload UFW to use the new setting.
$ sudo ufw reload
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 use 127.0.0.1as
the default
nameserverin
/etc/resolv.conffile.
The NetworkManager also sets up
dnsmasqto
use the real DNS servers of the connection and sets up
nameserver 127.0.0.1in /
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
dnsmasqin
NetworkManager. Though, disabling
dnsmasqmight
make DNS resolution slower on some networks.
The instructions below describe how to configure the Docker daemon running on Ubuntu 14.10 or below. Ubuntu 15.04 and above use
systemdas
the boot and service manager. Refer to control and configure Docker with systemd to
configure a daemon controlled by
systemd.
To specify a DNS server for use by Docker:
Log into Ubuntu as a user with
sudoprivileges.
Open the
/etc/default/dockerfile
for editing.
$ sudo nano /etc/default/docker
Add a setting for Docker.
DOCKER_OPTS="--dns 8.8.8.8"
Replace
8.8.8.8with 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.
Save and close the file.
Restart the Docker daemon.
$ sudo restart docker
Or, as an alternative to the previous procedure, disable
dnsmasqin
NetworkManager (this might slow your network).
Open the
/etc/NetworkManager/NetworkManager.conffile
for editing.
$ sudo nano /etc/NetworkManager/NetworkManager.conf
Comment out the
dns=dnsmasqline:
dns=dnsmasq
Save and close the file.
Restart both the NetworkManager and Docker.
$ sudo restart network-manager
$ sudo restart docker
Configure Docker to start on boot
Ubuntu uses systemdas its boot
and service manager
15.04onwards
and
upstartfor versions
14.10and
below.
For
15.04and up, to configure
the
dockerdaemon to start on
boot, run
$ sudo systemctl enable docker
For
14.10and below
the above installation method automatically configures
upstartto
start the docker daemon on boot
Upgrade Docker
To install the latest version of Docker with apt-get:
$ sudo apt-get upgrade docker-engine
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.
相关文章推荐
- dockerfile 收集
- Docker是用来干什么的?
- docker 搭建php开发环境
- Use Docker Machine to provision hosts on cloud providers
- Get started with Docker Machine and a local VM
- Install Docker Machine
- Docker compose操作指南
- 【原创】docker源码分析(1)---框架与engine
- Centos 下安装Docker 遇到的一些错误
- Docker入门笔记_1_安装docker
- Docker--使用Dockerfile创建镜像--RHEL7.2
- Docker计划将镜像迁移至Alpine Linux平台,简化容器运行环境
- 为Docker容器设置固定IP实现网络联通(1)——通过Pipework为Docker容器设置
- CentOS6.5 下安装Docker
- 自己学Docker:6.Docker的数据持久化之数据卷容器
- centos7安装docker并设置开机启动
- Docker网络基础配置--RHEL7.2
- 【云计算虚拟化】Docker的基本命令使用
- kubernetes调度详解
- Docker配置指南系列(三):Dockerfile(一)