How to assign a port mapping to an existing Docker container?
To assign a port mapping to an existing Docker container, you can follow these steps:
Identify the Container: Find the container ID or name of the Docker container you want to modify. You can use the
docker ps
command to list all running containers:docker ps
Stop the Container (if it's running): If the container is running, stop it using the following command. Replace
CONTAINER_ID
with the actual ID or name of your container:docker stop CONTAINER_ID
Remove the Existing Container (if necessary): If you prefer to keep the existing container's data, you can skip this step. However, if you want to start a new container based on the same image but with a different port mapping, you might need to remove the existing container. Be careful as removing a container deletes any changes made inside the container:
docker rm CONTAINER_ID
Create a New Container with the Desired Port Mapping: You can create a new container based on the existing container's image and define the port mapping at this stage. Use the
p
flag to map the ports:docker run -p NEW_HOST_PORT:EXISTING_CONTAINER_PORT --name NEW_CONTAINER_NAME IMAGE_NAME
NEW_HOST_PORT
: This is the port on the host machine where you want to map the container's port.EXISTING_CONTAINER_PORT
: This is the port inside the container that you want to expose.NEW_CONTAINER_NAME
: This is the name you want to assign to the new container.IMAGE_NAME
: This is the image of the existing container.
Start the New Container: Once you've created the new container with the modified port mapping, start it:
docker start NEW_CONTAINER_NAME
Remember to replace the placeholder values (like NEW_HOST_PORT
, EXISTING_CONTAINER_PORT
, NEW_CONTAINER_NAME
, and IMAGE_NAME
) with your actual port numbers, container name, and image name.
By following these steps, you can re-create a container based on an existing image with a different port mapping. This allows you to assign a new port to an existing Docker container without directly modifying the port mappings of a running container.
-
How To Deal With Persistent Storage (e.g. Databases) In Docker?
The best way to deal with persistent data storage (such as a database) in Docker is to use Docker’s volume API (for docker 1.9.0 or newer) or use data-only containers for older versions of Docker. ...
Questions -
How to Mount a Host Directory in a Docker Container?
If you want to mound a host directory in a Docker container, you have to main ways to do that: Using the ADD command: The simplest way is to use the dockers ADD command as shown below: ADD . /path/...
Questions -
How to view log output using docker-compose run?
You can start Docker compose in detached mode and attach yourself to the logs of all containers later. If you're done watching logs you can detach yourself from the logs output without shutting dow...
Questions -
How to fix docker: Got permission denied while trying to connect to the Docker daemon socket
This error may appear when running docker commands and it is caused by insufficient privilages. Solution To resolve the problem, you need to do the following: Create a docker group sudo groupadd do...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github