How to change the network of a running docker container?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)