当前位置:首页 » 《关于电脑》 » 正文

Docker给容器添加新端口映射

13 人参与  2024年04月12日 08:38  分类 : 《关于电脑》  评论

点击全文阅读


Docker容器添加新端口映射

目录

Docker容器添加新端口映射前言方法一:创建新容器方法二:创建新容器使用Docker Compose停止并移除当前服务:修改docker-compose.yml文件启动服务: 常用命令查看运行的容器查看容器挂载目录停止容器停止docker服务进入容器挂载目录修改config.v2.json文件位置1:位置2: 修改hostconfig.json文件位置1: 重启容器启动容器

前言

Docker容器创建后,其网络配置通常是固定的,包括端口映射。基于Docker的原生工具,一旦容器启动,就无法直接修改其端口映射。然而,有几种方法可以解决需要对现有容器添加新端口映射的问题。

方法一:创建新容器

最直接的方法是创建一个新的容器,并在创建时指定所有需要的端口映射。这可以通过以下步骤完成:

停止当前运行的容器:

docker stop <旧容器名或ID>

删除旧容器(可选,但如果你想重用容器名,这是必须的):

docker rm <旧容器名或ID>

启动一个新容器,并添加新的端口映射:

docker run -d -p <新端口>:<容器端口> --name <容器名> <其他选项> <镜像名>

这里的<新端口>是你希望在宿主机上公开的端口,<容器端口>是容器内部要映射的端口。

如果旧容器有持久化的数据或者特定的配置,确保在创建新容器时通过挂载相同的数据卷或传递相同的环境变量等来保留这些设置。

方法二:创建新容器

使用Docker Compose

如果你在使用Docker Compose来管理你的容器,修改端口映射比较简单。你只需要在docker-compose.yml文件中更新ports部分,然后重新启动服务:

停止并移除当前服务:

docker-compose down

修改docker-compose.yml文件

修改docker-compose.yml文件中的ports部分,添加新的端口映射。

启动服务:

docker-compose up -d

这些步骤将重新创建服务中定义的容器,并应用新的端口映射。

在处理端口映射问题时,请记住,重新创建容器可能涉及到数据迁移和配置复制,因此在执行任何操作之前请确保数据的备份和迁移策略已经到位。

常用命令

查看运行的容器

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

在这里插入图片描述


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/94087.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1