添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
文章详细介绍了如何在已经运行的Docker容器中添加端口映射,包括停止容器、修改`hostconfig.json`和`config.v2.json`配置文件、重启Docker服务以及验证端口映射的步骤。主要涉及的命令有`dockerstop`、`systemctlstart/stop`以及查看端口占用的`lsof`命令。 摘要由CSDN通过智能技术生成

docker 在已经启动的容器中开启端口映射

1. 老办法就是打包再启动

2. 修改配置文件

2.1. 先关闭已经启动的所有容器

docker stop [ID/Name]

2.2. 关闭docker服务

systemctl stop docker
systemctl stop docket.socket

2.3. 找到docker配置文件

cd /var/lib/docker/containors/your_containor_id

在找这个ID的时候要先在之前docker没关闭之前查看

docker ps -a

2.4. 修改hostconfig.json 和 config.v2.json

配置文件如下

  • 这个是hostconfig.json
  • 在这里要把映射的端口信息写入
"Binds":[ "//home/kali/Desktop/docker/share/:/home/share/" "ContainerIDFile":"", "LogConfig":{ "Type":"json-file", "Config":{ "NetworkMode":"default", "PortBindings":{ "10086/tcp":[{ "HostIp":"", "HostPort":"10086" }], "80/tcp":[{ "HostIp":"", "HostPort":"80" }], "443/tcp":[{ "HostIp":"", "HostPort":"443" "RestartPolicy":{ "Name":"no", "MaximumRetryCount":0 "AutoRemove":false, "VolumeDriver":"", "VolumesFrom":null, "CapAdd":null, "CapDrop":null, "CgroupnsMode":"private", "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":[ "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":null, "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"
  • 这里是config.v2.json
  • 这里要把暴露的端口写入
"StreamConfig":{ "State":{ "Running":false, "Paused":false, "Restarting":false, "OOMKilled":false, "RemovalInProgress":false, "Dead":false, "Pid":0, "ExitCode":137, "Error":"", "StartedAt":"2023-07-11T06:34:07.531152901Z", "FinishedAt":"2023-07-11T06:50:44.834512517Z", "Health":null "ID":"532f96985e4536759554cc7c2b68642e3075ec0dca5aed1fffc19c749e8907a2", "Created":"2023-07-11T06:34:07.211250431Z", "Managed":false, "Path":"bash", "Args":[ "Config":{ "Hostname":"532f96985e45", "Domainname":"", "User":"", "AttachStdin":false, "AttachStdout":false, "AttachStderr":false, "ExposedPorts":{ "80/tcp":{}, "10086/tcp":{}, "443/tcp":{} "Tty":true, "OpenStdin":true, "StdinOnce":false, "Env":[ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8" "Cmd":[ "bash" "Image":"kalilinux/kali-last-release", "Volumes":null, "WorkingDir":"", "Entrypoint":null, "OnBuild":null, "Labels":{ "io.buildah.version":"1.28.2", "org.opencontainers.image.authors":"Kali Developers \[email protected]\u003e", "org.opencontainers.image.created":"2023-07-09T04:26:43Z", "org.opencontainers.image.description":"Official Kali Linux container image for 2023.2", "org.opencontainers.image.revision":"b65b5a63", "org.opencontainers.image.source":"https://gitlab.com/kalilinux/build-scripts/kali-docker", "org.opencontainers.image.title":"Kali Linux (2023.2 branch)", "org.opencontainers.image.url":"https://www.kali.org/", "org.opencontainers.image.vendor":"OffSec", "org.opencontainers.image.version":"2023.2" "Image":"sha256:727e3f9a0dbc1449e9fcb04b902e717c85004383fbdff13fb4c3808ac3b8773d", "NetworkSettings":{ "Bridge":"", "SandboxID":"71b8410fa517e48b860dd8c8d8c63cd39c18e7463174c4104e3ce16cbd9f3f67", "HairpinMode":false, "LinkLocalIPv6Address":"", "LinkLocalIPv6PrefixLen":0, "Networks":{ "bridge":{ "IPAMConfig":null, "Links":null, "Aliases":null, "NetworkID":"d2820cac2564714950b4a883d348b753d5fe01a4b2b699990d09df451ea6d929", "EndpointID":"", "Gateway":"", "IPAddress":"", "IPPrefixLen":0, "IPv6Gateway":"", "GlobalIPv6Address":"", "GlobalIPv6PrefixLen":0, "MacAddress":"", "DriverOpts":null, "IPAMOperational":false "Service":null, "Ports":null, "SandboxKey":"/var/run/docker/netns/71b8410fa517", "SecondaryIPAddresses":null, "SecondaryIPv6Addresses":null, "IsAnonymousEndpoint":false, "HasSwarmEndpoint":false "LogPath":"/var/lib/docker/containers/532f96985e4536759554cc7c2b68642e3075ec0dca5aed1fffc19c749e8907a2/532f96985e4536759554cc7c2b68642e3075ec0dca5aed1fffc19c749e8907a2-json.log", "Name":"/x-ui", "Driver":"overlay2", "OS":"linux", "MountLabel":"", "ProcessLabel":"", "RestartCount":0, "HasBeenStartedBefore":true, "HasBeenManuallyStopped":true, "MountPoints":{ "/home/share":{ "Source":"/home/kali/Desktop/docker/share", "Destination":"/home/share", "RW":true, "Name":"", "Driver":"", "Type":"bind", "Propagation":"rprivate", "Spec":{ "Type":"bind", "Source":"//home/kali/Desktop/docker/share/", "Target":"/home/share/" "SkipMountpointCreation":false "SecretReferences":null, "ConfigReferences":null, "AppArmorProfile":"docker-default", "HostnamePath":"/var/lib/docker/containers/532f96985e4536759554cc7c2b68642e3075ec0dca5aed1fffc19c749e8907a2/hostname", "HostsPath":"/var/lib/docker/containers/532f96985e4536759554cc7c2b68642e3075ec0dca5aed1fffc19c749e8907a2/hosts", "ShmPath":"", "ResolvConfPath":"/var/lib/docker/containers/532f96985e4536759554cc7c2b68642e3075ec0dca5aed1fffc19c749e8907a2/resolv.conf", "SeccompProfile":"", "NoNewPrivileges":false, "LocalLogCacheMeta":{ "HaveNotifyEnabled":false

3. 重启docker服务

systemctl start docker

4. 查看容器端口映射

docker start [your_containor_id/name]
docker post [your_containor_id/name]
docker ps -a

在本机查看一下端口占用

lsof -i:22
lsof -i:80
lsof -i:443
lsof -i:10086
				
如果用docker run启动容器的时候发现自己忘记指定 -p 进行端口映射了,可以采取如下三种方式来增加端口映射: 1.第一种是最无脑的方式,直接删除当前容器,然后用原镜像重新跑一个新的容器。简单暴力,但是如果是需要配置的东西过多的话,这样反而会比较麻烦,而且这个时候是新的容器,操作不当,可能丢失某些重要的状态或者数据。 用commit的方式把当前容器重新打包成一个新的镜像,相比于1,好处是不...
1* 本来是想启动两个redis容器的(已经改过了配置文件的端口),但是当我进入到6380端口的redis客户端的时候却报错了! [root@zch config]# docker exec -it redis2 redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected> 2* 于是,我回到了启动容器的代码处 [root@zch config]# docker r
Docker运行容器映射端口 在使用Docker容器时,经常会遇到需要将容器内部的端口映射到主机上的情况。这个功能非常重要,因为它能够让外部的应用程序能够访问到容器内部的服务,比如Web服务器、数据库等。本文将介绍如何使用Docker运行容器映射端口,并提供一些代码示例。 什么是Docker [Docker](
docker运行容器添加端口映射 docker上面运行容器,run -p 的时候只映射了一个端口,后面对该nginx做扩展,需要开放其他端口 Warning: Stopping docker.service, but it can still be activated by: docker.socket systemctl stop docker.socket iptable转发 通过以上命令创建新的镜像文件,run -p参数开放新端口出来;实际使用上不方便。 宿主机(host)上修改iptables 规则,开放容器的响应端口;参考网上的命令 iptables -t nat -A DOCKER -p tcp -dport 8080 -j DNAT --to-destination 172.17.0.2:8080 当我们创建nginx镜像时,并且启动nginx时,我们只能在容器内部区访问n