暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

docker的常用操作

原创 手机用户3593 2023-02-07
1202

常见的概念:
服务发现组件:在写代码的过程中会调用某些接口,代码需要知道服务实例的网络位置,但是在云端,所有的IP资源都是变动的,如何确定IP地址呢,就使用服务发现,如zookeeper、etcd、consul
k8s使用的是服务端发现模式,每个集群上面有一个代理,


1、yum install -y yum-utils #添加yum组件
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #配置yum源,使用阿里

2、安装docker
yum install -y docker-ce 安装社区版
启动docker systemctl enable docker systemctl start docker
编辑daemon.json文件 vi /etc/docker/daemon.json
{
"graph": "/data/docker", # docker的存储目录
"storage-driver": "overlay2", # 存储驱动
"insecrue-registries": ["registry.access.redhat.com","quay.io"], #私有仓库
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"], #阿里云的加速云
"bip": "172.7.5.0/24", #docker的网络,建议使用172网络,可以有个对应关系,让容器的地址和主机有个对照关系
"exec-opts": ["native.cgroupdriver=systemd"], #启动时候的一些额外参数,cgroup是谷歌写到linux内核的关于cpu内存资源的一些控制方法
"live-restore": true #当docker引擎已经死掉当 Docker 守护进程终止时,它将关闭正在运行的容器。您可以配置守护程序,以便容器在守护程序不可用时保持运行。此功能称为live-restore。live-restore选项有助于减少由于守护进程崩溃、计划中断或升级而导致的容器停机时间
}


docker info 命令信息,查看docker内容的详细,可以查看docker安装是否正常,启动是否正常

3、运行docker第一容器
docker run hello-world
Unable to find image 'hello-world:latest' locally 无法找到helloworld镜像
latest: Pulling from library/hello-world 拉取
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest 下载完成

Hello from Docker!
This message shows that your installation appears to be working correctly.
#hello-world镜像是如何跑起来的
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon. docker客户端连接到了docker的服务端,docker服务端就是守护进程,docker是一个C/S架构
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64) #docker daemon从docker hub拉取了hello-world镜像
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.

4、docker镜像管理和docker容器的操作为docker的重点
镜像的结构 ${registry_name}/${repository_name}/${image_name}:${tag_name}
例如:docker.io/library/alpine:3.10.1
远端仓库的地址/分类仓库地址/镜像名称:镜像的标签(一般是版本号)

全球最大的docker仓库,http://hub.docker.com
docker.io为registry名称
docker login docker.io
登录后,在/root/.docker/config.json里面包含对应的文件
登录后才可以搜索镜像等等

5、搜索镜像
docker search 镜像名
docker pull 镜像名 #下载镜像

docker search alpine
docker pull alpine
docker pull alpine:3.10.3 #下载指定的tag版本文件
docker pull docker.io/library/alpine:3.10.1 #使用镜像的全称下载 registry_name/repository_name/image_name:tag_name
# 如果使用私有仓库,需要使用上面的全称下载

# 本地到服务器
docker cp 本地路径 容器id或者容器名字:容器内路径

# 服务器到本地
docker cp 容器id或者容器名字:容器内路径 本地路径

6、查看本地镜像
docker images / docker image ls
给镜像打标签
docker tag $IMAGE_ID docker.io/beyonfu/alpine:v3.10.3

#把镜像推送到仓库中
docker push docker.io/beyonfu/alpine:v3.10.3

#删除本地镜像
docker rmi docker.io/xiaoyuxin/alpine:v3.10.3 #仅仅是删除对应的标签
docker rmi -f $IMAGE_ID # 强制删除镜像,会把之前的镜像对应的tag全部untag,然后再删除镜像

7、docker镜像分层:
docker镜像位于bootfs之上,每一层镜像的下面一层称为父镜像,第一层镜像为Base Image,
容器在最顶层,其下的所有层都为readonly, Docker将readonly的FS层称为image
docker从镜像中更新的部分只有增量的部分,base image部分不会更新的,比如10G大小的镜像,下次更新就更新增量大小,启动速度非常快,基于aufs

8、容器的基本操作 --14
docker ps列出本地运行的容器进程
docker ps -a 列出所有的容器

启动容器
docker run :
optinon :
-i 启动一个可交互的容器,并且持续打开标准输入
-t 表示使用终端关联到容器的标准输入输出
-d 表示将容器放置后台运行,不走交互方式
-rm 退出后即删除容器
-name 表示定义容器唯一名称
IMAGE 表示要运行的镜像
COMMAND 表示启动容器时要运行的命令

--priviledge


docker run -ti beyonfu/alpine:v3.10.3 #交互式启动

docker run --rm beyonfu/alpine:v3.10.3 /bin/echo hello #执行输出hello后就删除这个容器,通过docker ps -a也看不到到退出的容器
docker run -d --name $容器名称 $镜像 /bin/sleep 300 #丢到后台运行这个容器,执行/bin/sleep 300 休息5分钟

docker exec -ti $CONTAINER_ID /bin/sh #进入到一个已经运行的容器中

docker stop $CONTAINER_ID #把容器停止掉
docker start $CONTAINER_ID #启动容器
docker restart $CONTAINER_ID #重启下容器

docker rm $CONTAINER_ID/$CONTAINER_NAME #删除容器,可以用容器id,或者容器名

#删除已经退出的容器
for i in `docker ps -a | grep -i exit | awk '{print $1}'`; do docker rm -f $i; done
# 执行docker ps -a| grep -i

#docker 的容器内容固化,比如运行一个容器,容器中程序运行产生的日志,需要把这个日志固化下来,产生一个新的镜像
docker commit -p myalpine beyonfu/alpine_has_1_txt
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] 生成了一个新的镜像
同样也可以使用docker export

9、导入导出镜像
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
docker save beyonfu/alpine_has_1_txt > alpine_has_1_txt.tar

导入镜像
docker load < alpine_has_1_txt.tar

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论