k0pernikus
k0pernikus

Reputation: 66500

How to setup hostnames using docker-compose?

I have setup a few docker-containers with docker-compose.

When I start them via docker-compose up I can access them via their exposed ports, e.g. localhost:9080 and localhost:9180.

I really would like to access them via hostnames, the localhost:9180 should be accessable on my localhost via api.local and the localhost:9080 via webservice.local

How can I achieve that? Is that something that docker-compose can do or do I have to use a reverse proxy on my localhost?

Currently my docker-compose.yml looks like this:

api:
    build: .
    ports:
        - "9180:80"
        - "9543:443"
    external_links:
        - mysql_mysql_1:mysql
    links:
        - booking-api

webservice:
    ports:
        - "9080:80"
        - "9443:433"
    image: registry.foo.bar:5000/webservice:latest
    volumes:
        - ~/.docker-history:/.bash_history
        - ~/.docker-bashrc:/.bashrc
        - ./:/var/www/virtual/webservice/current

Upvotes: 3

Views: 6298

Answers (2)

Freedom_Ben
Freedom_Ben

Reputation: 11933

You should check out the dory project. By adding a VIRTUAL_HOST environment variable, the container becomes accessible by domain name. For example, if you set VIRTUAL_HOST=web.docker, you can reach the container at http://web.docker.

The project home page has more info. It's a young project but under active development. Support for macOS is also planned now that Docker for Mac and dlite have emerged/matured.

Upvotes: 1

nessuno
nessuno

Reputation: 27042

No, you can't do this.

/etc/hosts file resolves host-names only. Thus it can only resolve localhost to 127.0.0.1.

If you add a line like api.local 127.0.0.1:9180 it wont work.

The only think you can do is to setup a reverse proxy (like nginx) on your host that listen to api.local and forwards the requests to localhost:9180.

Upvotes: 2

Related Questions