Docker容器笔记

181 阅读1分钟

Docker学习笔记

Ubuntu安装Docker

使用脚本自动安装

curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh --mirror Aliyun

Docker 镜像加速器

对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)

{
    "registry-mirrors": [
        "https://registry.docker-cn.com"
    ]
}

镜像

1.获取Docker

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库号[:标签]

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

Docker 镜像仓库地址:地址的格式一般是 <域名/IP>[:端口号]。默认地址是 Docker Hub。 仓库名:如之前所说,这里的仓库名是两段式名称,即 <用户名>/<软件名>。对于 Docker Hub,如果不给出用户名,则默认为 library,也就是官方镜像。 比如:

$ docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
bf5d46315322: Pull complete
9f13e0ac480c: Pull complete
e8988b5b3097: Pull complete
40af181810e7: Pull complete
e6f7c7e5c03e: Pull complete
Digest: sha256:147913621d9cdea08853f6ba9116c2e27a3ceffecf3b492983ae97c3d643fbbe
Status: Downloaded newer image for ubuntu:16.04

上面的命令中没有给出 Docker 镜像仓库地址,因此将会从 Docker Hub 获取镜像。而镜像名称是 ubuntu:16.04,因此将会获取官方镜像 library/ubuntu 仓库中标签为 16.04 的镜像。

2. 镜像运行容器

docker run -it --rm ubuntu:16.04 bash
  • -it:-i(交互式操作),-t(终端)
  • -rm:退出容器时同时把容器删除
  • ubuntu:16.04:指定启动容器的镜像
  • bash:用于交互的Shell