0

I'm new to Docker and Containers, and I'm trying to run a simple asp.net web app in a container but running into issues. My OS is Windows 10 Home, so I have to use the Docker Toolbox, which runs on a VM that only includes a basic Linux OS. When I spin up the container, it seems to start fine, but I can't view the app on the localhost.

$ docker run -p 8342:5000 -it jwarren:project

Hosting environment: Production Content root path: /app Now listening on: http://*:5000 Application started. Press Ctrl+C to shut down.

$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 98cc4aed7586 jwarren:project "dotnet run" 8 minutes ago Up 8 minutes 0.0.0.0:8342->5000/tcp naughty_brattain

I've tried several different recommendations that I found on the web, but none have helped so far. However, my knowledge of networking is very limited, so maybe I'm not fully understanding what needs to be done. I've tried accessing it with the default VM machine IP and the container IP. I understand that the port forwarding does not carry over to the container. Any assistance would be great, as this project is due on Tuesday, and this is the last road block before finishing.

JoJo
  • 77
  • 1
  • 10

2 Answers2

3

I found the following post that was really helpful: How to connect to a docker container from outside the host (same network) [Windows]. Following the steps below worked perfectly:

  1. Open Oracle VM VirtualBox Manager
  2. Select the VM used by Docker
  3. Click Settings -> Network Adapter 1 should (default?) be "Attached to:NAT"
  4. Click Advanced -> Port Forwarding Add rule: Protocol TCP, Host Port 8080, Guest Port 8080 (leave Host IP and Guest IP empty)

You should now be able to browse to your container via localhost:8080 and your-internal-ip:8080.

Started up the container (Dockerfile EXPOSES 5000): docker run -p 8080:5000 -it jwarren:project

Was able to connect with http://localhost:8080

JoJo
  • 77
  • 1
  • 10
0

There are few things to consider when working with a VM networking.

Virtual Box has 3 types of networking options NAT, Bridged and Host Only.

  • NAT would allow your VM to access internet through your internet. But won't allow your HOST machine to access the VM
  • Host Only network will create a network where the VM can reach the host machine and the Host can reach the VM. No internet using this network
  • Bridged network will allow your VM to assign another IP from your Wifi router or the main network. This IP will allow VM to have net access as well as access to other machines on the network. This will allow even the host machine to reach the IP

Now in most cases when you want to run Docker inside a VM and access that VM using the host machine you want the VM to have both NAT and Host only bridges

Now accessing your app on port 8342 needs few things checked

  • seliunx, firewalld, ufw are disabled on your VM (or properly configured to allow the port)
  • Your VM has a host only network or bridged network
  • iptables -S should not show REJECT rules

Some VMs come pre-configure to only allow port 22 from external network. So you should try access the app on <hostonlyip>:8342 or <bridgedip>:8342.

If you want to test if the app is up or not you can do the following

docker inspect <containerid> | grep IPA

Get the IP from this and run the command

curl http://<containerip>:5000/

This command needs to be execute inside the VM and not on your machine. If this command doesn't work then your container is not listening on 5000. Sometimes app listen to only 127.0.0.1 inside the container. This means they will work only inside the container and not outside. The app inside the container needs to listen to 0.0.0.0

If nothing works you can try an ssh tunnel approach

ssh -L 8342:127.0.0.1:8342 user@<VMIP>

And then you should be able to access the app on localhost:8342

Tarun Lalwani
  • 124,930
  • 8
  • 149
  • 214
  • Thanks for the quick and thorough response! I'll give it a shot when I get home and comment how it goes. – JoJo Jul 31 '17 at 17:20