Docker学习笔记
2022/4/13 3:12:36
本文主要是介绍Docker学习笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Docker概述
Docker学习链接
官网链接:Home - Docker
Docker与虚拟机比较
虚拟化技术
容器技术不是模拟的一个完整的操作系统。
比较Docker 和虚拟机技术不同
- 传统虚拟机,虚拟出一台硬件,运行完整的操作系统。
- 容器内的应用直接运行在宿主机的内容,容器是没有自己的内核的,也没有虚拟我们的听见
- 每个容器间相互隔离,每个容器内都有一个属于自己的文件系统,互不影响。
使用Docker的好处
1、应用更快的交付和部署。
2、更便捷的升级和扩缩容
3、更简单的系统运维
4、更高效的计算资源利用。
Docker安装
Docker的基本名词
镜像(image):
一个模板,可以通过这个模板来创建容器服务。
容器(container):
独立运行一个或者一组应用,通过镜像来创建的。
启动、停止、删除
仓库(repository):
存放镜像的地方
仓库分为:公有、私有
安装Docker步骤
安装环境检查
# 系统内核是 3.10以上的 [root@localhost ~]# uname -r 3.10.0-1160.el7.x86_64
# 系统版本 [root@localhost ~]# uname -r 3.10.0-1160.el7.x86_64 [root@localhost ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"
Docker安装
帮助文档:Docker Documentation
# 前期准备: 1、修改主机名 # hostnamectl set-hostname docker # bash # 2、关闭防火墙 # systemctl stop firewalld && systemctl disable firewalld # systemctl status firewalld # 3、关闭selinux # setenforce 0 # getenforce # sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config cat /etc/selinux/config # 4、配置网卡联网 # cat /etc/sysconfig/network-scripts/ifcfg-ens32 # 5、配置阿里云镜像 # cd /etc/yum.repos.d/ && mkdir bak && mv CentOS-* bak/ # curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo # sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo # 6、生成本地缓存 # yum makecache fast # 7、更新YUM源软件包 # yum update -y
# yum安装gcc相关环境(需要确保虚拟机可以上外网。) yum install -y gcc && yum install -y gcc-c++
# 本实验基于CentOS 7.9系统进行演示安装。 # 1、卸载旧版本docker yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine # 2、安装需要的安装包 # yum install -y yum-utils # 3、设置镜像 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo # 默认镜像源是国外的,不推荐使用 yum-config-manager \ --add-repo \ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 推荐使用国内镜像 # 更新YUM源软件包索引 yum makecache fast # 4、安装docker docker-ce ee企业版 yum install docker-ce docker-ce-cli containerd.io # 5、启动Docker systemctl start docker && systemctl enable docker systemctl status docker # 6、查看docker版本信息 docker version # 7、测试,运行hello-world docker run hello-world # 8、查看hello-world镜像 # docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 5 months ago 13.3kB
安装完成。
查看docker版本信息
运行hello-world结果
卸载Docker
# 卸载docker 1、卸载依赖 yum remove docker-ce docker-ce-cli containerd.io 2、删除资源 rm -rf /var/lib/docker # docker的默认工作路径 rm -rf /var/lib/containerd
配置阿里云镜像加速
1、登录阿里云平台
2、选择"容器镜像服务"
容器镜像服务 (aliyun.com)
3、配置使用
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://w2kavmmf.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
回顾hello-world运行流程
Docker底层工作原理
docker是一个C/S结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问。
Docker Server接收 Docker-Client的指令。就会执行这个命令。
Docker为什么比VM块
1、Docker有着比虚拟机更少的抽象层
2、docker利用的是宿主机的内核,vn需要时Guest OS
所以说,创建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导。
虚拟机是加载Guest OS。分钟级别的,而Docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级。
Docker常用命令
帮助命令
docker version # 显示docker版本信息 docker info # 显示docker系统信息 docker 命令 --help # 帮助命令
镜像命令
docker images 查本地主机上的所有镜像
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 5 months ago 13.3kB # 解释: REPOSITORY: 镜像的仓库源 TAG: 镜像的标签 IMAGE ID : 镜像的ID CREATED: 镜像的创建时间 SIZE: 镜像的大小 [root@docker ~]# docker images --help Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images # 可选项 Options: -a, --all # Show all images -q, --quiet # Only show image IDs
docker search 搜索镜像
[root@docker ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12283 [OK] mariadb MariaDB Server is a high performing open sou… 4723 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 911 [OK] [root@docker ~]# docker search --help Usage: docker search [OPTIONS] TERM Search the Docker Hub for images Options: -f, --filter filter Filter output based on conditions provided --format string Pretty-print search using a Go template --limit int Max number of search results (default 25) --no-trunc Don't truncate output # 可选项,通过收藏来过滤 [root@docker ~]# docker search mysql --filter=STARS=5000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12283 [OK] [root@docker ~]#
docker pull 下载镜像
[root@docker ~]# docker pull --help Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST] Pull an image or a repository from a registry Options: -a, --all-tags Download all tagged images in the repository --disable-content-trust Skip image verification (default true) --platform string Set platform if server is multi-platform capable -q, --quiet Suppress verbose output
# 下载镜像 docker pull 镜像名[:tag] [root@docker ~]# docker pull mysql Using default tag: latest #如果不写 tag,默认是latest latest: Pulling from library/mysql 72a69066d2fe: Pull complete # 分层下载,docker image的核心 联合文件系统 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 #这两条命令等价 docker pull mysql docker pull docker.io/library/mysql:latest # 指定版本下载 [root@docker ~]# 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@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 c20987f18b13 3 months ago 448MB mysql latest 3218b38490ce 3 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB
docker rmi 删除镜像
[root@docker ~]# docker rmi --help Usage: docker rmi [OPTIONS] IMAGE [IMAGE...] Remove one or more images Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents
# 查询镜像,使用IMAGE ID删除 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 c20987f18b13 3 months ago 448MB mysql latest 3218b38490ce 3 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB [root@docker ~]# docker rmi c20987f18b13 Untagged: mysql:5.7 Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94 Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76 Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989 Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150 Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 3218b38490ce 3 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB [root@docker ~]# # 删除多个容器 docker rmi 容器id 容器id 容器id # 删除所有镜像 [root@docker ~]# docker rmi -f $(docker images -aq) Untagged: mysql:latest Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5 Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9 Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3 Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7 Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838 Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0 Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2 Untagged: hello-world:latest Untagged: hello-world@sha256:6d60b42fdd5a0aa8a718b5f2eab139868bb4fa9a03c9fe1a59ed4946317c4318 Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
容器命令
说明: 有了镜像才可以创建容器,Linux,下载一个centos镜像来测试学习。
docker pull centos [root@docker ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest
新建容器并启动
docker run [可选参数] image # 参数说明 --name="Name" # 容器名字,用来区分容器 -d # 后台启动方式 -it # 使用交互方式运行,进入容器查看内容 -p # 指定容器的端口 -p 8080:8080 -p ip:主机端口:容器端口 -p 主机端口:容器端口(常用) -p 容器端口 容器端口 -P # 随机指定端口 # 测试 # 启动并进入容器 docker -it centos /bin/bash [root@docker ~]# docker run -it centos /bin/bash [root@c7dfc970a91b /]# ls #查看容器centos,基础版本,很多命令不完善 bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@c7dfc970a91b /]# exit #从容器中退回主机 exit
列出所有运行的容器
# docker ps 命令 # 列出当前正在运行的容器 -a # 列出当前正在运行的容器,带出历史运行过的容器 -n=?# 显示最近创建的容器 -q # 只显示容器的编号 [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7dfc970a91b centos "/bin/bash" About a minute ago Exited (0) 46 seconds ago frosty_swirles 3f4ea26f2030 feb5d9fea6a5 "/hello" 6 hours ago Exited (0) 6 hours ago flamboyant_zhukovsky # 显示所有镜像编号 [root@docker ~]# docker ps -aq c7dfc970a91b 3f4ea26f2030
退出容器
exit #直接容器停止并退出 Ctrl + P + Q #容器不停止退出 [root@docker ~]# docker run centos /bin/bash [root@docker ~]# docker run -it centos /bin/bash [root@e9e247efe0ac /]# [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e9e247efe0ac centos "/bin/bash" 28 seconds ago Up 27 seconds priceless_merkle [root@docker ~]#
删除容器
docker rm 容器id # 删除指定容器(不能删除正在运行的容器) docker rm —f $(docker ps -aq) # 删除所有容器 docker ps -a -q | xargs docker rm # 删除所有的容器 # 删除正在运行的容器会报错 [root@docker ~]# docker rm e9e247efe0ac Error response from daemon: You cannot remove a running container e9e247efe0ac9f13ebabf0fc8c10f1d770d8c08617ad870e5d16d20a763fa62f. Stop the container before attempting removal or force remove [root@docker ~]# docker ps -aq e9e247efe0ac c7dfc970a91b 3f4ea26f2030 [root@docker ~]# docker rm -f $(docker ps -aq) e9e247efe0ac c7dfc970a91b 3f4ea26f2030 [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker ~]# docker ps -aq [root@docker ~]#
启动和停止容器的操作
docker start 容器id docker restart 容器id docker stop 容器id docker kill 容器id [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 313175cf8109 centos "/bin/bash" 44 seconds ago Exited (0) 6 seconds ago cool_kirch [root@docker ~]# docker start 313175cf8109 313175cf8109 [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 313175cf8109 centos "/bin/bash" 56 seconds ago Up 5 seconds cool_kirch [root@docker ~]# docker stop 313175cf8109 313175cf8109 [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker ~]#
常用其他命令
1、后台启动容器
# 命令docker run -d 镜像名 [root@docker ~]# docker run -d centos # 问题docker ps。发现CentOS停止了 # 常见的坑:docker 容器使用是后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止 # nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
2、查看日志
docker logs -f -t --tail 容器,没有日志 # 自己编写一段shell脚本 [root@docker ~]# docker run -d centos /bin/bash -c "while true;do echo xybdiy;sleep 1;done" 63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63334f8d0ac3 centos "/bin/bash -c 'while…" 24 seconds ago Up 24 seconds ecstatic_albattani # 显示日志 -tf # 显示日志 --tail number #要显示日志条数 [root@docker ~]# docker logs -tf --tail 10 63334f8d0ac3 2022-03-21T13:45:24.983694039Z xybdiy 2022-03-21T13:45:25.986523455Z xybdiy 2022-03-21T13:45:26.990955021Z xybdiy 2022-03-21T13:45:27.993041663Z xybdiy 2022-03-21T13:45:28.995840669Z xybdiy 2022-03-21T13:45:30.000526171Z xybdiy 2022-03-21T13:45:31.006629083Z xybdiy 2022-03-21T13:45:32.015123258Z xybdiy 2022-03-21T13:45:33.018435524Z xybdiy 2022-03-21T13:45:34.021714677Z xybdiy [root@docker ~]#
3、查看容器进程命令信息ps
#命令 docker top 容器id [root@docker ~]# docker top 63334f8d0ac3 UID PID PPID C STIME TTY TIME CMD root 12114 12095 0 21:44 ? 00:00:00 /bin/bash -c while true;do echo xybdiy;sleep 1;done root 12432 12114 0 21:48 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1 [root@docker ~]#
4、查看镜像的元数据
# 帮助命令 [root@docker ~]# docker inspect --help Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...] Return low-level information on Docker objects Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes if the type is container --type string Return JSON for specified type [root@docker ~]# [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63334f8d0ac3 centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes ecstatic_albattani d735fbaa4bc1 centos "/bin/bash" 17 minutes ago Exited (0) 17 minutes ago intelligent_carver 313175cf8109 centos "/bin/bash" 28 minutes ago Exited (0) 18 minutes ago cool_kirch # 命令 docker inspect 容器id # 测试 [root@docker ~]# docker inspect 63334f8d0ac3 [ { "Id": "63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e", "Created": "2022-03-21T13:44:35.503041384Z", "Path": "/bin/bash", "Args": [ "-c", "while true;do echo xybdiy;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 12114, "ExitCode": 0, "Error": "", "StartedAt": "2022-03-21T13:44:35.767782143Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/resolv.conf", "HostnamePath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/hostname", "HostsPath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/hosts", "LogPath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e-json.log", "Name": "/ecstatic_albattani", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83-init/diff:/var/lib/docker/overlay2/ab65f0c4d3420c4a290a7d9956c96631e583ddb7102a700e94fe19b2b80893a0/diff", "MergedDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83/merged", "UpperDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83/diff", "WorkDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "63334f8d0ac3", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash", "-c", "while true;do echo xybdiy;sleep 1;done" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "88a08479f6ec55f7df18c951d743189d245ff141fb0eab1800d0ed2485dd13f3", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/88a08479f6ec", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "33d8d755edf77fc0294cfef8c351e9ff6ace5aef4fdd0923ab0be0f59aaf2485", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "48c02734d850fe7a67cefc00923e4e72d5adc1ef05cdb8aab68373e7289fe4a7", "EndpointID": "33d8d755edf77fc0294cfef8c351e9ff6ace5aef4fdd0923ab0be0f59aaf2485", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@docker ~]#
5、进入当前正在运行的容器
# 通常容器都是使用后台方式运行的,需要进入容器,需要进入容器,修改一些配置。 #命令 docker exec -it 容器id bashShell # 测试 [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63334f8d0ac3 centos "/bin/bash -c 'while…" 22 minutes ago Up 22 minutes ecstatic_albattani [root@docker ~]# docker exec -it 63334f8d0ac3 /bin/bash [root@63334f8d0ac3 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@63334f8d0ac3 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 13:44 ? 00:00:00 /bin/bash -c while true;do echo xybdiy;sleep 1;done root 1362 0 0 14:07 pts/0 00:00:00 /bin/bash root 1388 1 0 14:07 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1 root 1389 1362 0 14:07 pts/0 00:00:00 ps -ef [root@63334f8d0ac3 /]# #命令 docker attach 容器id # 测试 [root@docker ~]# docker attach --help Usage: docker attach [OPTIONS] CONTAINER Attach local standard input, output, and error streams to a running container Options: --detach-keys string Override the key sequence for detaching a container --no-stdin Do not attach STDIN --sig-proxy Proxy all received signals to the process (default true) [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63334f8d0ac3 centos "/bin/bash -c 'while…" 25 minutes ago Up 25 minutes ecstatic_albattani [root@docker ~]# docker attach 63334f8d0ac3 正在执行当前的代码 # docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用操作) # docker attach # 进入容器正在执行的终端,不启动新的进程
6、从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 6 months ago 231MB [root@docker ~]# docker run -it centos /bin/bash [root@76539a2682cc /]# [root@docker ~]# ls anaconda-ks.cfg [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76539a2682cc centos "/bin/bash" 39 seconds ago Up 39 seconds laughing_morse [root@docker ~]# docker attach 76539a2682cc [root@76539a2682cc /]# cd home/ [root@76539a2682cc home]# ls # 在容器内创建test.java镜像 [root@76539a2682cc home]# touch test.java [root@76539a2682cc home]# ls test.java [root@76539a2682cc home]# exit exit [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76539a2682cc centos "/bin/bash" About a minute ago Exited (0) 33 seconds ago laughing_morse # 将文件拷贝出来 [root@docker ~]# docker cp 76539a2682cc:/home/test.java /home/ [root@docker ~]# ll /home/ total 0 -rw-r--r--. 1 root root 0 Mar 21 22:19 test.java # 拷贝是一个手动的过程,以后使用 -v 卷的技术,可以实现,自动同步。
…
这篇关于Docker学习笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14企业协同软件:现代化管理的新选择
- 2024-11-14职场协作不再混乱:8个团队管理技巧提升你的项目效率
- 2024-11-14想让Excel表格设计更美观?试试这几款好用工具!
- 2024-11-14导航效果资料:初学者必备指南
- 2024-11-14制作右侧跟随效果资料的简单教程
- 2024-11-14职场效率倍增!学会这6个任务管理法则轻松完成工作
- 2024-11-14PS网页切图资料:新手入门教程
- 2024-11-14如何使用API查询虚拟货币的实时行情
- 2024-11-14低代码应用入门指南:轻松创建你的第一个应用
- 2024-11-14实物量法到底是什么?让项目管理更精准的秘密