Docker Community Forums

Share and learn in the Docker community.

Unable to create virtual host

Hi,

I’m new to Docker. Currently I have a working WordPress container in localhost:8000, but I’m trying to create a virtual host for it, so I will be able to access it by http://starter.dev, and nothing works. I have edited my docker-compose.yml file according to the docs, but still no luck. Nothing is added to hosts file, and if I manually add it, I cannot reach the site at all.

This is how my docker-compose.yml file looks like:

version: '3'

services:
  # Database
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password 
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    extra_hosts:
      - "starter.dev:127.0.0.1"
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:

Your extra_hosts setting actualy works, exec into the container and curl the domain… you will see that it works. But this setting will have no effect on your host.

Thus said, I am pretty sure this is not what you want… You want the domain starter.dev to be resolvable from your host, don’t you?

If so, you will either need to edit the c:\Windows\System32\drivers\etc\hosts file (which is the windows equivalent to Linux /etc/hosts) OR preferably add a dns server to your network that manges the name resolution for you in a proper way.

In home lab environment I tend to use pihole for that, even though its primary functionality is to filter domains based on lists, one of the secondary functions is to manage arbitrary domains and register A, AAAA or CNAME records for those within your network. Pihole makes it realy easy to do that.

You should consider to solve the missing name resolution on the level it emerges: on the dns level.

Hi,

I have already tried to add this to hosts file like you said, but still the domain is unreachable.
This is what I have added:

::1 starter.dev
127.0.0.1 starter.dev
::1 www.starter.dev
127.0.0.1 www.starter.dev

I’m not sure how to use Pihole. It should be in the docker-compose.yml file? I can’t file an exact demo settings for that.

Thanks!

You did edit the windows host file and add those entries, right?
What happens if you ping those domains in windows?

I am sure a short google search will yield dozens of blog posts that tall you how it’s done, BUT: once setup, you will need to loop it into your network configuration. Either on the host, OR configure as the dns server your dhcp server announces OR as primary dns server for wan connection. As this will affect your network name resolution, I strongly recommend to NOT run Pihole on a desktop computer. If you run a docker capable NAS, a pihole, or another 24/7 running server → run it there.

If you feel overwhelmed by the idea of running your own pihole, then probably the solution is unsuited for you, and you might stick to manipulating the hosts file.

Never the less, docker is not responsible for your network name resolution by itself. You could choose to run phole in docker, or directly on your host, or on a raspberry pi, but that would be custom solution introduced by yourself, and nothing that docker is responsible of.

If I try to ping I get this: https://i.imgur.com/XbzwQht.png
But in the browser, the site is unreachable: https://i.imgur.com/Hlayh7l.png

This indicates that it’s working. So nameresolution is not your problem (anymore).

According the details you shared and the published port for the wordpress service, this URL should work in the browser: http://www.starter.dev:8000
If not, check the logs of the containers,