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?
You can obtain the ip address of the container using docker inspect command with the filter:
Your command will be:
Also you can use static ip addresses for containers, declaring own networks.
My solution to this would be to run the restore commands in a container on the same network (or with a link). It sounds like you already have the folder information available so those can be passed to a temporary container as a volume.
Using the legacy Docker links it would look something like:
This would use the same image (assuming
mysql
andgunzip
binaries are available and there is noENTRYPOINT
) to mount the host volume to a temporary location in the container and then unzip from that temporary location to themysql
binary connecting to the link.You could probably avoid the legacy link by creating a custom Docker network bridge. In this case, the container names in that custom network should be available by DNS without the need for a link. But this would add additional parts to your recipe that may be more work than worth.