Adding a New Port to an Existing Docker Container
Overview
This guide explains how to add a new port to an existing Docker container using alpine/socat and Docker networking.
Steps
Create a Docker Network
First, create a Docker network to connect your containers.
1
docker network create my-network
Connect the Existing Container to the Network
Connect your existing container to the newly created network.
1
docker network connect my-network my-container
Run
alpine/socatto Forward the PortRun a new Docker container using the
alpine/socatimage to forward the desired port to the existing container. This example forwards port1883.1
docker run -d --name rabbitmq-mqtt-proxy --network my-network -p 1883:1883 alpine/socat tcp-listen:1883,fork,reuseaddr tcp-connect:my-container:1883
-d: Run the container in detached mode.--name rabbitmq-mqtt-proxy: Name the new containerrabbitmq-mqtt-proxy.--network my-network: Connect the new container tomy-network.-p 1883:1883: Map port1883on the host to port1883in the container.alpine/socat: Use thealpine/socatimage.tcp-listen:1883,fork,reuseaddr: Listen on port1883and forward connections.tcp-connect:my-container:1883: Connect to port1883on the existing container namedmy-container.
Summary
By following these steps, you can forward a new port to an existing Docker container using alpine/socat and Docker networking. This allows you to expose additional services without modifying the original container configuration.
This post is licensed under CC BY 4.0 by the author.