a.u.r
a.u.r

Reputation: 1263

can't access apache on docker from my localhost

I've been following this tutorial for beginners about docker which basically instructs you to create an apache container and map a localhost port to the one on the container. when I try localhost:80 it doesn't connect, although the container is up and running. I even made a rule in the firewall to allow connection to port 80, but couldn't get connected to the localhost.

Any ideas ?

Upvotes: 9

Views: 19214

Answers (3)

Keyvan
Keyvan

Reputation: 121

I stumbled upon this question as I was looking for a way to bind my local HTTP port (80) to the HTTP port of my container, an Apache container running on Docker Desktop for Windows - through WSL2 (this is important)

I couldn't find a quick and easy way to do this, so I figured it out myself.

What you must do is bind your local port (on Windows) to the port on WSL.

Here is how I did it :

$wsl_ip = (wsl -d "docker-desktop" -- "ifconfig" "eth0" "|" "grep" "inet addr:").trim("").split(":").split()[2]
netsh interface portproxy add v4tov4 listenport=443 listenaddress=0.0.0.0 connectport=443 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=80 connectaddress=$wsl_ip

You can either create a Powershell Script (.ps1) and run it with Powershell, or copy/paste each command line into Windows Terminal / Powershell running with Administrator Privileges.

What this does is :

  1. attach to the "docker-desktop" distribution running in WSL2 2
  2. run "ifconfig eth0 | grep inet addr:" to get the local IP address of the "virtual machine"
  3. parse the result, and use Netsh to create a portproxy between port 80 of your Windows machine and port 80 of your Linux machine. Same is done for port 443. You can easily map other ports if you understand what the command is doing.

More explanation :

Since Docker for Windows 10/11 uses WSL2, when you expose a port (through docker-compose or with an EXPOSE command in your Dockerfile), it is exposed to a Linux Distribution called "docker-desktop" that is ran with WSL2. For some reason, ports 80 and 443 that are exposed from a container are NOT forwarded to the host. The official documentation acknoledges some issues but their solution is just to use another port (for example, 8080 mapped to 80).

Issues with this method : Each time you reboot your system (or WSL2), the Linux machine gets assigned a new IP and you have to do it again. What you could do is setup a command to run when your container starts that connects through ssh to the host and runs the script, but I'm too lazy to have done it myself.

Upvotes: 0

Yalcin Yavas
Yalcin Yavas

Reputation: 51

Add a line to your DockerFile before restarting apache.

RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf

Upvotes: 5

Xiongbing Jin
Xiongbing Jin

Reputation: 12097

On Windows/OS X, Docker is running inside a Linux virtual machine (Docker Toolbox) with a default IP address of 192.168.99.100. Thus, when you use docker run -p 80:80 to bind the container port to host port, it in fact binds to the virtual machine's port 80. Thus the address you need is http://192.168.99.100.

The 172.17.0.3 address is the address of the docker container inside that virtual machine, and is not accessible directly from Windows/OS X.

Upvotes: 13

Related Questions