Docker 学习记录

Docker 从入门到放弃


三个基本概念

  • 镜像Image
  • 容器Container
  • 仓库`Repository

镜像:类似面向对象程序设计中的,静态的定义

容器:类似期中的实例,镜像运行时的实体,可以被创建、启动、停止、删除、暂停。

Docker 常用命令


  1. 安装
# 卸载旧版本
$ sudo apt-get remove docker \
            docker-engine \
            docker.io

# 更新源
$ sudo apt-get update
# 安装
$ sudo apt-get install \
             apt-transport-https \
             ca-certificates \
             curl \
             software-properties-common

# 国内源添加密钥(如果没有代理服务器)
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# 官方源添加密钥(二者选其一)
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 在 source.list 添加Docker软件源
$ sudo add-apt-repository \
 "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
 $(lsb_release -cs) \
 stable"
 
# 官方源(二者选其一)
# $ sudo add-apt-repository \
#       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
#       $(lsb_release -cs) \
#       stable"
 
 
# 安装Docker CE
$ sudo apt-get update
$ sudo apt-get install docker-ce

# 脚本自动化安装(二选一)
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

# 启动 Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker

# 建立 Docker 用户组
$ sudo groupadd docker

# 将当前用户加入 docker 组
$ sudo usermod -aG docker ruanheng

# 测试当前Docker是否安装正确
$ docker run hello-world
# 输出以下信息则表示安装成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker!
This message shows that your installation appears to be working correctly.
...

# 安装镜像加速(鉴于国内拉去镜像困难,有加速器则可以忽略 :)

$ sudo vim /etc/docker/daemon.json

# 加入以下内容
{
"registry-mirrors": [
  "https://registry.docker-cn.com"
]
}

# 重启服务
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

# 检查是否生效
$ docker info 

# 如下提示则表示安装成功,继续下一步 :)
Registry Mirrors:
  https://registry.docker-cn.com/

  1. 获取Docker三大件之一:镜像

    # 拉取镜像命令
    $ docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
    
    

    具体的选项可以通过 docker pull --help 命令看到,这里我们说一下镜像名称的格式。

  • Docker 镜像仓库地址:地址的格式一般是 <域名/IP>[:端口号]。默认地址是 Docker Hub。

  • 仓库名:如之前所说,这里的仓库名是两段式名称,即 <用户名>/<软件名>。对于 Docker Hub,如果不给出用户名,则默认为 library,也就是官方镜像。

    # 尝试拉取 Ubuntu16.04 镜像
    $ docker pull ubuntu:16.04
    16.04: Pulling from library/ubuntu
    18d680d61657: Pull complete 
    0addb6fece63: Pull complete 
    78e58219b215: Pull complete 
    eb6959a66df2: Pull complete 
    Digest: sha256:76702ec53c5e7771ba3f2c4f6152c3796c142af2b3cb1a02fce66c697db24f12
    
posted @ 2018-11-15 22:36  抑己  阅读(93)  评论(0)    收藏  举报