Docker使用详解

安装Docker

卸载旧版本

yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine

开始安装

# 1. 需要安装依赖
yum install -y yum-utils

# 2. 设置镜像仓库 
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

已加载插件:fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

# 3. 安装最新版的docker引擎
yum install docker-ce docker-ce-cli containerd.io

已安装:
  containerd.io.x86_64 0:1.4.8-3.1.el7                  docker-ce.x86_64 3:20.10.7-3.el7                 
  docker-ce-cli.x86_64 1:20.10.7-3.el7                 

作为依赖被安装:
  audit-libs-python.x86_64 0:2.8.5-4.el7               checkpolicy.x86_64 0:2.5-8.el7                    
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8   docker-ce-rootless-extras.x86_64 0:20.10.7-3.el7  
  docker-scan-plugin.x86_64 0:0.8.0-3.el7              fuse-overlayfs.x86_64 0:0.7.2-6.el7_8             
  fuse3-libs.x86_64 0:3.6.1-4.el7                      libcgroup.x86_64 0:0.41-21.el7                    
  libseccomp.x86_64 0:2.3.1-4.el7                      libsemanage-python.x86_64 0:2.5-14.el7            
  policycoreutils-python.x86_64 0:2.5-34.el7           python-IPy.noarch 0:0.75-6.el7                    
  setools-libs.x86_64 0:3.3.8-4.el7                    slirp4netns.x86_64 0:0.4.3-4.el7_8 

启动与测试

# 启动Docker
systemctl start docker

# 测试Docker
docker run hello-world

[root@salve-1 /]# systemctl start docker
[root@salve-1 /]# docker run hello-world
Unable to find image 'hello-world:latest' locally  # 没有找到镜像
latest: Pulling from library/hello-world # 去官方库拉取
b8dfde127a29: Pull complete 
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
# 此消息显示您的安装似乎正在正常工作。

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

# 查看版本号
docker version

