45

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
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
sudhir tataraju
  • 1,159
  • 1
  • 14
  • 30
  • 1
    Hi, can you give more details what you want to accomplish? what is the setup your trying to get? based on the error messages it sounds like docker does not support it – Mattias Wadman Feb 16 '19 at 08:14
  • How to change network of running docker container? – sudhir tataraju Feb 16 '19 at 10:13
  • Sorry maybe unclear question. What meant was that i don't think you're using the commands to change the networking of running container in the wrong way. It's more that you're trying to change it in way that is not supported. – Mattias Wadman Feb 16 '19 at 10:24

5 Answers5

49

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.

Rachid O
  • 13,013
  • 15
  • 66
  • 92
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • but if am attaching to bridge network, my running docker container all ports not exposing, also below error I was getting because my swarm was up after docker swarm leave command running again same command worked. [root@stagingrbt ~]# docker network connect docker_gwbridge cdf8d6e3013d error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/networks/docker_gwbridge/connect: EOF – sudhir tataraju Feb 16 '19 at 17:18
  • 2
    This should be marked as answer. – eco May 19 '22 at 01:30
  • I would like to highlight that "bridge" in the `docker network disconnect bridge alpine2` command should be replaced by the name of the docker network that the container is currently connected to. – Speedy Jul 19 '22 at 19:58
7

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
Christoph
  • 411
  • 5
  • 9
  • lifesaver, worked like a charm – mouchin777 Apr 25 '22 at 09:59
  • 1
    Although valuable the OP is on a linux system so a command line answer is necessary. i.e. See answer from Ortomala Lokni. All you are doing is creating a NEW container on a different network, that doesn't answer the question of the op: "I'm trying to update the network of a running docker container.". Running container. – eco May 19 '22 at 01:29
  • Works well! This did nothing to the data volume, which is what I wanted! – Jerther Apr 02 '23 at 17:45
0
Run or connect a container to a specific network: Note first of all, the network must exist already on the host. Either specify the network at container creation/startup time (docker create or docker run) with the --net option; or attach an existing container by using the docker network connect command. For example:

docker network connect my-network my-container

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 24 '22 at 19:58
0

I am not sure if we can change the container network while running, however, assuming that the new docker network already exists, you can run the following commands to update your container network.

Executed on Version: 20.10.21 Community Edition
# docker stop <container-name>
# docker network disconnect <old-network-id> <container-name> 
# docker network connect <new-network-id> <container-name>
# docker start <container-name>

Note: you won't be able to switch to host network from other network

Ravi Kulkarni
  • 527
  • 1
  • 7
  • 12
0

In case you are using swarm mode and want to connect to an external network, use this:

version: '3.6' # not 3.4

services:
  a_service:
    image: "nginx:alpine"
    command: sleep 10000
    networks:
      - outside

networks:
  outside:
    name: host
    external: true

with docker stack deploy -c filename.yml temp_stack

Also, check open ports.

Mohsenasm
  • 2,916
  • 1
  • 18
  • 22