Docker容器添加新端口映射
查看运行的容器
docker ps
查看容器挂载目录
docker inspect [容器id]
我这里是[容器name]
停止容器
docker stop python
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
停止docker服务
systemctl stop docker
进入容器挂载目录
修改config.v2.json文件
{"StreamConfig":{},"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":0,"ExitCode":137,"Error":"","StartedAt":"2023-12-28T07:14:02.730791168Z","FinishedAt":"2024-01-03T08:05:02.667062922Z","Health":null},"ID":"8dc4d34541a0d1c92786995d75619786ac82dc1031f36918ebc4c5a9a6e2937a","Created":"2023-12-28T07:14:02.473478097Z","Managed":false,"Path":"python3","Args":[],"Config":{"Hostname":"8dc4d34541a0","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":{"80/tcp":{}},"Tty":true,"OpenStdin":true,"StdinOnce":false,"Env":["PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","LANG=C.UTF-8","GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568","PYTHON_VERSION=3.9.18","PYTHON_PIP_VERSION=23.0.1","PYTHON_SETUPTOOLS_VERSION=58.1.0","PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/4cfa4081d27285bda1220a62a5ebf5b4bd749cdb/public/get-pip.py","PYTHON_GET_PIP_SHA256=9cc01665956d22b3bf057ae8287b035827bfd895da235bcea200ab3b811790b6"],"Cmd":["python3"],"Image":"python:3.9","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"sha256:edb6b76b75bad04bef16fec848f73e45c0b4522d77cf389baff429081157fa1a","ImageManifest":null,"NetworkSettings":{"Bridge":"","SandboxID":"9b46c5df4c1dcd3cfbaac30fe56133a0934441bdbb550683ebf1723d196f1445","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"13c59dd8b27c93fe0532208bcfc7ba050657ea0819b07b3aab3ca6f0cb41fb8a","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"5000"}]},"SandboxKey":"/var/run/docker/netns/9b46c5df4c1d","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/8dc4d34541a0d1c92786995d75619786ac82dc1031f36918ebc4c5a9a6e2937a/8dc4d34541a0d1c92786995d75619786ac82dc1031f36918ebc4c5a9a6e2937a-json.log","Name":"/python","Driver":"overlay2","OS":"linux","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":true,"MountPoints":{},"SecretReferences":null,"ConfigReferences":null,"MountLabel":"","ProcessLabel":"","AppArmorProfile":"","SeccompProfile":"","NoNewPrivileges":false,"HostnamePath":"/var/lib/docker/containers/8dc4d34541a0d1c92786995d75619786ac82dc1031f36918ebc4c5a9a6e2937a/hostname","HostsPath":"/var/lib/docker/containers/8dc4d34541a0d1c92786995d75619786ac82dc1031f36918ebc4c5a9a6e2937a/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/8dc4d34541a0d1c92786995d75619786ac82dc1031f36918ebc4c5a9a6e2937a/resolv.conf","LocalLogCacheMeta":{"HaveNotifyEnabled":false}}
位置1:
"ExposedPorts":{"80/tcp":{}}
位置2:
"Ports":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"5000"}]}
修改hostconfig.json文件
{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"5000"}]},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"ConsoleSize":[22,204],"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","/sys/devices/virtual/powercap"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}
位置1:
"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"5000"}]}
重启容器
systemctl start docker
启动容器
docker start python