Search code examples
nginxdockerdocker-composelamp

How to map a virtual host to a container in docker?


I am trying to setup a LEMP local development environment using Docker. Following is my docker-composer.yml:

version: '2'
services:
  apache:
    build:
      context: ./docker/apache-php7
      dockerfile: Dockerfile
    image: foo/apache-php7
    volumes:
      - .:/var/www/html
    ports:
      - "80:80"
      - "443:443"
    networks:
     - localnet
  node:
    build:
      context: ./docker/node
      dockerfile: Dockerfile
    image: foo/node
    volumes:
     - .:/var/www/html
    networks:
     - localnet
  mysql:
    image: mysql:5.7
    ports:
     - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_DATABASE: "root"
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "root"
    volumes:
     - mysqldata:/var/lib/mysql
    networks:
     - localnet
  redis:
    image: redis:alpine
    volumes:
     - redisdata:/data
    networks:
     - localnet
networks:
  localnet:
    driver: "bridge"
volumes:
  mysqldata:
    driver: "local"
  redisdata:
    driver: "local"

I can access the site by going to http://localhost. Instead of accessing the project using http://localhost, I am trying to map it to http://foo.dev but for the life of me I just can't figure out how I can achieve that? One example I have seen is this but it's using a different image and it is also using nginx.


Solution

  • I am assuming that the foo/apache-php7 image is forked from nimmis/apache-php7, which is based on Ubuntu 16.04, so by default your Apache will answer the same way to HTTP requests, irrespective of host name. So, the only thing you need to do is to set up A/AAAA records in the foo.dev DNS zone pointing at your server.

    It looks like the zone needs some attention in any case:

    % host foo.dev
    foo.dev has address 127.0.53.53
    foo.dev mail is handled by 10 your-dns-needs-immediate-attention.dev.
    

    The address record points to a localhost address, which won't work until you're actually on the same host. You'll need a globally routed IP address there.