Reputation: 33
I have two Docker containers. Container One is named 5extraextranodes_plc1_1
, and is run with docker-compose on a bridge network named test_net
.
Container 2 is built and run outside of docker-compose. In container 2, there is a Python3 script. Inside this Python3 script, I want to be able to look up the 5extraextranodes_plc1_1
container's IP address.
I originally tried to use subprocess.checkout()
inside the Python code to run the docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 5extraextranodes_plc1_1
command. But that gave me an error FileNotFoundError: [Errno 2] No such file or directory: 'docker'.
So now I am trying to do this using Docker SDK inside of my Python 3.6 script. I'm running the following code:
client = docker.from_env()
container = client.containers.list(filters={"name": "5extraextranodes_plc1_1"})
ip_add = container.attrs['NetworkSettings']['IPAddress']
Unfortunately, this code gives me the error: docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')) for the statement client = docker.from_env()
.
How can I look up the 5extraextranodes_plc1_1
container's IP address from Container 2 on the same network?
Note: I am currently running Container 2 on the network test_net
(same network as Container 1). I've also tried running it in network host
mode. Both fail.
Upvotes: 0
Views: 441
Reputation: 1940
If you do not want to use a common network and Docker DNS like stated by @larsks:
By default containers are not allowed to change stuff on host system, this is a core concept of docker. But you're able to explicitly allow access to docker sock:
docker run -v /var/run/docker.sock:/var/run/docker.sock container2
But keep in mind: This container gains superpower and should be secured very well if necessary.
If you have further problems please provide Dockerfile from container2
Upvotes: 0