2

This is kind of a duplicate question to this one. . But ironically, in spite of that question's title, all the answers say: Use an ENV variable.

My use case: I don't know if docker is running via docker-compose or swarm. However, it will not be docker run! I am trying to kick off an upgrade script that resides on the host. Thus, from within a container I need the docker host name. Is there any programmatic way to get this WITHOUT environment variables?

JoeG
  • 7,191
  • 10
  • 60
  • 105

3 Answers3

4

Here is the feature request that has been implemented in 17.10 https://github.com/moby/moby/issues/30966

This is how you do it:

$ hostname testing123

$ docker service create \
  --name foo \
  --hostname "{{.Service.Name}}-{{.Task.Slot}}-{{.Node.Hostname}}" \
  nginx:alpine

 $ docker inspect foo.1.k51r3eclvcelsxy6jthtavkwa --format 
   '{{.Config.Hostname}}'
   foo-1-testing123

This should also work in docker-compose

services:
  nginx:
    image: nginx
    hostname: '{{.Node.Hostname}}'
Ich
  • 1,350
  • 16
  • 27
0

No. The goal of docker is to isolate the host environment as much as possible. The only way you could get the hostname is if you were to pass it into the container as a variable (or bind-mount a file containing the hostname, etc).

I don't know if docker is running via docker-compose or swarm. However, it will not be docker run! I am trying to kick off an upgrade script that resides on the host.

Why do you care how the container was started? How do you plan to run a script on the host? The answers to these questions might help us provide a better answer to your original question.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • By "I don't know if docker is running... or swarm", I meant that I don't care "how" the container is started. If you know the BEST way to run a remote script, please let me know; however, with ANY such mechanism I know I need the name/IP-address of that host, thus my question – JoeG Aug 07 '17 at 18:14
0

If you're using Docker for Mac, you can use the following hostname:

docker.for.mac.localhost

Jacob Thomason
  • 3,062
  • 2
  • 17
  • 21