0

I am building a library to simulate some faults in a system. One of the faults, is to simulate a network failure, which would forbid any connection. Currently, I am using this Kotlin code to disable the container network interface:

Runtime.getRuntime().exec("ifconfig eth0 down")
// wait some time
Runtime.getRuntime().exec("ifconfig eth0 up")

When the interface is re-enabled, I am not able to restore connections with the container. I tried it on the command line, and the effect is the same:

docker run --privileged -it alpine:latest sh
/ # apk add curl
...
OK: 7 MiB in 18 packages
/ # curl google.com

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

/ # ifconfig eth0 down
/ # ifconfig eth0 up
/ # curl google.com
curl: (6) Could not resolve host: google.com

Does anybody have any idea why it occurs inside a docker container?

1 Answer 1

0

The problem was that Docker was losing the default gateway address. I just added another command to reset the gateway address after restarting the interface and everything worked again:

route add default gw ${this.defaultGatewayAddress}

At the end, I got this working Kotlin code:

data class NetworkInterface(val name: String) {

    private var defaultGatewayAddress: String

    init {
        this.defaultGatewayAddress = getDefaultGatewayIpAddress().address
    }

    fun disable() {
        Environment.runCLICommand("ifconfig $name down")
    }

    fun enable() {
        Environment.runCLICommand("ifconfig $name down")
        Environment.runCLICommand("route add default gw ${this.defaultGatewayAddress}")
    }

    fun getDefaultGatewayIpAddress(): IpAddress {
        val command = "netstat -nr | awk '{print $2}' | head -n3 | tail -n1"
        return IpAddress(Environment.runCLICommand(command).trim())
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.