Closed fabiang closed 4 years ago
I was thinking something similar with Kubernetes too. Maybe it can also be applied to docker too.
Maybe we can create an image which mounts the host file and add the project IPs once it started? I think docker-compose can pass the project name into the container. Question is then, how to handle the project shutdown.
Gonna try to figure out some things now and gonna write my idea here.
I'm reading a bit about the way you can get docker
and docker-compose
container IPs. Maybe we can do a simple version that just read from docker and sync a profile with docker IPs.
The command I'm using to test is this:
docker ps -q | xargs docker inspect --format \
'{{ range .NetworkSettings.Networks }}{{ .IPAddress }} {{ $.Name }}{{ println }}{{ end }}'
# Output:
172.22.0.6 /worker_1
172.22.0.5 /vote_1
172.21.0.3 /vote_1 # two IPs means it belongs to two networks
172.22.0.3 /db
172.22.0.2 /redis
172.22.0.4 /result_1
172.21.0.2 /result_1
172.17.0.2 /toolkit
From Go is easy to read/parse that info (it can be exported as JSON as well), and setup a profile based from that info. I'll give it a try.
@fabiang this already implemented on v0.7.0
. You can sync with Docker and Docker Compose.
Give it a try and send any feedback. Thanks!
I've tested it and it works, when I assume that hostctl
is installed on my local system.
What I was trying as well, was to manipulate my hosts file once a project is started or stopped with docker-compose
.
First I made a Dockerfile and an entrypoint script, which you can find here: https://github.com/fabiang/hostctl/blob/docker-integration/Dockerfile https://github.com/fabiang/hostctl/blob/docker-integration/docker-entrypoint.sh
I've build the Docker image and added hostsctl as container to two of my projects in the docker-compose.yml
:
version: "2"
services:
# ...
hostctl:
image: guumaster/hostctl
environment:
- PROJECT_NAME=test
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "$HOME/.etchosts:/home/hostctl/.etchosts"
- "/etc/hosts:/etc/host-hosts"
The result in the /etc/hosts is then:
# profile test
172.90.4.4 foo_php_1.loc
172.90.4.2 foo_nginx_1.loc
172.90.4.5 foo_database_1.loc
# end
I can then open http://foo_nginx_1.loc and browse the page.
Of cause there some problems:
docker-compose stop
To make this automatically work when a project is started or stopped hostctl could do the following:
/var/run/docker.sock
docker-compose up -d
twice before all IPs are registered.To override the path to the hosts file and hostctl config for Windows the user must override the settings in docker-compose.yml
in a seconds file and execute docker-compose -f docker-compose.yml -f docker-compose.windows.yml
. Currently there is no other way to archive different mounts per operating system, other then this and an environment variable.
I just recently added a --wait
flag to do stuff and undo it when receives SIGTERM
signal.
For example:
hostctl sync docker -p awesome --wait 0
It will run until you send ctrl-c
(hence the zero, otherwise it will timeout with a duration flag)
UPDATE: Now is documented and you can read about here Guides > Ephemeral profiles
Let me know if the --wait
flag helps with what you want to do. And also I invite you to check #44 and write a small guide on your use case.
@fabiang I'm closing this. If you think there is something that can be improved, open a new issue. Thanks!
Any ideas how to integrate this with Docker?
I need something that recognizes when a container starts and add then a hostname to the hosts file with the ip of the container (maybe the project name from docker-compose). Would be fantastic to automate, so I don’t have to remember all the service names and there port redirections.