Skip to content

Python service to populate docker container IPs in /etc/hosts

License

Notifications You must be signed in to change notification settings

toaomatis/docker-hosts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker Hosts

Python service to populate docker container IPs in /etc/hosts

Source code

Clone the repository to a directory of your choice. As long as it is at a non-volatile location.

git clone https://gitlab.huntfield35.nl/docker/docker-hosts.git

There are two ways to install and configure this service:

Installation using the install.sh script

Run the install.sh script as regular user. You'll be prompted when elevated privileges are necessary.

cd docker-Hosts
./install.sh

Manual Installation

To run this service, you need a few dependencies.

Dependencies

python3 pip virtualenv

sudo apt install python3 python3-pip python3-venv

Navigate to the source directory and create a virtual environment

cd docker-Hosts
python3 -m venv ./env

Source the virtual environment and install the python3 docker package

source env/bin/activate
pip install docker
# exit the virtual environment
deactivate

Configuration

Due to the way python and Linux' services work, we need to manually adapt some hard coded full paths.

This can be done by executing the following shell script from the source root

cp docker-hosts.py.dist docker-hosts.py
cp docker-hosts.service.dist docker-hosts.service
sed -i -e "s|%REPLACE_ME%|$(pwd)|g" docker-hosts.py docker-hosts.service

Install the service as a super user, start and enable it so it runs every system (re)start.

sudo su
cp docker-hosts.service /etc/systemd/system
systemctl start docker-hosts.service
systemctl enable docker-hosts.service

Verification

First check if the service is running

systemctl status docker-hosts.service

If there are any Docker containers running, they should be added to /etc/hosts file

cat /etc/hosts

There should be a list with the IP addresses of the running Docker containers between two comment lines

# Docker hosts start
172.19.0.6 webserver.test
172.19.0.5 php-fpm.test
172.19.0.4 mariadb.test
172.19.0.3 redis.test
172.19.0.2 mongo.test
# Docker hosts end

Success!

After either of the installation methods you should be able to connect to each container via its name and the .test TLD. Success Kid

Remark

This script requires docker 1.12 as minimum.

About

Python service to populate docker container IPs in /etc/hosts

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published