DEV Community

Raunak Jain
Raunak Jain

Posted on

How to do Port forwarding in docker-machine?

When you work with Docker, you sometimes need to use docker-machine. Docker-machine is a tool that helps you create and manage Docker hosts. Many times, these hosts run in a virtual machine. In such cases, you may not be able to reach your containers easily from your main computer. This is where port forwarding comes in. In this article, we will learn how to set up port forwarding in docker-machine. I try to use simple words and short sentences so it is easy to follow.


Introduction

Docker-machine makes it simple to run Docker on many environments. It creates a virtual machine on your computer or in the cloud. However, when you use a virtual machine, the ports used by your containers are not automatically open on your host computer. To access your containers from your host, you need to forward ports from the virtual machine to your computer. This process is called port forwarding.

Port forwarding helps you send traffic from a port on your host machine to a port on the virtual machine. This allows you to test your applications in a container and access them as if they were running on your computer. For more details on how container ports work, you can read about understanding Docker container ports.


What is docker-machine?

Docker-machine is a tool that creates and manages Docker hosts. It is useful when you want to run Docker on a local virtual machine or on cloud platforms. When you run docker-machine, it creates a virtual machine using a driver like VirtualBox. Once the machine is ready, you can use it to run your Docker containers.

Because docker-machine creates a separate virtual machine, the IP address of this machine is different from your computer’s IP address. To access services running inside the machine, you must know its IP address. You can get this by running:

docker-machine ip default
Enter fullscreen mode Exit fullscreen mode

Here, "default" is the name of your docker-machine. This command shows the IP address of your Docker host.

If you want to learn more about Docker and why you should use it, check out what is Docker and why you should use it.


What is Port Forwarding?

Port forwarding is a method to redirect network traffic. In our case, it means sending traffic from a port on your host machine to a port on the virtual machine created by docker-machine. By doing this, you can access the container’s application from your host computer. For example, if a web server runs on port 8080 inside the container, you can forward this port so that it is available on your host machine as well.

This idea is very similar to exposing ports in Docker containers. However, with docker-machine, an extra step is needed because of the virtual machine layer. You have to set up rules to forward the traffic correctly.


How Port Forwarding Works in docker-machine

When docker-machine creates a virtual machine, it usually sets up a Network Address Translation (NAT) connection. NAT hides the virtual machine behind a private network. With NAT, the ports used by containers are not automatically accessible from the host computer.

To allow communication, you must create port forwarding rules. These rules tell the virtual machine to listen on a specific port on its host interface and then pass the traffic to a specific port on the container. For example, you may want to forward port 8080 on your computer to port 8080 in a container running inside the virtual machine.

A good explanation on exposing container ports to the host can help you understand why this step is necessary. It shows that without port forwarding, the service in the container remains hidden.


Setting Up Port Forwarding in VirtualBox

Most docker-machine users use VirtualBox as the virtual machine driver. In VirtualBox, you can set up port forwarding using the VirtualBox command-line tool called VBoxManage or by using the VirtualBox graphical user interface (GUI).

Using VBoxManage

If you are comfortable with the command line, you can use VBoxManage. First, find the name of your docker-machine. It is often "default" unless you have changed it. Then run a command like this:

VBoxManage modifyvm "default" --natpf1 "tcp-port8080,tcp,,8080,,8080"
Enter fullscreen mode Exit fullscreen mode

Let’s break down the command:

  • VBoxManage modifyvm "default" tells VirtualBox to modify the settings of the virtual machine named "default".
  • --natpf1 "tcp-port8080,tcp,,8080,,8080" sets a NAT port forwarding rule.
    • The rule is named "tcp-port8080".
    • The first "tcp" indicates the protocol.
    • The first empty field means the host IP is not specified (it listens on all interfaces).
    • The second "8080" is the port on the host.
    • The next empty field means the guest IP is not specified.
    • The final "8080" is the port on the virtual machine.

This command forwards traffic from port 8080 on your host computer to port 8080 on the docker-machine virtual machine.

Using VirtualBox GUI

If you prefer a graphical interface:

  1. Open VirtualBox.
  2. Select your docker-machine virtual machine (often named "default").
  3. Go to Settings > Network.
  4. Click on Advanced and then Port Forwarding.
  5. Add a new rule with the following details:
    • Name: tcp-port8080
    • Protocol: TCP
    • Host IP: Leave empty or 127.0.0.1
    • Host Port: 8080
    • Guest IP: Leave empty
    • Guest Port: 8080

This does the same as the VBoxManage command and forwards port 8080.

For more details on exposing ports, you may also refer to exposing ports in Docker containers.


Running a Container with Port Forwarding

After setting up port forwarding on your virtual machine, you can run a container in docker-machine as usual. For example, if you want to run a web server on port 8080, you can use:

docker run -d -p 8080:80 --name my_webserver nginx
Enter fullscreen mode Exit fullscreen mode

This command does the following:

  • -d runs the container in detached mode.
  • -p 8080:80 maps port 8080 on the virtual machine to port 80 inside the container.
  • --name my_webserver gives the container a name.
  • nginx is the image used (running a web server).

Now, with port forwarding set up in VirtualBox, you can access the web server from your host computer by going to http://localhost:8080. The request goes to your host computer, then is forwarded by VirtualBox to the docker-machine virtual machine, and finally reaches the container on port 80.

A helpful article on exposing container ports to the host explains this mapping in more detail.


Troubleshooting Port Forwarding Issues

