Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker integration #1

Closed
fabiang opened this issue Mar 15, 2020 · 7 comments
Closed

Docker integration #1

fabiang opened this issue Mar 15, 2020 · 7 comments
Labels
feature New feature or request

Comments

@fabiang
Copy link

fabiang commented Mar 15, 2020

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.

@guumaster
Copy link
Owner

I was thinking something similar with Kubernetes too. Maybe it can also be applied to docker too.

@guumaster guumaster added the feature New feature or request label Mar 15, 2020
@fabiang
Copy link
Author

fabiang commented Mar 15, 2020

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.

@guumaster
Copy link
Owner

guumaster commented Mar 30, 2020

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.

@guumaster
Copy link
Owner

@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!

@fabiang
Copy link
Author

fabiang commented Apr 9, 2020

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:https://foo_nginx_1.loc and browse the page.

Of cause there some problems:

  1. This works only under Linux/Unix, because of the paths are volumes
  2. The profile isn't removed from /etc/hosts one the project is shut down with docker-compose stop

To make this automatically work when a project is started or stopped hostctl could do the following:

  1. Deamonize and scan the network for IP and add them as hosts for each container found on the network. This would allow removing the the mount for /var/run/docker.sock
  2. Remove the profile when the daemon receives SIGTERM
  3. Add a small duration before scanning the network, so the containers are up. Currently I've to run 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.

@guumaster
Copy link
Owner

guumaster commented Apr 9, 2020

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.

@guumaster guumaster reopened this Apr 9, 2020
@guumaster
Copy link
Owner

@fabiang I'm closing this. If you think there is something that can be improved, open a new issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants