Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I forward a docker-machine port to my host port on OSX?

Tags:

I’m delivering a private docker container in my company and want my colleagues to be able to access in our internal network, the problem is that my guest OS is OSX and as so I can only access my application using the 192.168.99.100:3000 default ip from docker machine.

How can I forward the docker-machine 3000 port to my host 80 port?

like image 976
Juliano Pacheco Avatar asked Mar 29 '16 13:03

Juliano Pacheco


People also ask

How do I map a Docker container port to a local port?

Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100. Map UDP port 80 in the container to port 8080 on the Docker host. Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

How do you expose a Docker container to a host?

You can expose a port through your Dockerfile or use --expose and then publish it with the -P flag. This will bind the exposed port to your Docker host on a random port (verified by running docker container ls ). You can expose a port through your Dockerfile or use --expose and then publish it with the -p 80:80 flag.

How do I run a Docker image on a specific port?

To publish a port for our container, we'll use the --publish flag ( -p for short) on the docker run command. The format of the --publish command is [host port]:[container port] . So, if we wanted to expose port 8000 inside the container to port 8080 outside the container, we would pass 8080:8000 to the --publish flag.


1 Answers

At this time Docker Machine is a virtual machine running under VirtualBox in your machine, so to expose your application port you need to map your virtual machine port to your host port.

To achieve this there are two options, but before make sure your Docker Machine is stopped running:

docker-machine stop default     # see PS below if docker machine isn't default

Option 1 - Use the VirtualBox interface

  • Open VirtualBox Manager
  • Select your Docker Machine VirtualBox image (e.g.: default)
  • Open Settings -> Network -> Advanced -> Port Forward
  • Add your app name, the desired host port (e.g.: 80) and your Guest port (e.g.: 3000)

Option 2 - Use the VirtualBox command line

Just run the following command with your own parameters:

VBoxManage modifyvm "dev" --natpf1 "myapp,tcp,,80,,3000"

Final considerations

Now you can start your Docker Machine running:

docker-machine start default
eval $(docker-machine env default)

Then just start your application Docker container and test it running http://localhost/.

P.S.: Your Docker Machine name may not be default, in this case change the name accordingly.

like image 103
Felipe Plets Avatar answered Oct 03 '22 18:10

Felipe Plets