If you expose port 80 on all your containers using web applications, you can link to your Nginx container.
Here is a small docker-compose.yml :
app: build: app/ expose: - "80" api: build: api/ expose: - "80" webserver: image: nginx ports: - "80:80" volumes: - default.conf:/etc/nginx/conf.d/default.conf links: - app - api
And here is the configuration for Nginx:
server { listen 80; server_name localhost; location / { proxy_pass http://app; } location /api { proxy_pass http://api; } } upstream app { server app:80; } upstream api { server api:80; }
When binding the container to another container (in this case, Nginx), Docker modifies the /etc/hosts and adds lines like this:
172.17.0.7 api 165637cfd4ab yourproject_api_1
172.17.0.5 app dedf870dec53 yourproject_app_1
So, Nginx knows about api and app containers.
When the request arrives at "localhost: 80", Nginx will proxy it to http://app:80 . hosts resolves the htt://app request to 172.17.0.7 , and therefore this request is forwarded to the application container.
Launch all containers using $ docker-compose up , and you're done.
source share