kba-team / dockertools
Scripts to remove and add all running docker containers in your /etc/hosts file.
Installs: 15 404
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 2
Language:Shell
Type:tools
pkg:composer/kba-team/dockertools
README
docker-compose is a very useful tool, especially if you're programming inside a network of docker-containers. Accessing the (TCP) services of these docker containers, however is not so easy. To the best of my knowledge there are currently no tools available, that will add the IPs and hostnames of your running network to your hosts /etc/hosts file- until now.
Why not use <insert DNS/discovery service here>?
Running a DNS service for docker containers always messes with the DNS settings of your workstation. Especially if these DNS/discovery services themselves run inside docker containers on an active development workstation. Resolving DNS issues is not an easy task. Contrary to that the /etc/hosts file is simple. It couldn't care less if your workstation switches domains, DNS services, VPNs, etc. on-the-fly. In case something goes wrong, you can always edit the file manually. It was made for adding the hosts of a docker network (joke). I like the /etc/hosts file (not a joke).
Add to your PHP project
composer require kba-team/dockertools --dev
Add entries to hosts
In a bridged docker network, remove and add the IP addresses and hostnames of containers created by docker-compose in the /etc/hosts file. This will save you the hassle of port-forwarding, name resolvers, etc.
The script first removes all existing entries of all the hostnames of containers created by docker-compose in the /etc/hosts file and then adds them. This ensures, that there a no duplicate entries in the /etc/hosts file.
It is implied, that this script can only be called while docker containers are running.
Usage: docker_hosts.sh [-h] [-f <hosts-file>] [-r]
-hDisplay usage information.-f <hosts-file>Use a different hosts file than/etc/hosts.-rOnly remove all the hostnames of containers created bydocker-compose, but do not add them.
Examples:
vendor/bin/docker_hosts.sh: Remove and then add all docker containers in the/etc/hostsfile.vendor/bin/docker_hosts.sh -r: Just remove all docker containers from the/etc/hostsfile.
Environment variables
DOCKER_REGISTRYInstead of usingbusybox:latestto manipulate the hosts file,${DOCKER_REGISTRY}/busyboxwill be used. This is useful, in case you manually cache docker images in a local registry for faster access.
Automate docker-compose
In order to automate commands that need execution after docker-compose did its work, use bin/docker.sh.
Usage: vendor/bin/docker.sh <command>
Available commands are:
add-hostsAdd container hostnames and IPs to hosts file.remove-hostsRemove container hostnames and IPs from hosts file.execExecute command(s) in docker container as${DEFAULT_EXEC_USER:-www-data:www-data}. Available options:-u|--userRun commands using user ID$(id -u)and group ID$(id -g).-c|--container <name>Run commands in the specified container. Default container:${DEFAULT_CONTAINER:-web}
migrateRun the migrations command${MIGRATE_COMMAND}inside the default container${DEFAULT_CONTAINER:-web}.seedRun the seed command${MIGRATE_COMMAND}inside the default container${DEFAULT_CONTAINER:-web}.startTry to pull the current versions of the images, start the containers and add hostnames and IPs to the hosts file.upRun start, migrate and seed commands.stopRemove container hostnames and IPs from hosts file and stop the docker containers without destroying them.downRemove container hostnames and IPs from hosts file and destroy the docker containers.restartRun down and up commands.cleanupDelete files owned by${DEFAULT_EXEC_USER}. Available options:--rootDelete files owned byroot.
behatRun behat tests. Any additional parameters will be added to the behat command.phpunitRun phpunit tests. Any additional parameters will be added to the phpunit command.helpShow help.
Environment variables
Configure the environment variables in your .env file. That way it can be used by docker-compose as well (DRY).
DEFAULT_EXEC_USERThe default user executing commands inside docker containers.DEFAULT_CONTAINERThe default container to execute commands in.MIGRATE_COMMANDThe command running the migrations to set up the DB structure. In case this variable is not defined, nothing happens.SEED_COMMANDThe command adding seed data to your empty DB. In case this variable is not defined, nothing happens.PHPUNIT_BINThe path to the PHPunit binary. In case this variable is not defined, nothing happens.BEHAT_BINThe path to the Behat binary. In case this variable is not defined, nothing happens.