I would like to start a Docker container normally, run it, install some things into it, and then I would like to disable the network, to run some more commands in it, but they should not have access to the network. How can I do that for a running container?
I use docker-py and I know I can use network_disabled
to disable networking for the whole container. But I am not sure how I can disable the network after the container is already created. Ideally, I would run the container with command sleep infinity
, then docker exec
some commands in it, then disable networking, then run few more commands using docker exec
.
To disconnect a running container from a user-defined bridge, use the docker network disconnect command.
This topic is about networking concerns from the point of view of the container. By default, when you create or run a container using docker create or docker run , it does not publish any of its ports to the outside world.
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.
Disable networking for a container. 1 Create the container. 2 Check the container’s network stack, by executing some common networking commands within the container. Notice that no eth0 was created. 3 Stop the container. It is removed automatically because it was created with the --rm flag.
This actually fits your immediate need quite nicely, since once you've built the image you can run it without network. Generally you should set your image up so that docker run does everything the container needs to do, without needing to docker exec ever (except for hand debugging).
Maybe an option would be docker network disconnect
Description
Disconnect a container from a network
Usage
docker network disconnect [OPTIONS] NETWORK CONTAINER
Example:
Create a container attached to the default bridge network
docker container run --rm -it alpine ping 8.8.8.8
and after a while disconnect it with:
docker network disconnect bridge <container-name>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With