Even when you follow the steps, you might face some problems. Here are a few common issues and their fixes:

1. Incorrect Port Numbers

Make sure you use the same port numbers in your port forwarding rule and in your docker run command. If you forward host port 8080, then your container should be mapped accordingly.

2. Virtual Machine Not Running

Before accessing your service, ensure that your docker-machine is running. Use the command:

docker-machine ls
Enter fullscreen mode Exit fullscreen mode

This shows the status of your machines. If your machine is not running, start it with:

docker-machine start default
Enter fullscreen mode Exit fullscreen mode

3. Firewall Settings

Your host computer’s firewall may block the forwarded port. Ensure that port 8080 is allowed. You may need to adjust your firewall settings.

4. Conflicting Applications

If another application is using port 8080 on your host, you may need to change the host port in your forwarding rule. For example, you can set the rule to forward port 9090 instead:

VBoxManage modifyvm "default" --natpf1 "tcp-port9090,tcp,,9090,,8080"
Enter fullscreen mode Exit fullscreen mode

And run the container with:

docker run -d -p 8080:80 --name my_webserver nginx
Enter fullscreen mode Exit fullscreen mode

Now, access the service at http://localhost:9090.


Best Practices for Port Forwarding in docker-machine

Here are some tips to make port forwarding work smoothly in docker-machine:

  • Use Consistent Port Numbers:

    Always use the same port numbers in your Docker run commands and in your VirtualBox settings. This helps avoid confusion.

  • Document Your Settings:

    Write down the port forwarding rules you create. It will help you remember which ports are forwarded if you need to update your configuration later.

  • Check Docker-Machine Status Regularly:

    Use docker-machine ls to ensure your virtual machine is running properly.

  • Test Access Often:

    After setting up port forwarding, test your application by accessing the forwarded port in your web browser. This confirms that the rules are correct.

  • Learn More About Container Ports:

    Understanding how container ports work can help you troubleshoot. A good resource is the article on exposing ports in Docker containers.

For more advanced setups, you can also look at using Docker with cloud environments. This article explains how Docker can work with cloud providers and may give you ideas if you want to move beyond a local VirtualBox setup.


Example Scenario

Let’s walk through a real-world example. Imagine you are developing a web application that runs inside a Docker container. You use docker-machine with VirtualBox on your laptop. You want to see your application in a web browser on your host computer.

Step 1: Create and Start Docker-Machine

If you do not already have a docker-machine, create one using:

docker-machine create --driver virtualbox default
Enter fullscreen mode Exit fullscreen mode

Then, start it:

docker-machine start default
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Port Forwarding

Using VirtualBox’s command line, run:

VBoxManage modifyvm "default" --natpf1 "tcp-web,tcp,,8080,,8080"
Enter fullscreen mode Exit fullscreen mode

This rule forwards traffic from port 8080 on your host to port 8080 on the docker-machine.

Step 3: Run Your Web Application Container

Run your container with port mapping:

docker run -d -p 8080:80 --name my_web_app nginx
Enter fullscreen mode Exit fullscreen mode

This command maps port 8080 on the docker-machine (forwarded from your host) to port 80 in the container.

Step 4: Access Your Application

Open your web browser and go to:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

Your request goes to your host computer, is forwarded to the docker-machine virtual machine, and finally reaches the container running the Nginx web server. You should see the default Nginx welcome page.

This scenario shows how port forwarding makes it possible to work with docker-machine easily. For a deeper understanding of how container ports are set up, check out the guide on exposing container ports to the host.


Additional Considerations

When you set up port forwarding in docker-machine, keep the following in mind:

  • Driver Differences:

    While VirtualBox is common, other drivers may have different ways of handling port forwarding. Always check the documentation for your chosen driver.

  • Multiple Forwarding Rules:

    You can forward more than one port. Repeat the process with different port numbers for each service you want to access.

  • Use of Docker CLI:

    Working with docker-machine and port forwarding can be managed via the Docker CLI. Learning more about how to work with containers using the Docker CLI can be helpful. For more on this topic, you might want to review articles on working with Docker containers using the Docker CLI.

  • Security Considerations:

    Be careful when exposing ports to the public. Ensure that only the necessary ports are forwarded and that your firewall settings are correctly configured.

  • Testing Your Configuration:

    After making changes, always test your configuration by accessing your application. Simple tools like curl can help you check connectivity from the command line.


Conclusion

Port forwarding in docker-machine is an important skill for anyone using Docker with virtual machines. By setting up port forwarding, you can access your containerized applications easily from your host computer. We learned that docker-machine creates a virtual machine where Docker runs. Because the virtual machine has its own IP address, you must forward ports so that services inside containers are reachable.

We also learned how to set up port forwarding using VirtualBox. You can use the VBoxManage command line tool or the VirtualBox GUI to create rules that forward traffic from your host machine to the docker-machine. In our example, we forwarded port 8080 and ran an Nginx container to demonstrate how it works.

Remember to use consistent port numbers and test your setup often. Good documentation and regular checks with commands like docker-machine ls will help you maintain a reliable environment. For further reading on container networking, consider exploring the topic of exposing ports in Docker containers.

If you want to know more about Docker and its features, you might also be interested in reading about what is Docker and why you should use it. And for those who work with cloud services, the guide on using Docker with cloud environments can provide additional insights.

I hope this guide helps you understand how to do port forwarding in docker-machine. Keep practicing and testing your configuration. With time, these steps will become simple and natural in your workflow. Happy containerizing and good luck with your projects!

Top comments (0)