Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker local environment custom local domains (hosts file?)

I'm a bit new to docker, but am searching this for quite a long time now.

I am using docker almost always with this container: https://hub.docker.com/_/wordpress/ , because most of my project are WordPress based.

The point is that every time I'm running this container, I'm running it on a localhost domain. Now at the company I'm working, we still using Virtual Machine. Here I have a 'homestead.yml' file, were I can add a custom domain and it's path. I also need to add this to my hosts file, and run a vagrant provision.

I don't want to use Virtual Machine on this Mac, because I like the speed of using Docker, but I do want the custom domains. For example; I work on a project called 'sunglasses', I want to create a local domain called 'sunglasses.local' for my local environment. But i can't seem to get it working...

My docker-compose file looks like this:

version: '3.1'
services:
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./wp-content:/var/www/html/wp-content
    # - /Users/username/dev/wordpress-foundation-boilerplate/wp-content:/var/www/html/wp-content
    # - /Users/username/dev/docker-wp-demo/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    environment:
      WORDPRESS_DB_NAME: database
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: p4ssw0rd!
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_DEBUG: 1
      WORDPRESS_CONFIG_EXTRA: | # Add config to wp-config.php
        define('FS_METHOD', 'direct');
        define('WP_DEBUG_LOG', true);
        define( 'WP_DEBUG', true );
    ports:
      - 8000:80
      - 443:443
    networks:
      - back
  db:
    image: mysql:5.7
    restart: unless-stopped
    volumes:
       - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: p4ssw0rd!
    networks:
      - back
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - 8080:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: p4ssw0rd!
    networks:
      - back
networks:
  back:
volumes:
  db_data:

I'm using Gasmask (see: http://clockwise.ee/) for editing my hosts file, and already added the domain I want to use with the right IP-adres.

Any idea what I'm missing? I don't now were to place the domain address in my docker-compose file. I have tried added it under 'ports' but this didn't even run my docker-compose file. I hope someone knows what I'm doing wrong.

like image 683
Loosie94 Avatar asked Jun 08 '19 12:06

Loosie94


1 Answers

At work we use dnsmasq to pass requests to TLDs that end in .docker to localhost. Here is how:

Requirements: homebrew and administration access

To forward .docker TLDs, install & configure Dnsmasq.

$ brew up && brew install dnsmasq
$ sudo mkdir -p /etc/resolver
$ echo 'nameserver 127.0.0.1' | sudo tee -a /etc/resolver/docker > /dev/null
$ echo 'address=/docker/127.0.0.1' | tee -a /usr/local/etc/dnsmasq.d/docker-tld.conf > /dev/null
$ sudo brew services start dnsmasq

Note: The resolver will start working after a reboot

Modify /usr/local/etc/dnsmasq.conf

    ...

listen-address=127.0.0.1

    ...

conf-dir=/usr/local/etc/dnsmasq.d/,*.conf

Test the DNS server

$ dig test.docker @127.0.0.1

; <<>> DiG 9.9.7-P3 <<>> test.docker @127.0.0.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40401
;; flags: qr aa rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;test.docker.           IN  A

;; ANSWER SECTION:
test.docker.        0   IN  A   127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Feb 08 16:24:12 CET 2018
;; MSG SIZE  rcvd: 45

Test the configuration

; Make sure your DNS is still working.
$ ping -c 1 www.google.com

PING www.google.com (216.58.206.4): 56 data bytes
64 bytes from 216.58.206.4: icmp_seq=0 ttl=53 time=26.789 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 26.789/26.789/26.789/0.000 ms

Check that .docker TLDs are working

$ ping -c 1 test.docker
  PING test.docker (127.0.0.1): 56 data bytes

Source: Passing Curiosity

like image 62
Tom Avatar answered Nov 08 '22 17:11

Tom