Docker安装与基本命令使用

Docker在CentOS上的安装

官方文档:https://docs.docker.com/engine/install/centos/

1. 卸载旧版本

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

2.下载需要的安装包

sudo yum install -y yum-utils

3. 设置yum镜像仓库

默认是国外的:

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

其他镜像源

镜像加速器 镜像加速器地址
Docker 中国官方镜像 https://registry.docker-cn.com
DaoCloud 镜像站 http://f1361db2.m.daocloud.io
Azure 中国镜像 https://dockerhub.azk8s.cn
科大镜像站 https://docker.mirrors.ustc.edu.cn
阿里云 http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
七牛云 https://reg-mirror.qiniu.com
网易云 https://hub-mirror.c.163.com
腾讯云 https://mirror.ccs.tencentyun.com

这里使用阿里镜像

sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 更新yum软件包索引

yum makecache fast

5. 开始安装docker

sudo yum install docker-ce docker-ce-cli containerd.io

注:docker-ce 表示社区版,企业版为 docker-ee,分别下载docker的核心,客户端,以及容器,这个都安装最新版本。

其他版本

yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable

指定版本安装

yum install -y docker-ce-18.09.0-3.el7 docker-ce-cli-18.09.0-3.el7 containerd.io-1.2.0-3.el7

6. 启动docker

sudo systemctl start docker

查看如下表示启动成功

[root@hadoop101 ~]# sudo systemctl start docker
[root@hadoop101 ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:41 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:44:05 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.13
  GitCommit:        9cc61520f4cd876b86e77edfeb88fbcd536d1f9d
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

7. 测试

sudo docker run hello-world

[root@hadoop101 ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
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/

[root@hadoop101 ~]#

表示运行成功。

8. docker卸载

卸载依赖

sudo yum remove docker-ce docker-ce-cli containerd.io

删除资源

sudo rm -rf /var/lib/docker

sudo rm -rf /var/lib/containerd

9. 阿里云镜像加速服务

登录阿里控制台,在所有产品与服务中找到 容器镜像服务

在镜像工具下有个镜像加速器:

image

4个配置步骤如下:

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

10.docker常用命令

帮助命令

docker version

docker info

docker -help

https://docs.docker.com/engine/reference/run/

镜像命令

docker images 查看主机上所有镜像

可选性

-a, --all             #显示所有主机上的镜像
-q, --quiet           #只显示镜像id

[root@hadoop101 ~]# docker images -aq
feb5d9fea6a5
[root@hadoop101 ~]#

docker search 搜索镜像

docker search mysql -f=STARS=5000

[root@hadoop101 ~]# docker search mysql -f=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12213     [OK]
mariadb   MariaDB Server is a high performing open sou…   4689      [OK]

docker pull 下载镜像

默认下载,不指定版本,则下载最新版本

docker pull mysql

[root@hadoop101 ~]# docker pull mysql
Using default tag: latest           #默认最新版本
latest: Pulling from library/mysql
72a69066d2fe: Pull complete         #docker分层下载
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实下载地址

上面真实下载地址和名称mysql相同,如下两个下载命令相同:

docker pull mysql

docker pull docker.io/library/mysql:latest

指定版本下载,要在官方文档中支持的版本中指定,最新支持版本如下

8.0.28-oracle, 8.0-oracle, 8-oracle, oracle
8.0.28, 8.0, 8, latest, 8.0.28-debian, 8.0-debian, 8-debian, debian
5.7.37-oracle, 5.7-oracle, 5-oracle
5.7.37, 5.7, 5, 5.7.37-debian, 5.7-debian, 5-debian

我们再下载一个5.7的版本

docker pull mysql:5.7

[root@hadoop101 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@hadoop101 ~]#

可以看到,在我们下载一个默认版本后,再下载一个指定版本,上面一些依赖是通用的,就提示已存在,这样就极大节省了空间,并且两个版本毫不影响。

此时我们在查看镜像时

[root@hadoop101 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   2 months ago   448MB
mysql         latest    3218b38490ce   2 months ago   516MB
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB
[root@hadoop101 ~]#

docker rmi 删除镜像

docker rmi -f c20987f18b13 :根据镜像id删除指定镜像

docker rmi -f $(docker images -aq):删除全部镜像

容器命令

有了镜像,才可以使用容器命令,我们先下载一个linux系统

docker pull centos

创建容器并启动

文档:https://docs.docker.com/engine/reference/commandline/run/

docker run [可选参数] image

可选参数

-- name="容器名字"

-d #后台方式运行

-it #使用交互式运行,运行并进入容器

-p #小写的p,指定容器端口,-p 8080:8080

      -p  ip:主机端口:容器端口

      -p   主机端口:容器端口

      -p   容器端口

      容器端口

-P #大写的p,随机指定容器端口

运行一个容器,并进入容器

docker run -it centos /bin/bash

[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@655b77d11f20 /]#

可以看到,我们已经进入容器,并且容器的id为:655b77d11f20

退出使用 exit,表示容器停止并退出

Ctrl+p+q:容器不停止且退出

列出当前容器

docker ps:列出当前正在运行的容器

docker ps [可选参数]

-a 列出所有容器,包括正在运行的和历史运行的容器

-n 列出最近创建的容器

-q 列出容器编号

删除容器

docker rm 7e9e7a1082f8:删除指定容器id,正在运行的无法删除,-f 可强制删除

docker rm -f $(docker ps -aq):删除所有容器

docker ps -aq|xargs docker rm -f:删除所有容器

启动和停止容器

docker start 容器id   #启动一个容器
docker restart 容器id #重启一个容器
docker stop 容器id    #停止一个容器
docker kill 容器id    #强制停止一个容器

其他常用命令

后台启动容器

docker run -d centos

注意:这里发现centos又停止了,没有启动起来。

这是docker的一个问题,docker容器使用后台运行,就必须要有一个前台进程,例如使用这个容器的一个应用,如果没有,docker容器就会自动停止。

查看日志

docker logs -ft --tail 100 4b3bc9e6e95f

[root@hadoop101 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
bb1b1f3023a2   centos    "/bin/bash"   12 seconds ago   Up 11 seconds             trusting_beaver
[root@hadoop101 ~]# docker logs -ft --tail 10 bb1b1f3023a2

这里日志为空,下面我们创建一个脚本来显示日志,重新创建一个容器,然后执行脚本

docker run -d centos /bin/sh -c "while true;do echo hello hahaha...;sleep 1;done"

[root@hadoop101 ~]# docker run -d centos /bin/sh -c "while true;do echo hello hahaha...;sleep 1;done"
62f18b85321a007b9e01c1cb237e9f4942b4d90dfae8ec22fceaa62ddb7de822
[root@hadoop101 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
62f18b85321a   centos    "/bin/sh -c 'while t…"   3 seconds ago   Up 2 seconds             elastic_bose
[root@hadoop101 ~]# docker logs -ft --tail 10 62f18b85321a
2022-03-06T15:38:08.383093375Z hello hahaha...
2022-03-06T15:38:09.388922480Z hello hahaha...
2022-03-06T15:38:10.395572128Z hello hahaha...
2022-03-06T15:38:11.401655410Z hello hahaha...
2022-03-06T15:38:12.410696441Z hello hahaha...
2022-03-06T15:38:13.413753657Z hello hahaha...
2022-03-06T15:38:14.427307889Z hello hahaha...
2022-03-06T15:38:15.432224741Z hello hahaha...

查看容器中的进程

docker top 630e00985bd2

[root@hadoop101 ~]# docker top 630e00985bd2
UID      PID     PPID     C      STIME      TTY     TIME         CMD
root    2033    2013     0      22:21      pts/0   00:00:00    /bin/bash
[root@hadoop101 ~]#

查看容器元数据信息

docker inspect 630e00985bd2

显示内容过长,不再展示。

进入正在运行的容器

方式一:

docker exec -it 630e00985bd2 /bin/bash

[root@hadoop101 ~]# docker exec -it 630e00985bd2 /bin/bash
[root@630e00985bd2 /]# ps
   PID TTY          TIME CMD
    15 pts/1    00:00:00 bash
    30 pts/1    00:00:00 ps
[root@630e00985bd2 /]# exit
exit
[root@hadoop101 ~]#

方式二:

docker attach 630e00985bd2

区别:docker exec表示开启一个新的终端,类似于命令行,可做常用操作,docker exec进入正在运行的终端,有输出会正常打印。

从容器内拷贝文件到主机

格式:docker cp 容器id:容器内文件目录 主机目录

docker cp fe721a2a3c06:/opt/test.txt /opt/

[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@fe721a2a3c06 /]# cd /opt
[root@fe721a2a3c06 opt]# touch test.txt
[root@fe721a2a3c06 opt]# (ctrl+p+q)
[root@hadoop101 ~]# docker cp fe721a2a3c06:/opt/test.txt /opt/
[root@hadoop101 ~]# cd /opt
[root@hadoop101 opt]# ll
total 4
drwx--x--x   4 root     root       28 Mar  6 13:24 containerd
drwxr-xr-x. 23 zhangbao zhangbao 4096 Nov 14 00:12 module
drwxr-xr-x   3 root     root       17 Nov 13 23:59 redis
drwxr-xr-x.  5 zhangbao zhangbao  286 Nov 14 00:00 software
-rw-r--r--   1 root     root        0 Mar  8 22:47 test.txt
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,533评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,055评论 3 414
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 175,365评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,561评论 1 307
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,346评论 6 404
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,889评论 1 321
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,978评论 3 439
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,118评论 0 286
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,637评论 1 333
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,558评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,739评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,246评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,980评论 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,362评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,619评论 1 280
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,347评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,702评论 2 370

推荐阅读更多精彩内容