# 查看镜像
[root@salve-1 /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   4 months ago   13.3kB

阿里云镜像加速器配置

1. 安装/升级Docker客户端
推荐安装1.10.0以上版本的Docker客户端,参考文档docker-ce

2. 配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
   
  "registry-mirrors": ["https://c1xmoeh1.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

其它信息

# Docker的默认工作目录
/var/lib/docker

流程图

在这里插入图片描述

在这里插入图片描述

命令总结

常用命令

docker version # 显示版本号
docker info # 显示docker的系统信息
docker 命令 --help # 查看命令的帮助信息(命令代表所有docker命令)

镜像命令

查看所有镜像
[root@salve-1 docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   4 months ago   13.3kB

REPOSITORY # 镜像的仓库源
TAG       # 镜像的标签
IMAGE ID  # 镜像的ID     
CREATED   # 创建日期     
SIZE   # 大小

# 可选参数
Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs
搜索镜像
[root@salve-1 docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11164     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4235      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   830                  [OK

# 可选参数
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
      
[root@salve-1 docker]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11164     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4235      [OK] 
下载镜像
[root@salve-1 docker]# docker pull mysql
Using default tag: latest  # 默认下载的是最新版
latest: Pulling from library/mysql
latest: Pulling from library/mysql
33847f680f63: Pull complete  # 从这里开始,就分层下载,这是docker的核心,联合文件下载
5cb67864e624: Pull complete 
1a2b594783f5: Pull complete 
b30e406dd925: Pull complete 
48901e306e4c: Pull complete 
603d2b7147fd: Pull complete 
802aa684c1c4: Pull complete 
715d3c143a06: Pull complete 
6978e1b7a511: Pull complete 
f0d78b0ac1be: Pull complete 
35a94d251ed1: Pull complete 
36f75719b1a9: Pull complete 
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实的地址

# 以下两个命令是等价的
docker pull mysql 
docker pull docker.io/library/mysql:latest

# 也可以指定版本下载,但是版本号必须是真是存在的才行
[root@salve-1 docker]# docker pull mysql:5.7
5.7: Pulling from library/mysql
33847f680f63: Already exists 
5cb67864e624: Already exists 
1a2b594783f5: Already exists 
b30e406dd925: Already exists 
48901e306e4c: Already exists 
603d2b7147fd: Already exists 
802aa684c1c4: Already exists 
5b5a19178915: Pull complete 
f9ce7411c6e4: Pull complete 
f51f6977d9b2: Pull complete 
aeb6b16ce012: Pull complete 
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

# 查看上面下载的两个镜像
[root@salve-1 docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       8cf625070931   2 days ago     448MB
mysql         latest    c60d96bd2b77   2 days ago     514MB
hello-world   latest    d1165f221234   4 months ago   13.3kB
删除镜像
[root@salve-1 docker]# docker rmi -f 8cf625070931
Untagged: mysql:5.7
Untagged: mysql@sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Deleted: sha256:8cf6250709314f2fcd2669e8643f5d3bdebfe715bddb63990c8c96e5d261d6fc
Deleted: sha256:452fe6896278c26338d547f8d1092011d923785247c46629b374d3477fe28c84
Deleted: sha256:bd40bf60af5d06e6b93eaf5a648393d97f70998faa3bfa1b85af55b5a270cb35
Deleted: sha256:c43e9e7d1e833650e0ed54be969d6410efa4e7fa6e27a236a44a2b97e412ee93
Deleted: sha256:70f18560bbf492ddb2eadbc511c58c4d01e51e8f5af237e3dbb319632f16335b

# 8cf625070931 是镜像ID

# 删除全部
docker rmi -f $(docker images -aq)

容器命令

# 首先下载一个centos镜像
docker pull centos

# 启动并进入容器
docker run -it centos /bin/bash

[root@salve-1 docker]# docker run -it centos /bin/bash
[root@dbf663a27f57 /]# 

# 退出容器
[root@dbf663a27f57 /]# exit
exit

Ctrl + Q + P #退出单不停止
列出运行的容器
docker ps # 列出正在运行的容器
docker ps -a # 列出当前正在运行的容器,并且显示历史运行过的容器
docker ps -a -n=1 # 显示最近创建的容器,只显示一个,n后面的数字可以改
docker ps -a -q # 只显示容器的ID
删除容器
docker rm 容器ID # 删除指定的容器,但是不能删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
启动和停止容器
docker start 容器ID  # 启动
docker stop 容器ID  # 停止
docker restart 容器ID  # 重启
docker kill 容器ID # 强制停止

其他命令

后台启动容器
# docker run -d 镜像名
docker run -d centos
查看日志
# 运行时执行一段shell脚本
[root@salve-1 ~]# docker run -d centos /bin/sh -c "while true;do echo vivi;sleep 1;done"
edfca93dfb294625d69e4dfcf59bf6e69bfa18b89376d185560ebc8bca145e47

# 查看进程信息
[root@salve-1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
edfca93dfb29   centos    "/bin/sh -c 'while t…"   17 seconds ago   Up 15 seconds             gallant_babbage

# 查看10条日志
[root@salve-1 ~]# docker logs -tf --tail 10 edfca93dfb29
2021-07-25T04:54:14.358938636Z vivi
2021-07-25T04:54:15.361539542Z vivi
查看容器中的进程信息
[root@salve-1 ~]# docker top edfca93dfb29
UID  PID  PPID C STIME TTY TIME      CMD
root 1646 1628 0 12:52 ?   00:00:00  /bin/sh -c while true;do echo vivi;sleep 1;done
root 2073 1646 0 12:58 ?   00:00:00  /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器的元数据
[root@salve-1 ~]# docker inspect edfca93dfb29
[
    {
   
        "Id": "edfca93dfb294625d69e4dfcf59bf6e69bfa18b89376d185560ebc8bca145e47",
        "Created": "2021-07-25T04:52:15.953124535Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo vivi;sleep 1;done"
        ],
        "State": {
   
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值