Skip to content

Instantly share code, notes, and snippets.

@DavidDeCoding
Last active July 19, 2017 14:42
Show Gist options
  • Save DavidDeCoding/45e2689c64c4e52404c35528026af7b8 to your computer and use it in GitHub Desktop.
Save DavidDeCoding/45e2689c64c4e52404c35528026af7b8 to your computer and use it in GitHub Desktop.
Exposing port on host machine to docker container

The vice versa can easily be done by including -p flag. But the way to expose ports of host machine to a docker container is bit convoluted, but can be done.

The steps are:

  1. Create an alias for the ip you want to use.
sudo ifconfig lo0 alias 172.16.123.1
  1. add the flag to docker:
docker run <image> --add-host=docker.local:172.16.123.1

Or

add it in docker-compose as:

services:
  service1:
    extra_hosts:
      - "docker.local:172.16.123.1"
  1. Use docker.local as host in the container.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment