阅读量:0
一、 centos 7 使用yum进行安装docker及docker的使用
1、添加docker-ce源信息
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
2、修改docker-ce源
sed -i 's@download.docker.com@mirrors.tuna.tsinghua.edu.cn/docker-ce@g' /etc/yum.repos.d/docker-ce.repo
3、更新并安装docker-ce
yum makecache fast yum install -y docker-ce #默认安装最新版本
4、安装指定版本的docker
yum list docker-ce.x86_64 --showduplicates | sort -r #查看有哪些版本
5、配置加速器(提高下载和访问速度)
mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' > { > "registry-mirrors":["https://rvq9mjyt.mirror.aliyuncs.com"] #阿里 > } > EOF systemctl daemon-reload #重新加载守护进程 systemctl start docker
6、测试
docker pull busybox #拉取镜像试试 docker images #查看镜像的指令 REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest beae173ccac6 2 years ago 1.24MB
二、使用docker镜像
1、获取镜像
docker pull ubuntu:18.04 #获取镜像 18.04: Pulling from library/ubuntu 284055322776: Pull complete Digest: sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6 Status: Downloaded newer image for ubuntu:18.04 docker.io/library/ubuntu:18.04
2、使用镜像创建一个容器
[root@centos7-node1 ~]# docker run -it ubuntu:18.04 bash #创建容器 root@cb10b3ab86e8:/# echo hello world #执行打印hello world 命令 hello world root@cb10b3ab86e8:/# docker images #查看镜像的信息
3、给镜像改名字
docker tag busybox:latest mybusybox:latest docker images #两个镜像的id是一样的,说明是同一个 REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest beae173ccac6 2 years ago 1.24MB mybusybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB
4、查看镜像的详细信息及镜像的历史
docker inspect busybox docker history busybox:latest
5、搜索官方提供的nginx关键字的镜像
docker search --filter=is-official=true nginx #official显示ok表示为官方 NAME DESCRIPTION STARS OFFICIAL nginx Official build of Nginx. 19668 [OK] unit Official build of NGINX Unit: Universal Web … 24 [OK]
6、删除镜像
docker images #查看有哪些镜像 REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest beae173ccac6 2 years ago 1.24MB mybusybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB docker rmi mybusybox:latest #删除镜像(也可以使用id来删除) docker images #再次查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB
7、正在运行的镜像容器不能被删除(或者加上-f参数强制删除 不推荐)
docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB [root@centos7-node1 ~]# docker run busybox echo 'I am run' #运行容器 I am run [root@centos7-node1 ~]# docker rmi beae173ccac6 #使用id删除容器 Error response from daemon: conflict: unable to delete beae173ccac6 (must be forced) - image is being used by stopped container f5bb159b992b #容器正在运行删除不了 docker ps -a #列出当前系统中所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5bb159b992b busybox "echo 'I am run'" 4 minutes ago Exited (0) 4 minutes ago sweet_sammet cb10b3ab86e8 ubuntu:18.04 "bash" 28 minutes ago Exited (0) 26 minutes ago vigorous_bardeen #正确删除步骤,先删除容器,再删除镜像 [root@centos7-node1 ~]# docker rm f5bb159b992b f5bb159b992b [root@centos7-node1 ~]# docker rmi beae173ccac6 Untagged: busybox:latest Untagged: busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678 Deleted: sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a Deleted: sha256:01fd6df81c8ec7dd24bbbd72342671f41813f992999a3471b9d9cbc44ad88374
8、基于已有容器创建
启动一个镜像,并在其中进行修改操作。如:创建一个test文件,之后退出,代码如下: [root@centos7-node1 ~]# docker run -it ubuntu:18.04 bash root@d6e2e1fc9c71:/# touch test root@d6e2e1fc9c71:/# exit exit [root@centos7-node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d6e2e1fc9c71 ubuntu:18.04 "bash" 33 seconds ago Exited (0) 16 seconds ago peaceful_sanderson cb10b3ab86e8 ubuntu:18.04 "bash" 2 hours ago Exited (0) 2 hours ago vigorous_bardeen [root@centos7-node1 ~]# docker commit -m 'add a new file' -a 'kongd <kongd@123.com>' d6e2e1fc9c71 sha256:feb22f3bf11c382fb6d7a2b2d597e40dfd4411fc17ecfda25afebe68b65d4e04 [root@centos7-node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> feb22f3bf11c 25 seconds ago 63.1MB busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB
9、基于本地模版导入
wget -c http://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz #使用wget命令从指定的URL下载centos-7-x86_64-minimal.tar.gz文件。 docker images #列出当前系统中所有的Docker镜像,包括已经创建的和未命名的镜像。 REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> feb22f3bf11c 20 minutes ago 63.1MB busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB cat centos-7-x86_64-minimal.tar.gz | docker import - centos7:latest #将下载的centos-7-x86_64-minimal.tar.gz文件导入为一个Docker镜像,并命名为centos7:latest。 sha256:f22433edd7e345dee4db09f2a3af3611689d2df1baf9df053c2df76fb7433494 docker images #再次列出当前系统中所有的Docker镜像,查看新导入的centos7:latest镜像是否成功创建 REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest f22433edd7e3 5 seconds ago 435MB <none> <none> feb22f3bf11c 21 minutes ago 63.1MB busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB docker run -it --rm centos7:latest bash #以交互式模式在新创建的centos7:latest镜像上运行一个bash shell。 [root@ca13d1c5eeb9 /]# ls bin boot dev etc fastboot home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@ca13d1c5eeb9 /]# exit exit docker ps -a #列出所有容器的信息,包括已经停止的容器。 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d6e2e1fc9c71 ubuntu:18.04 "bash" 25 minutes ago Exited (0) 25 minutes ago peaceful_sanderson cb10b3ab86e8 ubuntu:18.04 "bash" 2 hours ago Exited (0) 2 hours ago vigorous_bardeen [root@centos7-node1 ~]# docker rm -f `docker ps -aq` #强制删除所有已经停止的Docker容器。这里使用了反引号来获取docker ps -aq的输出,并将其作为docker rm -f的参数来删除这些容器。 d6e2e1fc9c71 cb10b3ab86e8 [root@centos7-node1 ~]# docker images #最后再次列出当前系统中所有的Docker镜像,查看镜像和容器的最新状态。 REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest f22433edd7e3 4 minutes ago 435MB <none> <none> feb22f3bf11c 25 minutes ago 63.1MB busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB
10、存出和载入
docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest f22433edd7e3 24 minutes ago 435MB <none> <none> feb22f3bf11c 45 minutes ago 63.1MB busybox latest beae173ccac6 2 years ago 1.24MB ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB docker save -o centos7.tar centos7:latest #将名为centos7:latest的Docker镜像保存为一个tar文件centos7.tar ls anaconda-ks.cfg centos7.tar centos-7-x86_64-minimal.tar.gz scp centos7.tar 192.168.80.172:~ #将文件传输到192.168.80.172主机上面 [root@centos7-node2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@centos7-node2 ~]# ls anaconda-ks.cfg centos7.tar [root@centos7-node2 ~]# docker load -i centos7.tar #加载一个Docker镜像文件centos7.tar到Docker中 788edba9eaa8: Loading layer [==================================================>] 446.1MB/446.1MB Loaded image: centos7:latest [root@centos7-node2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest f22433edd7e3 29 minutes ago 435MB
11、上传镜像
#登陆阿里云docker registry docker login --username=用户名 registry-cn-hangzhou.aliyuncs.com #从registry中拉取镜像 docker pull registry-cn-hangzhou.aliyun.com/用户名/镜像版本 #将镜像推送到registry dokcer login --usernmae=用户名 registry.cn-hangzhou.aliyuncs.com docker tag [Imageld] registry.cn-hangzhou.aliyuncs.com/用户名/镜像版本 docker push registry.cn-hangzhou.aliyun.com/用户名/镜像版本