Docker 安装

Docker 安装

转载链接

可以在任何操作系统上安装 Docker,无论是 Mac,Windows,Linux 还是任何云服务器。Docker 引擎在 Linux 发行版上运行。 在这里,我们将以 Linux Ubuntu Server 16.04 作为演示安装 Docker 引擎的过程。

前提条件

Docker 需要两个重要的安装要求:

  • 它仅适用于 64 位 Linux 安装
  • 它需要 Linux 内核版本 3.10 或更高版本。

要查看当前的内核版本,请打开终端并键入uname -r命令以查看内核版本:

lusifer@UbuntuBase:~$ uname -r
4.4.0-21-generic

查看操作系统是32位还是64位:

lusifer@UbuntuBase:~$ uname --m
x86_64

使用脚本安装 Docker

使用在线安装脚本

先更新数据源

curl -sSL https://get.daocloud.io/docker | sh

执行后会自动下载并安装 Docker 及依赖包

lusifer@UbuntuBase:~$ curl -sSL https://get.daocloud.io/docker | sh
# Executing docker install script, commit: 49ee7c1
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c apt-get install -y -qq apt-transport-https ca-certificates curl software-properties-common >/dev/null
+ sudo -E sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null
+ sudo -E sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial edge" > /etc/apt/sources.list.d/docker.list
+ [ ubuntu = debian ]
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sudo -E sh -c docker version
Client:
 Version:      17.10.0-ce
 API version:  1.33
 Go version:   go1.8.3
 Git commit:   f4ffd25
 Built:        Tue Oct 17 19:04:16 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.10.0-ce
 API version:  1.33 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   f4ffd25
 Built:        Tue Oct 17 19:02:56 2017
 OS/Arch:      linux/amd64
 Experimental: false
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker lusifer

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

完成后有个提示

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker lusifer

Remember that you will have to log out and back in for this to take effect!

当要以非 root 用户可以直接运行 docker 时,需要执行 sudo usermod -aG docker lusifer 命令,然后重新登陆,否则会有如下报错

lusifer@UbuntuBase:~$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.33/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

启动 Docker 后台服务

service docker start

查看 Docker 当前版本

lusifer@UbuntuBase:~$ docker version
Client:
 Version:      17.10.0-ce
 API version:  1.33
 Go version:   go1.8.3
 Git commit:   f4ffd25
 Built:        Tue Oct 17 19:04:16 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.10.0-ce
 API version:  1.33 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   f4ffd25
 Built:        Tue Oct 17 19:02:56 2017
 OS/Arch:      linux/amd64
 Experimental: false

配置加速器

由于网络原因,我们在 pull Image 的时候,从 Docker Hub 上下载会很慢... 所以,国内的 Docker 爱好者们就添加了一些国内的镜像 (mirror),方便大家使用。

修改配置文件

nano /lib/systemd/system/docker.service

添加 --registry-mirror=https://jxus37ac.mirror.aliyuncs.com 到 ExecStart:

保存配置:systemctl daemon-reload

重启服务:service docker restart

第一个 Docker 应用程序


Docker 允许你在容器内运行应用程序,使用 docker run 命令来在容器内运行一个应用程序。

输出 Hello Docker:

lusifer@UbuntuBase:~$ docker run ubuntu:15.10 /bin/echo "Hello Docker"
Hello Docker

参数解释:

  • docker:Docker 的二进制执行文件
  • run:与前面的 docker 组合来运行一个容器
  • ubuntu:15.10:指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像
  • /bin/echo "Hello Docker":在启动的容器里执行的命令

以上命令完整的意思可以解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello Docker",然后输出结果。

运行交互式的容器


我们通过 docker 的两个参数 -i -t,让 docker 运行的容器实现"对话"的能力

lusifer@UbuntuBase:~$ docker run -it ubuntu:15.10 /bin/bash
root@76ab065de67b:/#

参数解释:

  • -t:在新容器内指定一个伪终端或终端
  • -i:允许你对容器内的标准输入进行交互

此时我们已进入一个 ubuntu15.10 系统的容器

我们尝试在容器中运行命令 cat /proc/version 和 ls 分别查看当前系统的版本信息和当前目录下的文件列表

root@76ab065de67b:/# cat /proc/version
Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) ) #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016
root@76ab065de67b:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

退出容器:

  • 运行 exit
  • 使用 CTRL + D

以后台模式运行容器


使用以下命令创建一个以进程方式运行的容器

lusifer@UbuntuBase:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello docker; sleep 1; done"
0977407542d16c5699a2cc0455d977cb5d8ef011a1a762cdcf2323851f6b3ed3

在输出中,我们没有看到期望的"hello docker",而是一串长字符

0977407542d16c5699a2cc0455d977cb5d8ef011a1a762cdcf2323851f6b3ed3

这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。

首先,我们需要确认容器有在运行,可以通过 docker ps 来查看

lusifer@UbuntuBase:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0977407542d1        ubuntu:15.10        "/bin/sh -c 'while..."   2 seconds ago       Up 1 second                             thirsty_kowalevski

CONTAINER ID:容器ID

NAMES:自动分配的容器名称

在容器内使用 docker logs 命令,查看容器内的标准输出

lusifer@UbuntuBase:~$ docker logs 0977407542d1
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
lusifer@UbuntuBase:~$ docker logs thirsty_kowalevski
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker

与正在运行的容器交互

docker exec -it <container> /bin/bash



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值