Docker简介与安装

Docker

1、Docker简介

1.1、Docker是什么

Docker属于Linux容器的一种封装,提供简单易用的容器使用接口。

Docker将应用程序与其依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。有了Docker,就不需要担心环境问题。

1.2、Docker的作用

  • 提供一次性环境:比如在本地测试他人软件、持续集成的时候提供单元测试和环境架构
  • 提供弹性的云服务:Docker可以随意开关,很适合动态扩容、缩容
  • 组件微服务架构:通过多个容器,一台机器可以跑多个服务,因此在本机就可以模拟出微服务架构

1.3、Docker架构

Docker包括三个基本概念:

  • 镜像(image):Docker就相当于一个root文件系统
  • 容器(Container):镜像和容器的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体,容器可以被创建、启动、停止、删除、暂停等
  • 仓库(Repository):仓库可看做一个代码控制中心,用来保存镜像
概念说明
Docker 镜像(Images)Docker 镜像是用于创建 Docker 容器的模板,比如 Ubuntu 系统。
Docker 容器(Container)容器是独立运行的一个或一组应用,是镜像运行时的实体。
Docker 客户端(Client)Docker 客户端通过命令行或者其他工具使用 Docker SDK (https://docs.docker.com/develop/sdk/) 与 Docker 的守护进程通信。
Docker 主机(Host)一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker 仓库(Registry)Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。
Docker MachineDocker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

2、Docker安装

2.1、示例环境

uname -a
Linux ubuntu 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

2.2、Docker安装

2.2.1、卸载旧版本

如果已安装Docker的旧版本,请卸载

root@ubuntu:~# apt-get remove docker docker-engine docker.io containerd runc
2.2.2、Docker仓库安装

在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,可以从仓库安装和更新 Docker 。

1、安装仓库

# 更新apt包索引
root@ubuntu:~# apt-get update     
# 安装apt依赖包,用于通过https来获取仓库
root@ubuntu:~# apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

在这里插入图片描述

2、添加Docker官方密钥

root@ubuntu:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo: unable to resolve host ubuntu
OK
# 验证现在是否拥有带有指纹的密钥
root@ubuntu:~# apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

3、设置稳定版仓库

root@ubuntu:~# add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>   $(lsb_release -cs) \
>   stable"

4、安装Docker Engine-Community

  • 安装最新版本的 Docker Engine-Community 和 containerd
apt-get install docker-ce docker-ce-cli containerd.io
  • 安装特定版本
# 列出仓库中可用版本
root@ubuntu:~# apt-cache madison docker-ce
 docker-ce | 5:19.03.5~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.4~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.3~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
......
# 安装选择的版本
root@ubuntu:~# apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

5、测试是否安装成功

root@ubuntu:~# docker run hello-world
# 有以下输出说明安装成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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.
    (amd64)
 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 ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

其他操作系统安装Docker请参照Docker Documentation

2.2.3、用户

不能使用root用户的情况下

Docker 需要用户具有 sudo 权限,为了避免每次命令都输入sudo,可以把用户加入 Docker 用户组

$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ newgrp docker      # 激活对组的更改
$ docker run hello-world    # 不带sudo,来验证

docker组授予与root 用户等效的特权。有关如何影响系统安全性的详细信息,请参阅 Docker Daemon Attack Surface

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值