2.1 核心概念
三大核心概念:镜像 容器 仓库
2.1.1 Dokcer 镜像
Docker镜像类似于虚拟机镜像,可以将它理解为一个只读模板。
镜像是创建Docker容器的基础。
2.1.2 Docker 容器
Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用。容器是从镜像创建的应用实例。可以将其启动、停止、启动、删除,而这些容器都是彼此相互隔离的、互不可见的。
可以把容器看成一个简易版的Linux系统环境(包括root用户权限、进程空间、用户空间和网络空间等)以及运行再其中的应用程序打包而成的盒子。
镜像自身是只读的。容器从镜像启动的时候,会在镜像的最上层创建一个可写层。
2.1.3 Docker 仓库
Docker仓库类似于代码仓库,它是Docker集中存放镜像文件的场所。
仓库中存放某一类镜像,往往包括多个镜像文件,通过不同的标签(tag)来进行区分。例如存放Ubuntu操作系统镜像的仓库称为Ubuntu仓库,其中可能包括14.04、12.04、15.10等不同版本的镜像。
2.2 安装docker
2.2.1 在Centos7.4上安装
[root@monitor ~]# yum -y install docker-io
启动docker后台服务
[root@monitor ~]# service docker start
测试运行hello-world
[root@monitor ~]# docker run hello-world
2.2.2 镜像加速
[root@monitor ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://1f03k6nh.mirror.aliyuncs.com"]
}
[root@monitor ~]# service docker restart
(这个地址需要在阿里云管理控制台-容器镜像服务-镜像库-镜像加速器获取)
2.2.3 登陆镜像市场
[root@monitor ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: lxldoudou
Password:
Login Succeeded
2.2.4 运行交互式的容器
[root@monitor ~]# docker run -i -t ubuntu:15.10 /bin/bash
Unable to find image 'ubuntu:15.10' locally
Trying to pull repository docker.io/library/ubuntu ...
15.10: Pulling from docker.io/library/ubuntu
7dcf5a444392: Pull complete
759aa75f3cee: Pull complete
3fa871dc8a2b: Pull complete
224c42ae46e7: Pull complete
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for docker.io/ubuntu:15.10
root@f141acff7de7:/#
-t:在新容器内指定一个伪终端或终端。
-i:允许你对容器内的标准输入 (STDIN) 进行交互。
我们可以通过运行exit命令或者使用CTRL+D来退出容器
2.2.5 启动容器(后台模式)
[root@monitor ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
f709fa87f8967153d816a5a55b17a020ca48e9303929a94e73c920cfe01f7405
(这个是容器ID具有唯一性,自动分配)
[root@monitor ~]# docker logs f709
hello world
hello world
hello world
docker logs命令,查看容器内的标准输出
2.2.6 停止容器
[root@monitor ~]# docker stop f709
2.3 配置Docker服务
为了避免每次使用Docker命令都要用特权身份,可以将当前用户加入安装中自动创建docker用户组。
[tom@monitor ~]$ sudo usermod -aG USER_NAME
用户更新组信息后,退出并重新登陆后即可生效。