How to Assign Port Mapping to an Existing Docker Container?

To assign port mapping to a Docker container that is already running, we need to know that we can’t change the port mapping directly on it. The best way is to stop the current container. Then we can create a new one with the ports we want. This way, our application will use the right ports to communicate. It also keeps everything working well while following Docker’s rules.

In this article, we will look at different ways to assign port mapping to Docker containers. We will talk about why we cannot change the port mapping of a running container. We will also explain how to create a new container with the port mappings we need. We will cover how to use Docker commit for changes. Plus, we will discuss how to use Docker networks for managing ports and how to expose ports for containers that already exist. Here are the solutions we will explain:

  • How to assign port mapping to an existing Docker container
  • Why you cannot change port mapping of a running Docker container
  • How to create a new Docker container with port mapping
  • How to use Docker commit to assign port mapping
  • How to use Docker network to manage port mapping
  • How to expose ports for existing Docker containers
  • Frequently asked questions on port mapping in Docker

Why You Cannot Change Port Mapping of a Running Docker Container

We cannot change the port mapping of a running Docker container. This is because of how Docker manages network ports. When we start a container with a certain port mapping using the -p (or --publish) option, Docker creates a link between the host’s port and the container’s port. This link is made when we create the container and it cannot be changed while the container is running.

Here are some key reasons why:

  • Network Namespace Isolation: Each container runs in its own network space. Once a port is linked to a container’s network space, Docker does not let us change it. This keeps things secure and separate.

  • Port Binding Conflicts: If we could change port mappings freely, it could cause problems. Multiple containers might try to use the same host port. This would create network issues.

  • Data Integrity: Changing port mappings while the container is running could interrupt connections and data transfers. This would make our applications less reliable.

If we want to change port mappings, we need to stop the running container first. Then, we create a new container with the new port settings. We can stop a running container with this command:

docker stop <container_name_or_id>

After stopping it, we can start a new container with the port mapping we want:

docker run -d -p <host_port>:<container_port> <image_name>

For example, if we want to link host port 8080 to container port 80, we would use:

docker run -d -p 8080:80 nginx

By following this process, we can change the port mapping for our Docker containers while keeping everything stable and safe.

How to Create a New Docker Container with Port Mapping

To create a new Docker container with port mapping, we can use the docker run command. This command lets us set port mappings with the -p option. This option maps a port on the host to a port in the container.

Syntax

docker run -d -p <host_port>:<container_port> --name <container_name> <image_name>

Example

For example, if we want to run a web server in a Docker container and make it available on port 8080 of the host while the container listens on port 80, we would run:

docker run -d -p 8080:80 --name my_web_server nginx

In this example: - -d runs the container in background. - -p 8080:80 connects port 8080 on the host to port 80 in the container. - --name my_web_server gives a name to the container. - nginx is the Docker image we use to build the container.

Multiple Port Mapping

We can also map more than one port by adding more -p options:

docker run -d -p 8080:80 -p 443:443 --name my_web_server nginx

This command maps: - Host port 8080 to container port 80 (HTTP) - Host port 443 to container port 443 (HTTPS)

Using Docker Compose

If we like using Docker Compose, we can set port mappings in a docker-compose.yml file:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
      - "443:443"

We run the services with:

docker-compose up -d

This will start the services that we defined in the docker-compose.yml file with the port mappings we specified.

For more information on Docker commands, check out this guide on Docker commands.

How to Use Docker Commit to Assign Port Mapping

To assign port mapping to a Docker container, we first need to stop the container that is running. The docker commit command helps us create a new image from the changes made to the existing container. We can set port mappings when we create a new container from that image.

  1. Stop the Existing Container:

    docker stop <container_name_or_id>
  2. Commit Changes to Create a New Image: We use the docker commit command with the -p option to set the port mappings. But remember that docker commit does not directly assign the port mapping. We will do this when we run a new container from the image.

    docker commit <container_name_or_id> <new_image_name>
  3. Run a New Container with Port Mapping: After we create a new image, we can start a new container with the port mapping we want. We use the -p flag to connect the container port to the host port.

    docker run -d -p <host_port>:<container_port> <new_image_name>

Example

If we have a container named my_app_container running on port 80 and we want to map it to port 8080 on the host, we do the steps below:

# Stop the existing container
docker stop my_app_container

# Commit the container to a new image
docker commit my_app_container my_app_image

# Run a new container with port mapping
docker run -d -p 8080:80 my_app_image

This way, we can create a new image from an existing container and run it with the port mappings we want. For more info on Docker commands, we can check this article on Docker commands.

How to Use Docker Network to Manage Port Mapping

Docker networking gives us good ways to manage port mapping. With custom networks, we can control how containers talk to each other and to the outside world.

Creating a Custom Network

To use Docker networks for port mapping, we first need to create a custom network. We can do this with the following command:

docker network create my_custom_network

Running Containers on a Custom Network

When we start a container, we can choose the network to connect it to by using the --network flag. Here is how to run a container with port mapping on the custom network:

docker run -d --name my_container --network my_custom_network -p 8080:80 nginx

In this case, port 80 inside the container is linked to port 8080 on the host.

Inspecting Network Configuration

To see the details of our custom network, we can use:

docker network inspect my_custom_network

This command shows us information about the containers that are connected to the network, their IP addresses, and other settings.

Connecting Existing Containers to a Network

If we have containers that are already running and we want to connect them to our custom network, we can use this command:

docker network connect my_custom_network existing_container_name

Disconnecting Containers from a Network

To remove a container from a network, we can use:

docker network disconnect my_custom_network existing_container_name

Use Case for Port Mapping with Custom Networks

  1. Service Discovery: Containers on the same custom network can talk to each other using their container names. This means we do not need to use port mapping.
  2. Isolation: Custom networks help us keep services separate. This gives us better security and makes it easier to manage.

If you want to learn more about Docker networking, you can check out what are Docker networks and why are they necessary.

How to Expose Ports for Existing Docker Containers

To expose ports for existing Docker containers, we cannot change the port settings of a running container. But we can create a new container with the port settings we want. Or we can use some tricks. Here’s how we can do it.

Using Docker Network

  1. First, we create a user-defined bridge network:

    docker network create my_bridge_network
  2. Next, we run a new container with the port settings on this network:

    docker run -d --name my_container --network my_bridge_network -p 8080:80 my_image

Using Docker Commit

  1. We can also commit changes to make a new image from the existing container:

    docker commit existing_container new_image_name
  2. After that, we run a new container from the new image with the port settings:

    docker run -d --name new_container -p 8080:80 new_image_name

Exposing Ports with docker run and docker-compose

For new containers, we can expose ports right away using the -p option in docker run or in a Docker Compose file.

Example with docker run:

docker run -d -p 8080:80 my_image

Example using Docker Compose:

version: '3'
services:
  my_service:
    image: my_image
    ports:
      - "8080:80"

Alternative: Using docker exec for Temporary Access

We can access a container temporarily using exec commands. This does not expose a port but lets us work inside the container:

docker exec -it existing_container /bin/bash

Note on Port Exposure

  • We should always check that the host port is not in use. This helps us avoid problems.
  • For more details on Docker networks, we can read this article.

Frequently Asked Questions

1. Can we change the port mapping of a running Docker container?

No, we cannot change the port mapping of a running Docker container. Docker does not let us change the settings of a container that is already active. If we need different port mappings, we must stop the container. Then we remove it and make a new one with the port mapping we want. We do this using the -p flag in the docker run command. For more details, we can check our guide on how to create a new Docker container with port mapping.

2. How do we expose ports for an existing Docker container?

To expose ports for an existing Docker container, we cannot change the port mappings of a running container directly. But we can use Docker’s commit command. This command helps us make a new image from the existing container. After that, we run a new container from that image with the port mappings we want. This way, we keep the state of the original container while changing access to its ports.

3. What is the difference between exposing and publishing ports in Docker?

In Docker, exposing a port means it is available for communication between containers in the same network. But it does not allow access from the host machine. On the other hand, publishing a port maps a container’s port to a port on the host. This way, we allow external access. For more information, we can explore our article on how to expose ports in Docker containers.

4. How can we check which ports are being used by our Docker containers?

We can check which ports are being used by our Docker containers with the command docker ps. This command shows all running containers with their port mappings. If we want detailed information about a specific container, including its port usage, we can use docker inspect <container_id>. This command gives us complete information, including the network settings of the container.

5. Is there any way to manage port mapping with Docker networks?

Yes, Docker networks help us manage port mappings well, especially for applications with many containers. By making custom networks, we can control which containers can talk to each other and on which ports. This method also makes our setup more secure and organized. For more insights, we can check our article on what are Docker networks and why are they necessary.