Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable network for a running Docker container?

Tags:

docker

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.

like image 817
Mitar Avatar asked May 19 '19 20:05

Mitar


People also ask

What is the docker command to disconnect a container from bridge network?

To disconnect a running container from a user-defined bridge, use the docker network disconnect command.

What are the networking concerns with Docker containers?

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.

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.

How to disable networking for a container in Linux?

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.

Is it possible to run a docker image without a network?

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).


1 Answers

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>

enter image description here

like image 56
tgogos Avatar answered Oct 14 '22 00:10

tgogos