Using /etc/hosts with docker

亡梦爱人 提交于 2019-12-08 15:13:29

You can use a jwilder/nginx-proxy, it's a reverse proxy auto-configured by the env vars of other containers, so you don't need to manually write nginx proxy configs. Also as requested, it allows to use specific port to forward requests to.

# docker-compose.yml

version: '3.3'

services:

  lamp:
    environment:
      VIRTUAL_HOST: some_domain.dev
      VIRTUAL_PORT: 9999
    image: my_lamp_image

  app:
    environment:
      VIRTUAL_HOST: another_domain.dev
      VIRTUAL_PORT: 3000
    image: my_app_image

  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
# /etc/hosts

127.0.0.1 some_domain.dev
127.0.0.1 another_domain.dev

jwilder/nginx-proxy has many more nice features like ssl, uwsgi, fastcgi and can also be used in production. There are also "companion" additions like let's encrypt ssl and man in the middle proxy.

It looks like your apache server runs on port 80 inside the container. If you want use dockertest.com outside with your /etc/hosts entry, than u have to use port 80 for outside also.

  1. make your /etc/hosts entry for the dockertest.com domain
  2. If you use docker, run it with -p 80:80or if you use docker-compose
ports:
  - "80:80"

One possibility is to set up all the applications in their separate containers and then connecting them via a docker network.

And in order to reach all of the containers I would suggest adding an nginx webserver container to the network as a reverse proxy, which you can then bind to port 80 of your machine.

You can then either define a location for every app separately ot define one generic location like

# sample.conf
server {
  listen 80 default_server;
  server_name ~ (?<docker_host_name>.+);
  location ~ {
    # for actual request forwarding
    proxy_pass                         http://$docker_host_name$1$is_args$args;
    # some stuff I figured out I have to use in order for service to work properly
    proxy_set_header                   Upgrade $http_upgrade;
    proxy_set_header                   Connection 'upgrade';
    proxy_http_version                 1.1;
    proxy_cache_bypass                 $http_upgrade;
    proxy_set_header Host              $host;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

This configuration either has to be placed inline in the original /etc/nginx/nginx.conf or in a separate file that is included inside the http config block.

After restarting the nginx service or container (depending on container setup) you should be able to reach all the services inside the docker network and all of the services should be able to communicate to each other without problems.

Of course you still have to keep the entries in the hosts file, so your computer knows that it has to process the request locally.

Edit:

The original config (probably) does not do what it is supposed to do. So, I came up with a newer version, which should get the job done:

# sample.conf
server {
  listen 80 default_server;
  location ~ {
    # for actual request forwarding
    proxy_pass                         http://$host$1$is_args$args;
    # some stuff I figured out I have to use in order for service to work properly
    proxy_set_header                   Upgrade $http_upgrade;
    proxy_set_header                   Connection 'upgrade';
    proxy_http_version                 1.1;
    proxy_cache_bypass                 $http_upgrade;
    proxy_set_header Host              $host;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

With this configuration, the nginx server will be listening to all incoming requests on port 80 and forward them to the proper container inside the network. You also do not have to configure the host resolution yourself, as docker container names also represent the host(-name) of a container.

Hopefully this works out for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!