I have a following docker-compose file:
nginx:
image: nginx
hostname: myservice.local
volumes:
- nginx.local.conf:/etc/nginx/conf.d/myservice.conf
container_name: goout-nginx
Is there a way for docker-compose to set the myservice.local for the host machine as well? At the moment calling this on the host machine doesn't do anything:
$ ping myservice.local
ping: cannot resolve myservice.local: Unknown host
I don't want to manually update local /etc/hosts
for such thing.
EDIT:
My problem seems to be more complex then I previously thought. I am trying to create a development setup for my colleagues which they could run locally on their machines.
This should be an easily-runnable application running on 80/443
ports in the browser. This cannot run on the localhost:80/443
as these ports are already taken and used by apache on their machines. To do that I want the running machine to have assigned IP address so they could just interact with the application just by opening myservice.local
. This doesn't have to be accessible to the outside world, only the host machine.
The setup should be super simple - I was hoping all this could be achieved just by running docker-compose up
– I was hoping to avoid any complicated setup of networking, IP tables or /etc/hosts.
Basic idea is to use docker inspect to obtain the pid of the container, then enter the uts namespace of the container via nsenter . Running hostname inside that namespace will change the hostname for the docker instance that shares that namespace.
Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.
On Docker for Linux, the IP address of the gateway between the Docker host and the bridge network is 172.17. 0.1 if you are using default networking. Do you see the problem already? They are different, so you cannot simply run docker-compose up -d and all operating systems behave the same.
This will be a little convoluted given your requirements, but it should work.
The objective here is to modify the /etc/hosts on the host machine without needing manual intervention from the running users. The non-functional requirement is that it shouldn't impact your application when it's running non-locally (I.e., no changes to the nginx or backing containers running your application). The changes should be limited to the docker-compose file and utility scripts.
To that end, I'd recommend one of two things:
grep -q -F '<static_ip> myservice.local' /etc/hosts || echo '<static_ip> myservice.local' >> /etc/hosts
then starts up docker compose using docker-compose up -d
. The script is idempotent, so it should not add the line a second time if grep finds the line in /etc/hosts. This requires you to assign a static IP to the nginx container, but given you can do that within docker-compose, and it won't conflict with your host, that should meet your requirements. Name the file "startMyApp.sh" so it's clear what it does, and your non-tech savvy folks should gravitate to using that instead of the docker-compose syntax anyway.OR
Note that in both of these cases I'm assuming port 80/443 is exposed from your webserver container. You'll need to do that unless you're running a proxy to remap them.
Sample running:
version: '2'
services:
tomcat:
image: tomcat
ports:
- "1234:8080"
hostname: myservice.local
networks:
my_network:
ipv4_address: 10.5.0.5
networks:
my_network:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1
grep -q -F "myservice.local 10.5.0.5" || sudo echo "myservice.local 10.5.0.5" >> /etc/hosts
docker-compose up -d
open a web browser and browse to "myservice.local:8080" and you can see apache tomcat's base page there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With