安装 Docker
(提示: 对 Docker 感兴趣的朋友可以加我的微信 ghostcloud2016, 然后我把你加到我们的一个 Docker 爱好者社群里面)
Docker 最核心的程序是 Docker Engine, 根据参数, 它既可以作为后台 daemon 运行, 也可以作为命令行工具使用
1 Linux 上安装
Docker 本身是基于 linux 内核的, 因此只要内核版本足够新, 一般大于 3.10 左右, 就可以运行各种系统的安装方式基本相同, 只是针对各系统的进程管理或者目录结构略有不同下面是 docker 在 linux 系统下的物理逻辑结构:
2 Ubuntu 上安装 Docker
在所有系统中, Docker 对 ubuntu 的支持是最好的, 因此如果使用 docker, 我推荐都适用 ubuntu 系统 Ubuntu 系统的可视化安装以及内核的更新, 我认为都是最好的 Docker 支持的 Ubuntu 系统:
- Ubuntu Wily 15.10
- Ubuntu Vivid 15.04
- Ubuntu Trusty 14.04 (LTS)
Ubuntu Precise 12.04 (LTS) 环境要求:
64 位系统
3.10 以上 linux 内核, 具体可以根据 uname r 进行查看
具体安装步骤:
更新 apt 源
- # 添加 gpg key
- sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
- # 增加 / etc/apt/sources.list.d/docker.list
- # 增加下面一行 (选择你的具体版本)
- Ubuntu precise 12.04: deb https://apt.dockerproject.org/repo ubuntu-precise main
- Ubuntu trustry 14.04: deb https://apt.dockerproject.org/repo ubuntu-trusty main
- Ubuntu trustry 15.04: deb https://apt.dockerproject.org/repo ubuntu-vivid main
- Ubuntu trustry 15.10: deb https://apt.dockerproject.org/repo ubuntu-wily main
- # 执行 apt-get update, 更新源
删除旧版本 Docker
apt-get purge lxc-docker
检查 Docker 的 apt 源是否正常
apt-cache policy docker-engine
不同版本的处理
- #Ubuntu Wily 15.10, Ubuntu Vivid 15.04, Ubuntu Trusty 14.04 (LTS)
- $ sudo apt-get install linux-image-extra-$(uname -r)
linux-image-extra 内核包安装后, 可以使用 aufs
#Ubuntu Precise 12.04(LTS)
对于这个版本的 ubuntu, 需要 3.13 内核及一些相关的包, 执行下面的命令进行安装:
$ sudo apt-get install linux-image-generic-lts-trusty
重启系统 (如果没有安装内核包, 忽略本条)
reboot -nf
安装 docker-engine
- $ sudo apt-get update
- $ sudo apt-get install docker-engine
- $ sudo service docker start
$ sudo docker run hello-world #验证是否安装成功
其他配置
创建 docker 用户
docker daemon 默认绑定在 unix socket 上, 而不是 tcp 的端口而 unix socket 默认是属于 root 用户, 所以其他用户要访问该 socket 需要通过 sudo 命令如果想避免使用 sudo, 你需要将当前用户添加到 docker 用户组具体步骤:
- $ sudo usermod - aG docker ubuntu#退出当前用户,
- 并登入当前用户
$ docker run hello-world#验证是否成功
- # 如果失败会报错:
- Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
调整 Memory 和 Swap 的配额
有时候你在启动时会出现:
- WARNING: Your kernel does not support cgroup swap limit. WARNING: Your
- kernel does not support swap limit capabilities. Limitation discarded.
这个提示说明, 你当前的内核不支持 cgroup 对 swap 的控制如果需要支持, 会使用大约 1% 的系统总内存, 同时会有 10% 左右的性能损失具体步骤如下:
- # 以 root 用户编辑 / etc/default/grub
- # 添加 GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
- # 保存文件, 并执行 sudo update-grub
- #sync
- #reboot 系统
同 UFW 一起使用
如果你使用了 UFW(Uncomplicated Firewall), 你需要做额外的配置, 因为 UFW 默认会屏蔽所有的 Forwarding 流量, 所以你需要 zuo 如下配置:
- $ sudo ufw status
- $ sudo vi /etc/default/ufw
- DEFAULT_FORWARD_POLICY="ACCEPT"
- $ sudo ufw reload
- $ sudo ufw allow 2375/tcp
配置 docker 的 DNS
Ubuntu 系统默认使用 127.0.0.1 作为 DNS 服务器, 具体配置文件为 / etc/resolv.conf 同时, 还使用 dnsmasq 作为 DNS 的缓存当你启动容器的时候, 可能会遇到下面的警告:
- 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, 因此使用外部的默认 DNS(8.8.8.8,8.8.4.4) 这两个地址都是 google 的免费 DNS 地址如果想自己指定 DNS, 具体步骤如下:
- $ sudo vi /etc/default/docker
- DOCKER_OPTS="--dns 8.8.8.8 192.168.1.1"
- $ sudo restart docker
- # 或者修改 dnsmasq
- sudo vi /etc/NetworkManager/NetworkManager.conf
- # 注释掉这一行
- dns=dnsmasq
- # 重启网络
- $ sudo restart network-manager
- # 重启 docker
- $ sudo restart docker
让 Docker 随系统启动
首先需要了解你所使用的系统用的什么启动管理工具, 在 ubuntu 15.04 及其以上版本使用的是 systemd, 在 14.10 及其以下使用的是 upstart 在 15.04 及以上版本需要执行:
$ sudo systemctl enable docker
在 14.10 以下, 安装程序会自动将 docker 加入到 upstart 中
升级 docker
$ apt-get upgrade docker-engine
卸载 docker
- $ sudo apt-get purge docker-engine
- $ sudo apt-get autoremove --purge docker-engine
- # 删除 docker 的镜像及工作目录
- $ rm -rf /var/lib/docker
3 CentOS 上安装 docker
3.1 环境要求
CentOS 7.X 及其以后版本
3.10 以后版本
3.2 通过脚本安装
更新 yum 源:
$ sudo yum update
下载脚本并安装:
$ curl -sSL https://get.docker.com/ | sh
启动服务:
$ sudo service docker start
验证 docker:
$ sudo docker run hello-world
3.3 通过 yum 安装
更新源:
$ sudo yum update
添加源及 gpg key:
- $ sudo tee /etc/yum.repos.d/docker.repo
- <<-'EOF'
- [dockerrepo]
- name=Docker Repository
- baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
- enabled=1
- gpgcheck=1
- gpgkey=https://yum.dockerproject.org/gpg
- EOF
安装 docker-engine:
$ sudo yum install docker-engine
启动 docker daemon:
$ sudo service docker start
验证 docker:
$ sudo docker run hello-world
3.4 添加 docker 用户组 (同 ubuntu)
$ sudo usermod -aG docker your_username
3.5 随系统启动
$ sudo chkconfig docker on`
3.6 卸载
- # 检查是否安装
- $ yum list installed | grep docker
- yum list installed | grep docker
- docker-engine.x86_64 1.7.1-1.el7 @/docker-engine-1.7.1-1.el7.x86_64.rpm
- # 删除包
- $ sudo yum -y remove docker-engine.x86_64
- # 删除 docker 镜像及中间文件
- $ rm -rf /var/lib/docker
4 Mac 上安装
背景
Mac OSX 下 Docker 通过 Docker Toolbox 来进行安装, Docker Toolbox 是 Docker 公司的一个工具包, 里面包括:
Docker Machine: 运行 docker-machine 程序
Docker Engine: Docker 可执行程序
Docker Compose: docker-compose 程序
Kitematic: Docker GUI
包含预定义的 shell 脚本, 用于命令行运行环境
Oracle VM VirtualBox
由于 Docker Daemon 使用了 linux kernel 的众多特性, 我们不能在 OS X 本地运行 Docker, 所以我们必须使用 docker-machine 来创建并关联到一个虚拟机这个虚拟机就是 Docker 的实际运行环境下面是 docker 在 mac 或 windows 下的物理逻辑结构
环境要求: OS X 10.8 及其以上版本
安装 Toolbox
地址是 https://www.docker.com/toolbox, 下载完成后根据提示进行安装默认情况, Docker Toolbox 安装在 / usr/local/bin, 每个用户都可以使用这些可执行程序, 同时会安装带有 Docker 运行环境的 Virtual Box
启动 Docker Machine 通过 Launchpad 启动程序:
执行程序后, 会自动执行一段 shell 脚本启动虚拟机并进入一个 shell 终端
Hello World, Docker
我们先来执行一个最简单的 docker 容器:
docker run hello-world
这个命令包含三部分:
docker - 所有 docker 命令的可执行程序, 默认在 / usr/local/bin/docker
run - docker 的子命令, 该命令首先查看本地是否有相应的镜像, 如果没有将默认从 Docker Hub 进行拉取, 之后再运行该镜像
hello-world - 镜像名称, 这个镜像就是 echo 一句 hello world
容器本身是一个简化的经过裁减的 linux 系统, 容器可以运行一个简单命令, 就像上例一样; 也可以运行比较复杂的应用程序, 比如: 数据库, web 后台等那 hello-world 镜像是由谁生成的呢? 是 Docker 官方生成的, 通过 docker 容器, 你可以在任何系统容器中运行镜像
查找 Image
你可以在 docker hub 主页, 或者本机通过命令行进行搜索, 一般我喜欢通过命令行进行搜索例如, 搜索 ubuntu:
- root@gctest:~# docker search ubuntu
- NAME DESCRIPTION STARS OFFICIAL AUTOMATED
- ubuntu Ubuntu is a Debian-based Linux operating s... 2761 [OK]
- ubuntu-upstart Upstart is an event-based replacement for ... 48 [OK]
- dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC on po... 28 [OK]
- torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 25 [OK]
- sequenceiq/hadoop-ubuntu An easy way to try Hadoop on Ubuntu 24 [OK]
- ubuntu-debootstrap debootstrap --variant=minbase --components... 20 [OK]
- tleyden5iwx/ubuntu-cuda Ubuntu 14.04 with CUDA drivers pre-installed 18 [OK]
- neurodebian NeuroDebian provides neuroscience research... 15 [OK]
- rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 15 [OK]
- consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC sessi... 7 [OK]
- n3ziniuka5/ubuntu-oracle-jdk Ubuntu with Oracle JDK. Check tags for ver... 5 [OK]
- nuagebec/ubuntu Simple always updated Ubuntu docker images... 4 [OK]
- ioft/armhf-ubuntu [ABR] Ubuntu Docker images for the ARMv7(a... 4 [OK]
- nimmis/ubuntu This is a docker images different LTS vers... 3 [OK]
- maxexcloo/ubuntu Docker base image built on Ubuntu with Sup... 2 [OK]
- sylvainlasnier/ubuntu Ubuntu 15.04 root docker images with commo... 1 [OK]
densuke/ubuntu-jp-remix Ubuntu Linux 日本語 remix 風味 1 [OK]
- seetheprogress/ubuntu Ubuntu image provided by seetheprogress us... 1 [OK]
- rallias/ubuntu Ubuntu with the needful 0 [OK]
- tvaughan/ubuntu https://github.com/tvaughan/docker-ubuntu 0 [OK]
- zoni/ubuntu 0 [OK]
- partlab/ubuntu Simple Ubuntu docker images. 0 [OK]
- teamrock/ubuntu TeamRock's Ubuntu image configured with AW... 0 [OK]
- konstruktoid/ubuntu Ubuntu base image 0 [OK]
- esycat/ubuntu Ubuntu LTS 0 [OK]
在搜索结果中, 你可以看到有的是通过 "/" 划分的, 有的没有凡是没有 "/" 分级的就是 Docker Hub 自带的镜像; 否则该镜像就出自于某个 Docker Hub 用户, 从这里你也可以看到, Docker Hub 是一个 Public 仓库, 你的任何镜像, 别人都可以搜索到因此, 一定要注意安全, 不要把敏感信息放上去了
拉取镜像
我们尝试拉取一个 centos 镜像:
- root@gctest:~# docker pull centos
- Using default tag: latest
- Pulling repository docker.io/library/centos
- ce20c473cd8a: Pulling dependent layers
- 47d44cb6f252: Pulling metadata
- ce20c473cd8a: Download complete
- 168a69b62202: Download complete
- 812e9d9d677f: Download complete
- 4234bfdd88f8: Pulling fs layer
- 4234bfdd88f8: Download complete
- Status: Downloaded newer image for centos:latest
- root@gctest:~# docker images
- REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
- ghostcloud.cn:5000/gcali 1.5 6ff13e5ab907 29 hours ago 213.9 MB
- centos latest ce20c473cd8a 7 weeks ago 172.3 MB
编译 Image 除了拉取官方的 Image, 也可以自己编译 Image, 编译的方法和 Makefile 类似
创建 Dockerfile
- root@gctest:~# mkdir myimage
- root@gctest:~# cd myimage/
- root@gctest:~/myimage# vi Dockerfile
- root@gctest:~/myimage# cat Dockerfile
- FROM centos:latest
- CMD echo "Hello myimage"
编译 Image
- root@gctest:~/myimage# docker build -t myimage .
- Sending build context to Docker daemon 2.048 kB
- Step 1 : FROM centos:latest
- ---> ce20c473cd8a
- Step 2 : CMD echo "Hello myimage"
- ---> Running in 1421c66fa1a6
- ---> 2408b71c40c0
- Removing intermediate container 1421c66fa1a6
- Successfully built 2408b71c40c0
- root@gctest:~/myimage# docker images
- REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
- myimage latest 2408b71c40c0 6 seconds ago 172.3 MB
- ghostcloud.cn:5000/gcali 1.5 6ff13e5ab907 30 hours ago 213.9 MB
- centos latest ce20c473cd8a 7 weeks ago 172.3 MB
运行自己的 Image
- root@gctest:~/myimage# docker run myimage
- Hello myimage
来源: https://yq.aliyun.com/articles/132