Docker修改已经创建的容器-宿主机IP和端口

需求:将slave1的PORTS的宿主机IP改为127.0.0.1,以实现端口仅对本机开放;

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                 NAMES
68d8338e3f96        mysql:5.7.24        "docker-entrypoint..."   2 weeks ago         Up 2 minutes        33060/tcp, 0.0.0.0:3308->3306/tcp     slave2
2278ff4689d0        mysql:5.7.24        "docker-entrypoint..."   6 weeks ago         Up 2 minutes        33060/tcp, 0.0.0.0:3307->3306/tcp     slave1

第一步:停止docker服务(这里会自动停止所有docker容器)

systemctl stop docker

第二步:修改对应的容器的配置:
先记下docker ps获取到的CONTAINER ID,比如slave1对应的CONTAINER ID2278ff4689d0,记录前2到3位即可,如227;
进入到对应的容器所在的目录:(到containers目录后,输入227再按tab键即可自动补全)

cd /var/lib/docker/containers/2278ff4689d074522499429fb10e0e1cb5937844689874178d722e0f61d3c806/

使用vim修改配置文件hostconfig.json:

{
    "Binds":[
        "/home/slave1/my.cnf:/etc/my.cnf",
        "/home/slave1/mysql:/var/lib/mysql"
    ],
    "ContainerIDFile":"",
    "LogConfig":{
        "Type":"json-file",
        "Config":{

        }
    },
    "NetworkMode":"default",
    "PortBindings":{
        "3306/tcp":[
            {
                "HostIp":"127.0.0.1",
                "HostPort":"3307"
            }
        ]
    },
    "RestartPolicy":{
        "Name":"always",
        "MaximumRetryCount":0
    },
    "AutoRemove":false,
    "VolumeDriver":"",
    "VolumesFrom":null,
    "CapAdd":null,
    "CapDrop":null,
    "Dns":[

    ],
    "DnsOptions":[

    ],
    "DnsSearch":[

    ],
    "ExtraHosts":null,
    "GroupAdd":null,
    "IpcMode":"",
    "Cgroup":"",
    "Links":[

    ],
    "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":null,
    "BlkioDeviceReadBps":null,
    "BlkioDeviceWriteBps":null,
    "BlkioDeviceReadIOps":null,
    "BlkioDeviceWriteIOps":null,
    "CpuPeriod":0,
    "CpuQuota":0,
    "CpuRealtimePeriod":0,
    "CpuRealtimeRuntime":0,
    "CpusetCpus":"",
    "CpusetMems":"",
    "Devices":[

    ],
    "DiskQuota":0,
    "KernelMemory":0,
    "MemoryReservation":0,
    "MemorySwap":0,
    "MemorySwappiness":-1,
    "OomKillDisable":false,
    "PidsLimit":0,
    "Ulimits":null,
    "CpuCount":0,
    "CpuPercent":0,
    "IOMaximumIOps":0,
    "IOMaximumBandwidth":0
}

重新启动docker服务:

systemctl start docker

启动容器:

docker start XXX
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值