01docker的安装与基本使用

01docker的安装与基本使用

2023年7月6日发(作者:)

01docker的安装与基本使⽤docker 安装1、如果之前安装过docker,需要⾃⾏卸载#1.卸载依赖yum remove docker-ce docker-ce-cli #2.删除⽬录rm -rf /var/lib/docker #docker默认的⼯作路径#3.镜像加速器(docker优化) - 登录阿⾥云找到容器镜像服务 - 找到镜像加速地址 - 配置使⽤#4.卸载旧的版本 sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine2、初始化系统环境sudo yum install -y yum-utils device-mapper-persistent-data lvm23、安装yum源wget -O /etc/.d/ /docker-ce/linux/centos/m clean allyum makecache4、安装dockeryum install docker-ce -y5、docker优化打开阿⾥云官⽹ 产品 --> 容器与中间件 --> 容器与镜像服务ACR --> 管理控制台 --> 镜像加速器 --> CentOSsudo mkdir -p /etc/dockersudo tee /etc/docker/ <<-'EOF'{ "registry-mirrors": [""]}EOFsystemctl daemon-reloadsystemctl restart docker6、设置开机⾃启动systemctl enable --now docker7、检查docker# 第⼀种⽅式docker run -d -P nginx# 第⼆种⽅式docker infodocker 的基本使⽤docker 中的三⼤基本概念镜像镜像就是启动⼀个容器的模板。--->容器容器就是对外提供服务的进程。或者容器就是镜像启动起来的⼀个实例。---->QQ仓库仓库是⽤来存放镜像的地⽅。docker 镜像相关命令常⽤镜像仓库官⽅仓库:⾃⼰的私有仓库:Harbor阿⾥云私有仓库:搜索镜像#格式 docker search [镜像名称]# 实例拉取镜像# 格式 docker pull [镜像名称]# 实例[root@Centos7 ~]# docker pull redisUsing default tag: latestlatest: Pulling from library/redis# 层a076a628af6f: Already exists

f40dd07fe7be: Pull complete

ce21c8a3dbee: Pull complete

ee99c35818f8: Pull complete

56b9a72e68ff: Pull complete

3f703e7f380f: Pull complete

# 镜像ID号(镜像ID号是全球唯⼀)Digest: sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494# 镜像下载状态Status: Downloaded newer image for redis:latest

# 镜像的全称(镜像的tag)/library/redis:latest查看当前系统上的有哪些镜像# 格式 docker images 或者 docker image ls# 参数-q : 只显⽰镜像ID[root@Centos7 ~]# docker images -q621ceef7494af6d0b4767a6c查找筛选镜像# 收藏量⼤于500的[root@docker ~]# docker search -f stars=500 mysqlINDEX NAME DESCRIPTION STARS OFFICIAL /mysql MySQL is a widely used, 11028 [OK]

/mariadb MariaDB Server is a high performing 4178 [OK]

/mysql/mysql-server Optimized MySQL Server Docker images. 820 [OK]# 是官⽅的[root@docker ~]# docker search mysql -f is-official=trueINDEX NAME DESCRIPTION STARS OFFICIAL /mysql MySQL is a widely used, 11028 [OK]

/mariadb MariaDB Server is a high performing 4178 [OK]

获取镜像的详细信息# 格式 docker inspect [镜像名称或镜像ID]# 参数-f : 格式化输出[root@Centos7 ~]# docker inspect -f '{{.Id}}' 621ceef7494asha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb[root@Centos7 ~]# docker inspect -f '{{.me}}' redis16535cfaf84a登录镜像仓库# 格式 docker login

注: 默认情况下,docker login登录的是官⽅仓库,如果登录其他镜像仓库则需要指定镜像仓库的URL连接。

# 实例 [root@Centos7 ~]# docker login Username: yangyang091022 Password:

WARNING! Your password will be stored unencrypted in /root/.docker/. Configure a credential helper to remove this warning. See /engine/reference/commandline/login/#credentials-store Login Succeeded [root@Centos7 ~]# cat ~/.docker/

{ "auths": { "": { "auth": "eWFuZ3lhbmcwOTEwMjI6Y2hlbjE4NzkwMDcwODMw" } } }# 参数--username|-u : 指定⽤户名--password|-p : 指定密码为镜像标签# 镜像标签的构成/library/redis: : 镜像仓库的URLlibrary :镜像仓库命名空间redis : 镜像名称latest : 镜像版本号# 打标签 # 格式 docker tag [镜像ID] 镜像标签[root@Centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEredis latest 621ceef7494a 2 months ago 104MBnginx latest f6d0b4767a6c 2 months ago 133MB[root@Centos7 ~]# docker tag 621ceef7494a /alvinos/redis:v2[root@Centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEredis latest 621ceef7494a 2 months ago /alvinos/redis v2 621ceef7494a 2 months ago 104MBnginx latest f6d0b4767a6c 2 months ago 133MB镜像上传# 格式 docker push [镜像标签]# 注:要想上传镜像,⾸先得登录镜像仓库,其次设置对应镜像仓库的tag[root@Centos7 ~]# docker push /alvinos/redis:v2The push refers to repository [/alvinos/redis]3480f9cdd491: Pushed

a24a292d0184: Pushed

f927192cc30c: Pushed

1450b8f0019c: Pushed

8e14cb7841fa: Pushed

cb42413394c4: Pushed

