Docker安装部署
什么是Docker
物理机、VM、容器的区别
- 容器虚拟化的是操作系统而不是硬件,容器之间是共享同一套操作系统资源的
- 虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统
此处借用网上的图 图片来源及Docker概念详解
物理机
虚拟机
容器
Docker在宿主机器的操作系统上创建Docker引擎,直接在宿主主机的操作系统上调用硬件资源,而不是虚拟化操作系统和硬件资源,所以操作速度快。
Docker安装
建议是使用Linux内核在3.10版本以上
[root@localhost ~]uname -r
3.10.0-1062.el7.x86_64
- 安装依赖
docker依赖于系统的一些必要的工具,可以提前安装
[root@localhost /] yum install -y yum-utils device-mapper-persistent-data lvm2
- 添加软件源
官方源比较慢,国内推荐阿里云镜像
[root@localhost /]yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 安装docker-ce
[root@localhost /]yum -y install docker-ce-18.03.1.ce-1.el7.centos
- 启动服务
(启动docker)
[root@localhost /]systemctl start docker
(设为开机启动)
[root@localhost /]systemctl enable docker
- 查看版本
[root@localhost conf] docker -v
Docker version 19.03.5, build 633a0ea
运行helloworld
- 查找镜像
命令: docker search 镜像名
从Docker Hub查找镜像
[root@localhost /] docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 1091 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demonstr… 136
tutum/hello-world Image to test docker deployments. Has Apache… 65 [OK]
搜索结果说明:
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
- 获取镜像
命令: docker pull 镜像名称:版本号
不写版本号则默认下载最新版
[root@localhost /] docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
- 查看已下载的镜像
[root@localhost /] docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 11 months ago 1.84kB
- 运行容器
[root@localhost /] docker run hello-world
Hello from Docker!
- 查看运行中的容器
[root@localhost /] docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b6299cf4013 hello-world "/hello" About a minute ago Exited (0) About a minute ago strange_lederberg
- 删除容器
命令: docker rm 容器ID/容器名
容器ID不需要完整,能唯一确定一个容器即可
[root@localhost /] docker rm 9b6
9b6
[root@localhost /] docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 删除镜像
命令: docker rmi 镜像名称
[root@localhost /] docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 11 months ago 1.84kB
[root@localhost /] docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
[root@localhost /] docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
- 修改容器
命令: docker update 容器名称
##修改容器开机自启动
docker update 容器名 --restart=always
Docker常用命令
命令 | 作用 |
---|---|
搜索镜像 | docker search 镜像名称 |
获取镜像 | docker pull 镜像名称:版本号(无版本号默认最新) |
查看已下载的镜像 | docker images |
运行容器 | docker run 镜像名称:版本号 |
查看运行中的容器 | docker ps |
查看所有创建的容器 | docker ps -a |
停止运行中的容器 | docker stop 容器名称或容器ID(无需全部,唯一指定即可) |
删除容器 | docker rm 容器名称或容器ID |
删除镜像 | docker rmi 镜像名称 |