title: Using /etc/hosts with docker
body: <p>On my Mac I use vagrant with Ubuntu and apache running on it, and have virtual host entries for my various code repositories for apache. In the OSX side of things I create /etc/hosts entries for each of those v-host entries.</p>
  
  <p>I'm trying to achieve the same effect with docker, but I'm struggling to figure it out without having to specify the port number when accessing the app, which I don't want to do. Ex: I have <code>127.0.0.1 dockertest.com</code> in my /etc/hosts, which I can then access at <code>http://dockertest.com:8080</code>. I'd like to be able to just go to <code>http://dockertest.com</code> without specifying the port. How can I achieve this? I know port numbers can't be used in the /etc/hosts file, so I'm looking for a way that would mimic the effect if it was possible. I need to be able to run multiple docker apps at the same time because some of the codebases communicate with one another and each need to have their own unique hostname, so I don't think simply setting ports to <code>80:80</code> in the docker-compose file will work because every app will be (attempting) to run on <code>127.0.0.1:80</code>.</p>
  
  <p>For context I've followed <a href="https://www.cloudreach.com/blog/containerize-this-php-apache-mysql-within-docker-containers/" rel="nofollow noreferrer">this tutorial</a> for running apache, php and mysql on docker. All of my files are exactly as shown on that site. </p>
  
  <p><strong>Update</strong></p>
  
  <p>I'm getting a <code>502 Bad Gateway</code> nginx error with the following <code>docker-compose.yml</code> file.</p>
  
  <pre><code>version: "3.3"
  services:
    php:
      build: './php/'
      networks:
        - backend
      volumes:
        - ./public_html/:/var/www/html/
    apache:
      build: './apache/'
      depends_on:
        - php
        - mysql
      networks:
        - frontend
        - backend
      volumes:
        - ./public_html/:/var/www/html/
      environment:
        - VIRTUAL_PORT=3000
        - VIRTUAL_HOST=dockertest.com
    mysql:
      image: mysql:5.6.40
      networks:
        - backend
      environment:
        - MYSQL_ROOT_PASSWORD=rootpassword
    nginx-proxy:
      image: jwilder/nginx-proxy
      ports:
        - 80:80
      volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
  networks:
    frontend:
    backend:
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>Resolved the '502 Bad Gateway' error, here's the updated <code>docker-compose.yml</code> file. I had to add the nginx-proxy to one of the networks I referenced. My question isn't completely resolved, but I do have part of it working. For anyone reading this looking for a solution, I created another question <a href="https://stackoverflow.com/questions/55446765/running-multiple-docker-compose-files-with-nginx-reverse-proxy">here</a> to prevent this one from getting too long.</p>
  
  <pre><code>version: "3.3"
  services:
    php:
      build: './php/'
      networks:
        - backend
      volumes:
        - ./public_html/:/var/www/html/
    apache:
      build: './apache/'
      depends_on:
        - php
        - mysql
      networks:
        - frontend
        - backend
      volumes:
        - ./public_html/:/var/www/html/
      environment:
        - VIRTUAL_HOST=dockertest.com
    mysql:
      image: mysql:5.6.40
      networks:
        - backend
      environment:
        - MYSQL_ROOT_PASSWORD=rootpassword
    nginx-proxy:
      image: jwilder/nginx-proxy
      networks:
        - backend
      ports:
        - 80:80
      volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
  networks:
    frontend:
    backend:
  </code></pre>