Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hostnames with underscore #472

Closed
bibendi opened this issue Sep 5, 2014 · 14 comments
Closed

Hostnames with underscore #472

bibendi opened this issue Sep 5, 2014 · 14 comments

Comments

@bibendi
Copy link

bibendi commented Sep 5, 2014

Hostnames with underscore fails in ruby URI validation

redis://redis_1:6379/4

 /usr/local/rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/uri/generic.rb:213:in `initialize': the scheme redis does not accept registry part: redis_1:6379 (or bad hostname?) (URI::InvalidURIError)
    /usr/local/rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/uri/common.rb:218:in `new'
    /usr/local/rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/uri/common.rb:218:in `parse'
    /usr/local/rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/uri/common.rb:747:in `parse'
    /usr/local/rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/uri/common.rb:994:in `URI'
    /usr/local/gems/gems/redis-3.0.7/lib/redis/client.rb:345:in `_parse_options'
    /usr/local/gems/gems/redis-3.0.7/lib/redis/client.rb:67:in `initialize'
    /usr/local/gems/gems/redis-3.0.7/lib/redis.rb:31:in `new'
    /usr/local/gems/gems/redis-3.0.7/lib/redis.rb:31:in `initialize'

http://stackoverflow.com/questions/2180465/can-someone-have-a-subdomain-hostname-with-an-underscore-in-it

@bibendi
Copy link
Author

bibendi commented Sep 5, 2014

This is a very critical bug for me. Could you make a additionally alternative hostname in the /etc/hosts in the form hostname-1, so as not to ruin backwards compatibility

@thaJeztah
Copy link
Member

I think the problem here is a combination of fig's conventions for the container- and link-names and Docker creating the host-entries. Docker will automatically add host-entries, based on the link-names, i.e. a link called db_1 will also be added as such in the host-file.

However, a PR has just been merged in fig that doesn't add _1 suffix (#364)

On a side-note; only the latest version of Docker (1.2.0) allows changing /etc/hosts in a running container (see moby/moby#5129) and those changes are not kept when a container is restarted.

As a workaround for now, you could run the latest version of Docker and use a shell script inside the container, that resolves the ip-address of a link (e.g. via the environment variable) and adds an entry to /etc/hosts on container-start. But keep in mind that restarting a linked container may change its IP-address.

@bibendi
Copy link
Author

bibendi commented Sep 5, 2014

Thanks
It seems that #364 is what I need.

@winmillwill
Copy link

That's not what #364 does though. It just changes a bunch of docs to refer to services by their service name instead of their link name.

The only change to a source code file used at runtime, which is fig/service.py, indicates that it's just falling back on the service name for the link name if the link name isn't initialized. Maybe I'm misunderstanding, but after installing like so:

pip install git+https://github.com/docker/fig.git@master#egg=fig

The names of the containers is unchanged (still suffixed with _1)

@rspeer
Copy link

rspeer commented Nov 17, 2014

I'm having this problem as well, using Django. It causes Django to fail in production only, and if you look for the log message, it looks like this:

django.security.DisallowedHost: Invalid HTTP_HOST header: 'proxy_1'. The domain name provided is not valid according to RFC 1034/1035.

@aanand
Copy link

aanand commented Nov 18, 2014

@rspeer A link alias is now added without a numeric suffix, so you should be able to just connect to proxy.

@rspeer
Copy link

rspeer commented Nov 18, 2014

Okay, that works.

It still does seem a bit silly to be generating invalid hostnames, though, encouraging code that seems right until it runs into an obscure failure mode of something that actually enforces RFCs. But I guess this is really just bug #229 now.

@thaJeztah
Copy link
Member

It still does seem a bit silly to be generating invalid hostnames, though,

If I'm not mistaken, the RFC your case refers to is outdated, so the hostnames aren't actually invalid (also see the link in the original description: http://stackoverflow.com/questions/2180465/can-someone-have-a-subdomain-hostname-with-an-underscore-in-it)

(I must say, I too was under the impression that underscores were not allowed)

@vandekeiser
Copy link

Does this mean that containers created with compose are unusable for web services? How am i supposed to configure the application that uses the web service (it validates the uri and fails)?
edit: As a workaround, i added this line for the configuration of the container hosting the ws in the yaml:
domainname: some.host
, and changed the client conf to use http://some.host/xxx

@dnephin
Copy link

dnephin commented Apr 9, 2015

You can use the name of the service in the docker-compose.yml as the hostname. It doesn't contain any underscores. You can also add an alias to your link to the container, which should allow you to access it as just the alias.

In the 1.3 release of compose there should be support for naming your container as anything you want, which will make this more obvious.

@JeanMertz
Copy link

I am not sure what the status of this issue is, but we're having problems with this as well.

Using the experimental build 1.8 (to use the new networking features), this is our current situation:

docker-compose.yml:

api:
  build: .
  ports:
    - 80

/etc/hosts:

cat /etc/hosts
172.17.42.23    dev_api_1

error:

Bad url URL: "http://dev_api_1/api.json"

@dnephin mentions the possibility of using simply api, but it is not listed in the /etc/hosts file (and subsequently does not work).

@thaJeztah mentions that _ characters are allowed in domains, which is true, but they are not allowed in hostnames, (valid: _static.my-app.com, invalid: _static.my_app.com).

@vandekeiser mentions using domainname, which I was not aware existed, so I'm going to try that now, but the default underscore hostname situation is still a pitty.

edit It doesn't look like either domainname or hostname has any impact on the contents of /etc/hosts for any but the current container. It did set hostname inside the container correctly, but that does not allow me to communicate with other containers using their respective hostnames.

@dnephin
Copy link

dnephin commented Sep 1, 2015

There's a lot going on in this issue.

Part of this is a duplicate of #229

@JeanMertz all linked containers should have a name without an underscore, and you can also alias the links (http://docs.docker.com/compose/yml/#links) if you need to. If this isn't working for you for some reason, please open a new issue.

@donoghuc
Copy link

donoghuc commented Apr 2, 2020

I hope this simple examples saves someone some googling.
Use case: I want a resolvable hostname (in my case without underscores) discoverable on a docker-compose network.
Here is the directory structure:

cas@cas-ThinkPad-T460p:~/why$ tree
.
├── docker-compose.yml
└── parent
    └── docker-compose.yml

1 directory, 2 files

In the parent directory I have the docker-compose network where I want the resolvable hostname without underscores:

version: '3'
services:
  bad_hostname:
    image: rastasheep/ubuntu-sshd:16.04
    hostname: goodhostname
    networks:
      default:
        aliases:
          - goodhostname
    ports:
      - "20024:22"
networks:
  default:

Note how I set the aliases under the network key. This will end up being the discoverable hostname!
Now I can attach to that network in a separate docker-compose file like:

version: '3'
services:
  outter:
    image: rastasheep/ubuntu-sshd:16.04
    ports:
      - "20023:22"
networks:
  default:
    external:
      name: parent_default

Finally if you attach to the outter container you can see that the hostname is resolvable by the alias you set.

cas@cas-ThinkPad-T460p:~/why$ docker exec -it why_outter_1 /bin/bash
root@d7dafbc45737:/# ssh root@goodhostname
The authenticity of host 'goodhostname (172.19.0.2)' can't be established.
ECDSA key fingerprint is SHA256:4utoWe5uVp79ImYqkPPukjmrGIfvDC5demc1sx8sY9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'goodhostname,172.19.0.2' (ECDSA) to the list of known hosts.
root@goodhostname's password: 
root@goodhostname:~# logout

@taseenb
Copy link

taseenb commented Dec 28, 2024

Hostnames with underscores may fail in many circumstances. They also fail with MongoDB/Mongoose.

Re #9618 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants