HomeНаука и техникаRelated VideosMore From: Macgyver

How To Interface Between Multiple Docker Containers and Host

65 ratings | 10264 views
In this video you will learn how to connect to services within docker containers from the host and also from container to container. Full Blog Post: https://askmacgyver.com/blog/tutorial/how-to-interface-between-multiple-docker-containers-and-host
Html code for embedding videos on your blog
Text Comments (21)
Roman Krček (13 days ago)
Very useful, thank you. :)
Tushar Hotchandani (5 months ago)
Please someone help me.....when i am installing packages on any container then it is too slow ....like i installed apt-get install gedit then it takes lots of time......and becomes idle in middle ......but when i am typing this out of docker in the root then packages install normally...what could be the problem
Macgyver (1 month ago)
Lack of ram potentially.
Dingo (1 year ago)
Here is better solution https://stackoverflow.com/questions/37242217/access-docker-container-from-host-using-containers-name?answertab=active#tab-top
Macgyver (1 year ago)
Sure it's a good solution but involves more overhead. The answer outlined in the video is more direct. From there is can be abstracted for ease of use.
Max Copley (1 year ago)
docker inspect < > | egrep '"IPAddress":[^,]+'
steelherrera (1 year ago)
Could you help me?
steelherrera (1 year ago)
I did it! Thanks
steelherrera (1 year ago)
Thanks
Macgyver (1 year ago)
For the first part you will have a host or localmachine that will house the two running containers. One container will contain a php/apache server and the other will have a mysql instance/server. This is as easy as easy as running 'run -it -d mysql' and 'run -it -d apache', you might have to log into each and check that everything is running but likely they will start by default. The issue you will have is getting the containers to talk to eachother, the default ip's and ports will no longer be sufficient because you are running them in isolated environments instead of a single environment. So you will have to change the default listening ip's and ports. Your php can't connect to mysql because it listening on the wrong ip or port. You need to target the mysql port against the ip of the mysql container. To get this ip simply inspect the container 'docker inspect containerId' and look for the exposed IP. Then in the php container find out how to change the default IP in the mysql config. You need to better understand how this dirvers (programming code) talk to running servers and it will make more sense. Go through the config code, try to understand what localhost means, etc. Likely your php mysql config code is referencing localhost which needs to be replaced with the mysql container ip. Ping me if you need further help.
steelherrera (1 year ago)
I want to create a web server using docker, so that in one container I have php/apache running and in another container I am running mysql, so when I go to localhost on the pc running those containers I can see what is in a database in mysql, but I've passed through some troubles, first, I am using mysqli_connect in my php code (index.php), but when I run it, it says that that function is not defined, after using some extensions, I finally could solve this, and now I get refused connection when using mysqli_connect, I've linked those containers but am still getting that response, so I wanted to know how could I link those so that mysql is listening to the container running php/apache, thanks for answering
Macgyver (1 year ago)
+steelherrera what are you trying to do?
Anis Hammoutene (1 year ago)
What if the containers are in a different hosts ?
Macgyver (1 year ago)
Then you would need to consider how the hosts would communicate. Honestly it should be no different, you would just issue http requests from the hosts IP's, then you might need a router to handle that traffic and proxy it to the containers.
Travis Runyard (1 year ago)
I wonder if there is a way for container to container communication over UNIX sockets instead of TCP. Even though IP is just fine, it adds the TCP stack overhead. That's why NGINX has the ability to proxy requests to its local unix sockets instead of 127.0.0.1 or localhost.
Macgyver (1 year ago)
I'll have to look into this, do you know if the sort of overhead incurred by TCP versus UNIX sockets is significant and if there would hypothetically be any addition drawbacks?
Revathy U (1 year ago)
I have a docker containers running in linux vm. I got a tomcat container with my war file. how can i communicate with my container from my local windows machine. My goal is to get the output in my windows browser. How can i do that? Please help me asap...
WD HUNTER TV (1 year ago)
putty IS cygwin... by and large.
Macgyver (1 year ago)
What do you mean by get output in chrome? You want to issue a get request from chrome and hit the docker containers? If so you will want to setup a web server such as nginx on the aws instance. Do you have the AWS host IP address? Can you hit that IP from your windows machine? A good windows terminal is Cygwin it emulates linux, if you download that we can try ping or cURL to determine this. Otherwise we can use a different driver.
Revathy U (1 year ago)
Thank you so much for your reply. It means a lot to me. I am connecting to the vm using putty from my windows desktop by being on the same network using VPN. I will first connect with one stepping vm which doesnt met the specifications for docker installation, from there i will connect to the vm AWS instance which is created for docker purpose. The containers are running in AWS instance (host), i am successfully able to get the output on the host. But i want to get the output in Chrome which is in my windows desktop and that's connected to the same network using proxy IP and VPN. I hope this is clear. Please reply even if you need any more clarification on this. Thank you in advance.
Macgyver (1 year ago)
Hey Revathy! So you have a linux virtual machine running on a windows computer - and within that linux vm you have running docker containers? This tutorial covers how you would interface from the linux VM (the host) and the docker containers running on that host. You are adding an additional step of having your windows OS talk to the virtual machine. This will depend on how you are running the VM, how are you running the linux virtual machine on windows? Essentially it should be the same process, the vm client should stand up the linux virtual machine as an IP address which will be exposed to the windows computer. I need you to clarify your browser will not be able to access any local data regardless that is a security issue. Can you elaborate on what you mean by "getting the output in my windows browser"? Do you mean windows OS?

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.