Skip to content

Instantly share code, notes, and snippets.

@agaro1121
Forked from DavidDeCoding/ExposeHostPort.md
Created July 19, 2017 14:42
Show Gist options
  • Save agaro1121/4a6679f2b74726ad818c69fbb2c97b38 to your computer and use it in GitHub Desktop.
Save agaro1121/4a6679f2b74726ad818c69fbb2c97b38 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