1.安装docker服务,配置镜像加速器
2.下载系统镜像(Ubuntu、 centos)
3.基于下载的镜像创建两个容器 (容器名一个为自己名字全拼,一个为首名字字母)
4.容器的启动、 停止及重启操作
5.怎么查看正在运行的容器和所有容器?
6.怎么退出容器: 两种方法分别实现?
7.怎么连接到运行的容器?
8.查看容器或镜像的内部信息?
9.如何查看所有镜像?
目录
1.安装docker服务,配置镜像加速器
1.安装docker
1.安装工具
2.配置软件源
3.修改软件源配置
4.下载docker
5.设置开机docker自启
2.配置加速器
2.下载系统镜像(Ubuntu、 centos)
3.基于下载的镜像创建两个容器 (容器名一个为自己名字全拼,一个为首名字字母)
4.容器的启动、 停止及重启操作
停止:
启动:
重启:
进入容器
5.怎么查看正在运行的容器和所有容器?
查看在运行的容器:
查看所有容器:
6.怎么退出容器: 两种方法分别实现?
进入容器
退出:
第一种:exit
第二种: ctrl+d / ctrl+q
7.怎么连接到运行的容器?
1.使用dockers exec
2.使用nsenter命令
8.查看容器或镜像的内部信息?
查看容器内部信息:
查看镜像的详细信息:
9.如何查看所有镜像?
1.安装docker服务,配置镜像加速器
1.安装docker
1.安装工具
[root@bogon]# yum install -y yum-utils device-mapper-persistent-data lvm2
2.配置软件源
[root@bogon yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
3.修改软件源配置
[root@bogon yum.repos.d]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
4.下载docker
yum makecache fast
yum -y install docker-ce
5.设置开机docker自启
[root@bogon yum.repos.d]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
2.配置加速器
2.下载系统镜像(Ubuntu、 centos)
3.基于下载的镜像创建两个容器 (容器名一个为自己名字全拼,一个为首名字字母)
4.容器的启动、 停止及重启操作
停止:
启动:
重启:
进入容器
[root@bogon ~]# docker exec -it wangjiasheng /bin/bash
root@dec77c79bb91:/#
5.怎么查看正在运行的容器和所有容器?
我又创建了一个容器,然后停止它,现在有三个容器,两个在运行
查看在运行的容器:
查看所有容器:
6.怎么退出容器: 两种方法分别实现?
进入容器
[root@bogon ~]# docker exec -it wjs /bin/bash
[root@37934146b020 /]#
退出:
第一种:exit
第二种: ctrl+d / ctrl+q
7.怎么连接到运行的容器?
1.使用dockers exec
使用 docker exec
命令:docker exec
命令用于在正在运行的容器内执行命令。你可以使用该命令在容器内部打开一个新的终端,并与容器进行交互。需要指定容器的名称或容器ID作为参数。例如:
docker exec -it <容器名称或ID> /bin/bash
[root@bogon ~]# docker exec -it wjs /bin/bash
[root@37934146b020 /]#
2.使用nsenter命令
使用 nsenter
命令:nsenter
是一个能够进入到命名空间的工具,可以用来连接到已经运行的容器。首先,获取容器的PID,可以使用 docker inspect
命令来查找容器的PID。然后,使用 nsenter
命令以附加模式连接到容器。例如:
docker inspect --format '{{.State.Pid}}' <容器名称或ID>
sudo nsenter -t <容器PID> -m -u -i -n -p /bin/bash
这将在容器内部打开一个新的 Bash 终端,并允许你直接与容器进行交互。
以上方法中,使用 -it
参数可以实现交互式的终端连接,/bin/bash
是默认的 shell,可以根据容器内部的配置来选择其他 shell。
8.查看容器或镜像的内部信息?
-
查看容器内部信息:
docker inspect <容器名或容器ID>
-
查看镜像的详细信息:
docker inspect <镜像名或镜像ID>
[root@bogon ~]# docker inspect wjs
[
{
"Id": "37934146b020fb100745bc1eba71cd7bf308c359d7579e3c5e39d4e7d1c59dfe",
"Created": "2023-08-22T04:19:02.012900892Z",
"Path": "tail",
"Args": [
"-f",
"/dev/null"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 22691,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-08-22T04:26:50.368763608Z",
"FinishedAt": "2023-08-22T04:26:30.072680729Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/37934146b020fb100745bc1eba71cd7bf308c359d7579e3c5e39d4e7d1c59dfe/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/37934146b020fb100745bc1eba71cd7bf308c359d7579e3c5e39d4e7d1c59dfe/hostname",
"HostsPath": "/var/lib/docker/containers/37934146b020fb100745bc1eba71cd7bf308c359d7579e3c5e39d4e7d1c59dfe/hosts",
"LogPath": "/var/lib/docker/containers/37934146b020fb100745bc1eba71cd7bf308c359d7579e3c5e39d4e7d1c59dfe/37934146b020fb100745bc1eba71cd7bf308c359d7579e3c5e39d4e7d1c59dfe-json.log",
"Name": "/wjs",
"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,
"ConsoleSize": [
30,
131
],
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"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/1c8fa53cb5774d432afacb27ace74f15bf053966cc480357fe3feaf9582dc8fc-init/diff:/var/lib/docker/overlay2/7692b11868e643af69f4cb7f2b6806bbdff5d36921549553ba617d55521ff777/diff",
"MergedDir": "/var/lib/docker/overlay2/1c8fa53cb5774d432afacb27ace74f15bf053966cc480357fe3feaf9582dc8fc/merged",
"UpperDir": "/var/lib/docker/overlay2/1c8fa53cb5774d432afacb27ace74f15bf053966cc480357fe3feaf9582dc8fc/diff",
"WorkDir": "/var/lib/docker/overlay2/1c8fa53cb5774d432afacb27ace74f15bf053966cc480357fe3feaf9582dc8fc/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "37934146b020",
"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": [
"tail",
"-f",
"/dev/null"
],
"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": "f3e4d3e233d000db9765b1cd229a27356f38d7b6988461a5fcc67cadd17dbb73",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/f3e4d3e233d0",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "ae718c71a7b0a34a86f7abb16968822e7536c97935735be45c18217268f7bbc5",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "5ed9b172b2fc6996976796777f5e744c1637d56e274d65c89ada062140a5517f",
"EndpointID": "ae718c71a7b0a34a86f7abb16968822e7536c97935735be45c18217268f7bbc5",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
[root@bogon ~]# docker inspect ubuntu
[
{
"Id": "sha256:01f29b872827fa6f9aed0ea0b2ede53aea4ad9d66c7920e81a8db6d1fd9ab7f9",
"RepoTags": [
"ubuntu:latest"
],
"RepoDigests": [
"ubuntu@sha256:ec050c32e4a6085b423d36ecd025c0d3ff00c38ab93a3d71a460ff1c44fa6d77"
],
"Parent": "",
"Comment": "",
"Created": "2023-08-04T04:53:00.244301537Z",
"Container": "822f331d59eb72d1131a8a5fcb2b935c8110114c22be26c8572d9881dcff31e0",
"ContainerConfig": {
"Hostname": "822f331d59eb",
"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/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"Image": "sha256:22d9eb9a70973f7eb625681c244522dad0bf3b4f8e9ea75977b09d8551364a19",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"DockerVersion": "20.10.21",
"Author": "",
"Config": {
"Hostname": "",
"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"
],
"Image": "sha256:22d9eb9a70973f7eb625681c244522dad0bf3b4f8e9ea75977b09d8551364a19",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 77823376,
"VirtualSize": 77823376,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/3b08437e6dca565278fd044216e06ea14f43fe8e7e14270857021f739be3daf2/merged",
"UpperDir": "/var/lib/docker/overlay2/3b08437e6dca565278fd044216e06ea14f43fe8e7e14270857021f739be3daf2/diff",
"WorkDir": "/var/lib/docker/overlay2/3b08437e6dca565278fd044216e06ea14f43fe8e7e14270857021f739be3daf2/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:bce45ce613d34bff6a3404a4c2d56a5f72640f804c3d0bd67e2cf0bf97cb950c"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
9.如何查看所有镜像?
- 使用以下命令查看所有已下载的镜像:
docker images
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 01f29b872827 2 weeks ago 77.8MB
centos latest 5d0da3dc9764 23 months ago 231MB
nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB