Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the network of a running docker container?

Tags:

docker

I'm trying to update the network of a running docker container.

Note: I didn't attach any network while running the container.

[root@stagingrbt ~]# docker network connect host cdf8d6e3013d Error response from daemon: container sharing network namespace with another container or host cannot be connected to any other network  [root@stagingrbt ~]# docker network connect docker_gwbridge cdf8d6e3013d error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/networks/docker_gwbridge/connect: EOF   [root@stagingrbt ~]# docker network create -d host my-host-network Error response from daemon: only one instance of "host" network is allowed   [root@stagingrbt ~]# docker network ls NETWORK ID          NAME                DRIVER              SCOPE 495080cf93e3        bridge              bridge              local cf0408d6f13f        docker_gwbridge     bridge              local 2c5461835eaf        host                host                local 87e9cohcbogh        ingress             overlay             swarm 84dbd78101e3        none                null                local 774882ac9b09        sudhirnetwork       bridge              local 
like image 728
sudhir tataraju Avatar asked Feb 16 '19 06:02

sudhir tataraju


People also ask

How do I add a running container to a network?

If you want to add a container to a network after the container is already running, use the docker network connect subcommand. You can connect multiple containers to the same network. Once connected, the containers can communicate using only another container's IP address or name.

Can a Docker container have multiple networks?

You can create multiple networks with Docker and add containers to one or more networks. Containers can communicate within networks but not across networks. A container with attachments to multiple networks can connect with all of the containers on all of those networks.

How do I change the IP address of a docker container?

When you connect an existing container to a different network using docker network connect, you can use the --ip or --ip6 flags on that command to specify the container’s IP address on the additional network.

How to change the name of a VPN network in Docker?

The simplest approach to this would be to delete the VPN network and create it anew with new parameters but the same name. If you use docker-compose up to recreate containers, include the networks section in the first container that you recreate. Then add the macvlan network definition to yml of your first re-created container.

How are Docker containers connected to the network?

The IP address is assigned from the pool assigned to the network, so the Docker daemon effectively acts as a DHCP server for each container. Each network also has a default subnet mask and gateway. When the container starts, it can only be connected to a single network, using --network.

How do I make a port available to services outside of Docker?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world. Here are some examples.


2 Answers

When you start a container, such as:

docker run -d --name alpine1 alpine 

It is by default connected to the bridge network, check it with:

docker container inspect alpine1 

If you try to connect it to host network with:

docker network connect host alpine1 

you obtain an error:

Error response from daemon: container cannot be disconnected from host network or connected to host network

you have to delete the container and run it again on the host network:

docker stop alpine1 docker rm alpine1 docker run -d --network host --name alpine1 alpine 

This limitation is not present on bridge networks. You can start a container:

docker run -d --name alpine2 alpine 

disconnect it from the bridge network and reconnect it to another bridge network.

docker network disconnect bridge alpine2 docker network create --driver bridge alpine-net docker network connect alpine-net alpine2 

Note also that according to the documentation:

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

like image 116
Ortomala Lokni Avatar answered Oct 13 '22 11:10

Ortomala Lokni


If you want to circumvent the command line and change the network of your docker container via portainer, you can do so. I'm not sure exactly which is the best way of doing this, but the steps below worked for me (changing a container that was running on the bridge network by default into the host network):

  1. In the Container list, click on the container name (emby, in my case)
  2. Stop the container
  3. Click on Duplicate/Edit
  4. Scroll down to Advanced container settings and select the Network tab
  5. Change the Network to host (or whatever you want to set it to)
  6. Click on Deploy the container right above.
  7. Confirm that you want to replace the old container (or deploy it under a new name if you want to be on the save side and keep the old one).
  8. Done! Screenshot of Advanced container settings
like image 35
Christoph Avatar answered Oct 13 '22 12:10

Christoph