问题原文
原帖链接 · Tom Klino · CC BY-SA 3.0
I have a recipe that sets up a docker instance with a specific mysql image I made for my app. As part of that recipe I would like to have it so, that after the container comes up with mysql running, a procedure for restoring data from backup will begin automatically.
For that I need to have the IP of the container (or a way to get it from DNS). However, Since I bring the container up automatically from a recipe, I have no way of knowing what that IP will be, so I'm trying to have docker set it with a hostname that it can later resolve.
Oddly, if I link a container to another, I will be able to resolve the host of the other container from within the one linked to it. But, I have found no way resolving that hostname from the host.
Here is the part of the recipe (chef) I use to bring up the container:
docker_container 'imhere-mysql' do
hostname mysqlHost
repo 'lutraman/imhere'
tag 'mysql'
env ["MYSQL_ROOT_PASSWORD=#{mysql_password}"]
volumes [ '/var/imhere/mysql:/var/lib/mysql' ]
action :run
end
The command I would like to use to restore data is something like this (constructed from the recipe):
"gunzip -c #{parent_data_dir}/#{db_restore_filename} | mysql -h #{mysqlHost} -u root -p'#{mysql_password}'"
only that running from the host, the name in mysqlHost (in the case at hand - imhere-mysql) is not resolved from the host.
There is a note at the bottom of this article, saying there should be a docker dns server running on 127.0.0.11 - but I fear this is internal to containers.
What I'm hoping I'll be able to do, is query docker (in the ideal situation, as a DNS, but any way will work) for the ip of the container named 'imhere-mysql'. What's the best way to do this in a way that will hopefully hold for future versions on docker and regardless of the host OS?
已采纳解答
原帖链接 · Gnat · CC BY-SA 3.0
You can obtain the ip address of the container using docker inspect command with the filter:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container name>
Your command will be:
"gunzip -c #{parent_data_dir}/#{db_restore_filename} | mysql -h $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' #{mysqlHost}) -u root -p'#{mysql_password}'"
Also you can use static ip addresses for containers, declaring own networks.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
