CentOS下Docker安装部署及常用命令

Docker安装部署

什么是Docker

物理机、VM、容器的区别
  1. 容器虚拟化的是操作系统而不是硬件,容器之间是共享同一套操作系统资源的
  2. 虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统
    此处借用网上的图 图片来源及Docker概念详解

物理机
在这里插入图片描述

虚拟机

在这里插入图片描述

容器
在这里插入图片描述
Docker在宿主机器的操作系统上创建Docker引擎,直接在宿主主机的操作系统上调用硬件资源,而不是虚拟化操作系统和硬件资源,所以操作速度快。

Docker安装

建议是使用Linux内核在3.10版本以上

[root@localhost ~]uname -r
3.10.0-1062.el7.x86_64
  1. 安装依赖
    docker依赖于系统的一些必要的工具,可以提前安装
[root@localhost /] yum install -y yum-utils device-mapper-persistent-data lvm2
  1. 添加软件源
    官方源比较慢,国内推荐阿里云镜像
[root@localhost /]yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  1. 安装docker-ce
[root@localhost /]yum -y install docker-ce-18.03.1.ce-1.el7.centos
  1. 启动服务
(启动docker)
[root@localhost /]systemctl start docker
(设为开机启动)
[root@localhost /]systemctl enable docker
  1. 查看版本
[root@localhost conf] docker -v
Docker version 19.03.5, build 633a0ea

运行helloworld

  1. 查找镜像
    命令: 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: 自动构建。
  1. 获取镜像
    命令: 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
  1. 查看已下载的镜像
[root@localhost /] docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
hello-world                                            latest              fce289e99eb9        11 months ago       1.84kB
  1. 运行容器
[root@localhost /] docker run hello-world
Hello from Docker!
  1. 查看运行中的容器
[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
  1. 删除容器
    命令: docker rm 容器ID/容器名
    容器ID不需要完整,能唯一确定一个容器即可
[root@localhost /] docker rm 9b6
9b6
[root@localhost /] docker ps -a
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                     PORTS                                                  NAMES
  1. 删除镜像
    命令: 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
  1. 修改容器
    命令: 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 镜像名称
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值