简体   繁体   中英

docker-compose add a hostname to a service

I have a backuppc local service running eg 127.0.0.1:8081. I can also reach it directly on http://172.23.0.4 (container ip)

docker-compose.yml

version: '3.7'
services:
  backuppc-app:
    image: tiredofit/backuppc
    container_name: backuppc-app
    ports:
      - "8081:80"
      - "8082:10050"
    environment:
      - BACKUPPC_UUID=1000
      - BACKUPPC_GUID=1000
    restart: always
    depends_on:
      - backuppc-mysql
    networks:
      - nginx-proxy

I want to assign it a hostname, something like

    hostname: backup.local

I tried to add it but doesn't work as expected

      backuppc-app:
        image: tiredofit/backuppc
        container_name: backuppc-app
        hostname: backup.local

Should I manually edit my local /ets/hosts?

    172.23.0.4    backup.local

You can add a hostname as a network alias:

version: '3.7'
services:
  backuppc-app:
    networks:
      nginx-proxy:
        aliases:
        - backup.local

For containers in nginx-proxy network it will be available both as backuppc-app and as backup.local .

If you want that hostname to be visible to your host you need to modify hosts file. But don't put container IP there - it can change. Rather add it as another name for localhost:

127.0.0.1 localhost myhostname backup.local

Then you can access it both with localhost:8081 and backup.local:8081 (that works due to port forwarding you've declared with ports: key).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM