Docker安装与基本命令使用

网友投稿 237 2022-10-22

Docker安装与基本命令使用

Docker在CentOS上的安装

官方文档:卸载旧版本

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 \ 更新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 -rdocker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stabledocker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stabledocker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stabledocker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stabledocker-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. 阿里云镜像加速服务

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

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

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

镜像命令

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

创建容器并启动

文档: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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Docker 文件系统-AUFS 原理解析(31)
下一篇:人工智能的安全API如何使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~