【docker】 get started - with ubuntu

docker 是什么?docker 起初是dotCloud公司的一个业余项目, 是基于Go和LXC实现的一个轻量级的操作系统虚拟化方案。他可以实现更快的交付部署、更高效的虚拟化、更耿丹的迁移拓展和管理等,具体的在后面会慢慢体会到。首先我们先要把 docker 装到你的机子上,下面以 ubuntu 为例:

docker 获取安装


最新的安装说明, 请查阅 官方文档

首先查看 ubuntu 发行版本号, 文档上针对不同版本的系统会有一些注意事项。

$ uname -r
3.13.0-98-generic

导入 apt key:

$ 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

选择对应的仓库, 根据 Ubuntu version 选择 Repository:

选择后, 写入 docker.list:

$ echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" | sudo tee /etc/apt/sources.list.d/docker.list
$ sudo apt-get update
$ apt-cache policy docker-engine
docker-engine:
Installed: (none)
Candidate: 1.12.3-0~trusty
Version table:
1.12.3-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.12.2-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.12.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.12.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.11.2-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.11.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.11.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.10.3-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.10.2-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.10.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.10.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.9.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.9.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.8.3-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.8.2-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.8.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.8.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.7.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.7.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.6.2-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.6.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.6.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.5.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages

对于以下版本:

  • Ubuntu Xenial 16.04 (LTS)
  • Ubuntu Wily 15.10
  • Ubuntu Trusty 14.04 (LTS)

推荐安装 linux-image-extra-* 内核包, 因为 linux-image-extra-* 包允许你使用 aufs 存储驱动.

安装如下:

$ sudo apt-get update
sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

安装 docker-engine:

$ sudo apt-get update
$ sudo apt-get install docker-engine
$ sudo service docker start

hello-world 测试一下 docker 运行情况


由于本地没有hello-world 这个image, docker会去远程搜索并拉取下来。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest

Hello 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.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

Hello from Docker! This message shows that your installation appears to be working correctly.

恭喜你, docker 正常工作。

其他


docker user


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.

因为 docker daemon 绑定了unix socket 而不是 tcp socket, 而 Unix socket 一般被root所拥有,正因如此,docker daemon 总是以root身份运行。

为了避免总是使用sudo来执行, 可以创建一个dockergroup, 当 docker daemon 启动时, 会使 Unix socket 的 read/writable 拥有权设为 docker 组.

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

Log out and log back in ?
This ensures your user is running with the correct permissions.

$ docker run hello-world

上面不用sudo来 run hello-world, 如果失败, 会有类似下面的提示:

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

卸载


移除 docker-engine 包:

$ sudo apt-get purge docker-engine

移除 docker-engine 包及无用依赖:

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

移除所有 images, containers, and volumes:

$ rm -rf /var/lib/docker

异常情况


异常:执行用户不在docker组

如果不能run,提示Cannot connect to the Docker daemon. Is the docker daemon running on this host,可能是没有把当前用户加入docker组。

sudo usermod -aG docker your-user
Log out and log back in

参考这里

异常:服务未启动

docker run hello-world
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

sudo service docker start
sudo docker run hello-world

参考


https://docs.docker.com/linux/started/
https://docs.docker.com/engine/installation/linux/ubuntulinux/