Python service to populate docker container IPs in /etc/hosts
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:
Run the install.sh
script as regular user. You'll be prompted when elevated privileges are necessary.
cd docker-Hosts
./install.sh
To run this service, you need a few dependencies.
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
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
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
After either of the installation methods you should be able to connect to each container via its name and the .test
TLD.
This script requires docker 1.12
as minimum.