温馨提示:这篇文章已超过772天没有更新,请注意相关的内容是否还可用!
一、Docker创建容器并启动
docker run等于创建加启动,如果镜像不存在本地,则会去线上仓库下载镜像
注意:
容器内的进程必须处于前台运行的状态,否则容器就会直接退出!
如果容器内什么事也没做,容器也会挂掉,容器内必须有一个进程在前台运行!
docker run -选项
-d 后台运行容器并返回容器的ID
-i 以交互方式访问容器,一般与 -t 搭配使用
-t 为容器重新分配一个伪输入终端,通常与 -i 搭配使用
-p 指定端口映射 宿主机端口:容器端口
-P 随机端口映射,大写P
#运行一个马上挂掉的容器 [root@localhost ~]# docker run ubuntu [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8a8243623c23 ubuntu "bash" 6 minutes ago Exited (0) 6 minutes ago musing_jang a31847debebf 51086ed63d8c "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp trusting_ride 7f448c222d1b 51086ed63d8c "/docker-entrypoint.…" 17 minutes ago Exited (127) 17 minutes ago laughing_mcnulty 310d35737694 51086ed63d8c "/docker-entrypoint.…" 18 minutes ago Exited (0) 17 minutes ago gallant_cohen 2bb4b292933f nginx "/docker-entrypoint.…" 19 minutes ago Exited (127) 19 minutes ago funny_mendeleev 6cd00c39af29 nginx "/docker-entrypoint.…" 19 minutes ago Exited (127) 19 minutes ago vibrant_darwin d9c990a6b235 nginx "/docker-entrypoint.…" 19 minutes ago Exited (127) 19 minutes ago clever_merkle 7eee30a9c59e nginx "/docker-entrypoint.…" 20 minutes ago Exited (0) 20 minutes ago quirky_cartwright f66ffe5ce253 nginx "/docker-entrypoint.…" 21 minutes ago Exited (0) 21 minutes ago nginx
#运行一个容器并进入容器空间,执行一个命令 [root@localhost ~]# docker run -it ubuntu bash root@77712674b6f7:/# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.04 DISTRIB_CODENAME=jammy DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS" root@77712674b6f7:/#
#docker ps -a 列出当前所有正在运行的容器+历史上运行过的容器 -l 显示最近创建的容器 -n 显示最近N个创建的容器 -q 只显示容器ID
二、退出容器
exit:run进去容器,exit退出,容器停止
[root@localhost ~]# docker run -it ubuntu root@cd8ed364afb2:/# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS" [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd8ed364afb2 ubuntu "bash" 41 seconds ago Up 39 seconds intelligent_snyder #退出容器 root@cd8ed364afb2:/# exit exit [root@localhost ~]# [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
run进去容器,ctrl+p+q退出,容器不停止
[root@localhost ~]# docker run -it ubuntu root@17b0e93920f4:/# ls /etc/lsb-release /etc/lsb-release root@17b0e93920f4:/# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS" root@17b0e93920f4:/# ls /etc/lsb-release [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 17b0e93920f4 ubuntu "bash" 54 seconds ago Up 53 seconds compassionate_pike
三、容器管理
启动已经停止运行的容器
#docker start 容器ID/容器名字
重启容器
#docker restart 容器ID/容器名字
停止容器
#docker stop 容器ID/容器名字
强制停止容器
#docker kill 容器ID/容器名字
删除单个已停止容器实例
#docker rm 容器ID
删除多个容器实例(慎用,生产环境建议不使用此语句)
#docker rm -f $(docker ps -aq) #docker ps -aq | xargs docker rm
查看容器日志
#docker logs 容器ID
查看容器内部细节
#docker inspect 容器ID
复制容器的文件到宿主机
#docker cp 容器ID:容器内路径 目的主机路径 [root@localhost ~]# docker exec -it ubuntu1 bash root@b48ae4cc3481:/# cd /tmp/ root@b48ae4cc3481:/tmp# ls docker.test root@b48ae4cc3481:/tmp# echo test >docker.test root@b48ae4cc3481:/tmp# exit exit [root@localhost ~]# docker cp b48ae4cc3481:/tmp/docker.test /tmp [root@localhost ~]# cat /tmp/docker.test test
备份整个容器
#docker export 容器ID > 文件名.tar [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b48ae4cc3481 ubuntu "bash" About an hour ago Up 6 minutes ubuntu1 [root@localhost ~]# docker export b48ae4cc3481 > /tmp/ubuntu1.tar [root@localhost ~]# ll /tmp/ 总用量 73404 -rw-r--r--. 1 root root 5 10月 19 21:54 docker.test -rw-r--r--. 1 root root 75159552 10月 19 21:57 ubuntu1.tar [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 9 months ago 141MB ubuntu latest ba6acccedd29 12 months ago 72.8MB centos latest 5d0da3dc9764 13 months ago 231MB #强制删除镜像 [root@localhost ~]# docker rmi -f ba6acccedd29 Untagged: ubuntu:latest Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB
#cat 文件名.tar | docker import - 镜像用户名/镜像名:镜像版本号 [root@localhost ~]# cat /tmp/ubuntu1.tar | docker import - ldx/ubuntu1:333 sha256:5d7ff25c881d8dff4b1e82239a8c0f26a75084d4042f7020ccea2187a979430a [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ldx/ubuntu1 333 5d7ff25c881d 13 seconds ago 72.8MB nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB [root@localhost ~]# docker run -it 5d7ff25c881d bash root@0bb7ab833453:/# ll /tmp/ total 4 drwxrwxrwt. 2 root root 25 Oct 19 13:51 ./ drwxr-xr-x. 1 root root 18 Oct 19 14:06 ../ -rw-r--r--. 1 root root 5 Oct 19 13:54 docker.test root@0bb7ab833453:/# cat /tmp/docker.test test
#docker image save 镜像名:标签 > 文件名.tgz #如果使用的是镜像id,加载时候则会导致虚悬镜像 [root@localhost ~]# docker image save 5d7ff25c881d > /tmp/ubuntu2.tgz [root@localhost ~]# ls /tmp/ docker.test ubuntu1.tar ubuntu2.tgz
#docker image load -i 文件名.tgz #危险命令,不建议使用 [root@localhost ~]# docker rm $(docker ps -qa) 2c7204204c05 74481dfbbe48 0bb7ab833453 39174b8eee2b afe4325f0fd2 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ldx/ubuntu1 333 5d7ff25c881d 9 minutes ago 72.8MB nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB [root@localhost ~]# docker rmi 5d7ff25c881d Untagged: ldx/ubuntu1:333 Deleted: sha256:5d7ff25c881d8dff4b1e82239a8c0f26a75084d4042f7020ccea2187a979430a Deleted: sha256:bff10ec1eac2d4c978d96fe3fcbe5e9eb84a122e90c863c5d8888f0d12b96416 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB #加载镜像 [root@localhost ~]# docker image load -i /tmp/ubuntu2.tgz bff10ec1eac2: Loading layer [==================================================>] 75.16MB/75.16MB Loaded image ID: sha256:5d7ff25c881d8dff4b1e82239a8c0f26a75084d4042f7020ccea2187a979430a #传说中的虚悬镜像 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 5d7ff25c881d 12 minutes ago 72.8MB #虚悬镜像 nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB
正规操作:
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 5d7ff25c881d 12 minutes ago 72.8MB nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB #备份镜像 [root@localhost ~]# docker image save nginx:latest > /tmp/nginx.tgz [root@localhost ~]# ll /tmp/ 总用量 289300 -rw-r--r--. 1 root root 5 10月 19 21:54 docker.test -rw-r--r--. 1 root root 145905152 10月 19 22:21 nginx.tgz -rw-r--r--. 1 root root 75159552 10月 19 21:57 ubuntu1.tar -rw-r--r--. 1 root root 75166720 10月 19 22:10 ubuntu2.tgz #删除原本镜像 [root@localhost ~]# docker rmi 605c77e624dd Untagged: nginx:latest Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85 Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5 Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14 Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4 Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5 Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 5d7ff25c881d 17 minutes ago 72.8MB centos latest 5d0da3dc9764 13 months ago 231MB #加载备份镜像 [root@localhost ~]# docker image load -i /tmp/nginx.tgz 2edcec3590a4: Loading layer [==================================================>] 83.86MB/83.86MB e379e8aedd4d: Loading layer [==================================================>] 62MB/62MB b8d6e692a25e: Loading layer [==================================================>] 3.072kB/3.072kB f1db227348d0: Loading layer [==================================================>] 4.096kB/4.096kB 32ce5f6a5106: Loading layer [==================================================>] 3.584kB/3.584kB d874fd2bc83b: Loading layer [==================================================>] 7.168kB/7.168kB Loaded image: nginx:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 5d7ff25c881d 18 minutes ago 72.8MB nginx latest 605c77e624dd 9 months ago 141MB centos latest 5d0da3dc9764 13 months ago 231MB [root@localhost ~]# docker run -d -p 85:80 nginx b709562cd688a4d60d57ca8c3d3515b7a96373e6c6b4eaab2c0037e4f9d82140 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b709562cd688 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:85->80/tcp, :::85->80/tcp awesome_roentgen
四、进入正在运行的容器
#docker exec -it 容器ID bash #在容器中打开新的终端,并且启动新的进程,用exit退出,不会导致容器的停止 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 17b0e93920f4 ubuntu "bash" 14 minutes ago Up 14 minutes compassionate_pike [root@localhost ~]# docker exec -it 17b0e93920f4 bash root@17b0e93920f4:/# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS" root@17b0e93920f4:/# exit exit [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 17b0e93920f4 ubuntu "bash" 15 minutes ago Up 15 minutes compassionate_pike [root@localhost ~]#
#docker attach 容器ID #直接进入容器启动命令的终端,不会启动新的进程,用exit退出,会导致容器的停止 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 17b0e93920f4 ubuntu "bash" 15 minutes ago Up 15 minutes compassionate_pike [root@localhost ~]# docker attach 17b0e93920f4 root@17b0e93920f4:/# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS" root@17b0e93920f4:/# exit exit [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]#
文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。