From time to time I get asked if services in the
.drone.yml file can be referenced by their custom hostname, similar to docker compose. This is not currently possible because drone implements custom networking that prevents the use of custom docker hostnames. This article will therefore propose a simple workaround that maintainers Docker compose compatibility, but also works with Drone.
Example docker-compose file that will use
cache:6379 as the redis container's address where
cache is a custom hostname created by Docker:
Let's say we have a node application that connects to the redis client:
Instead of hardcoding the custom hostname, make it configurable:
var redis_url = process.env.REDIS_ADDR || 'cache:6379'
The above example defaults to the custom docker hostname, however, we can now override the default value in the
- npm install
- npm run test
The benefit here is that your code using default docker-compose links and hostnames, while also being able to change the service address at runtime with an environment variable.
As an added bonus, you now have a slightly more portable application since it doesn't rely on docker links and custom hostnames, should you ever decide you want to stop using docker.