v2: digest: sha256:7ef832c720188ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892 size: 1572镜像的删除# 格式 docker rmi [镜像名称或者镜像ID]# 实例 [root@Centos7 ~]# docker rmi nginx# 参数 -f : 强制删除 [root@Centos7 ~]# docker rmi -f nginx Untagged: nginx:latest Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa # 注:当有容器正在使⽤镜像时,强制删除镜像,只能删除镜像的所有tag, 不会删除镜像。清空镜像# 格式 docker image prune# 实例 [root@Centos7 ~]# docker image prune WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N] y Total reclaimed space: 0B # 参数 -a : 删除所有镜像

[root@Centos7 ~]# docker image prune -aWARNING! This will remove all images without at least one container associated to you sure you want to continue? [y/N] yDeleted Images:untagged: redis:latestuntagged: redis@sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494untagged: /alvinos/redis:v2untagged: /alvinos/redis@sha256:7ef832c720188ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892deleted: sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebbdeleted: sha256:de66cfbf4712b8ba9ef292e08ef7487be26d9d21b350548e400ae351405d820edeleted: sha256:79b2381e35429e8fc04d31b3445f069c22d288bf5c4cba7b7c10004ff78ae201deleted: sha256:1d047d19be363b00139990d4d7f392dabdb0809dbc9d0fbe67c1f15b8caed27adeleted: sha256:8c41f4e708c37059df28ae1cabc200a6db2fee45bd3a2cadcf70f2765bb68730deleted: sha256:b51317bef36fe1900be48402c8a41fcd9cdb6b8950c10209f764473cb8323371Total reclaimed space: 35.04MB[root@Centos7 ~]#

查看镜像历史(镜像的构建历史)# 格式 docker history [镜像ID或镜像名称]# 实例[root@Centos7 ~]# docker history alpineIMAGE CREATED CREATED BY SIZE COMMENT7731472c3f2a 2 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B

2 months ago /bin/sh -c #(nop) ADD file:edbe213ae0c825a5b… 5.61MB

保存镜像(commit)# 保存正在运⾏的容器直接为镜像# 格式: docker commit [容器ID|容器名称]

# 实例[root@Centos7 ~]# docker commit -a "baim0" -m "这是⼀个docker镜像" -p be3b92e2886b test:v1sha256:4a06cd2af42877b5e2908073061f7ae1bf9e308a470bdfc0c6f906ef368aaed8[root@Centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest v1 4a06cd2af428 5 seconds ago 104MB保存镜像(import/export)# 保存正在运⾏的容器为镜像压缩包## 保存容器为镜像 docker export [容器的ID] > [包名称] # 实例 [root@Centos7 ~]# docker export be3b92e2886b > [root@Centos7 ~]# ll | grep redis -rw-r--r--. 1 root root 104178688 Mar 18 17:30

## docker import [包名称] [⾃定义镜像名称] # 实例 [root@Centos7 ~]# docker import test:v3 sha256:7776db3402fb8d59f6121a3b1977b5e7016f4064cf59218fd1b06637cb0fca87 [root@Centos7 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test v3 7776db3402fb 6 seconds ago 101MB保存镜像(save/load)# 保存镜像为压缩包# 保存镜像的格式: docker save [镜像名称|镜像ID] > [包名称] [root@Centos7 ~]# docker save 7731472c3f2a > [root@Centos7 ~]# ll

-rw-r--r--. 1 root root 5888000 Mar 18 17:36 [root@Centos7 ~]# docker save -o 7731472c3f2a [root@Centos7 ~]# ll total 148692 -rw-r--r--. 1 root root 5888000 Mar 18 17:36 -rw-------. 1 root root 5888000 Mar 18 17:37 # 导⼊镜像的格式: docker load < [包名称]

[root@Centos7 ~]# docker load <

c04d1437198b: Loading layer [========================================>] 5.88MB/5.88MB Loaded image ID: sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839 [root@Centos7 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 7731472c3f2a 2 months ago 5.61MB# 注:save/load保存镜像⽆法⾃定义镜像名称,save保存镜像时如果使⽤ID保存则load导⼊镜像⽆名称,使⽤名称导⼊时才有名称。[root@Centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEbusybox latest b97242f89c8a 2 months ago 1.23MB[root@Centos7 ~]# docker save busybox:latest > [root@Centos7 ~]# lltotal 150120-rw-r--r--. 1 root root 1459200 Mar 18 17:43 [root@Centos7 ~]# docker rmi b97242f89c8aUntagged: busybox:latestUntagged: busybox@sha256:c5439d7db88ab5423999530349d327b04279ad3161d7596d2126dfb5b02bfd1fDeleted: sha256:b97242f89c8a29d13aea12843a08441a4bbfc33528f55b60366c1d8f6923d0d4Deleted: sha256:0064d0478d0060343cb2888ff3e91e718f0bffe9994162e8a4b310adb2a5ff74[root@Centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE[root@Centos7 ~]# docker load <

0064d0478d00: Loading layer [==================================================>] 1.45MB/1.45MBLoaded image: busybox:latest[root@Centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEbusybox latest b97242f89c8a 2 months ago 1.23MB保存镜像三种⽅式的区别1、export保存的镜像体积要⼩于save(save保存更完全,export保存会丢掉⼀些不必要的数据)2、export可以重命名镜像名称⽽save则不⾏3、save可以同时保存多个镜像⽽export则不⾏

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688592513a153134.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信