Accessing apache2 residing inside Docker container

2019-07-17 23:48发布

I have been creating a webserver as Docker container and trying to access it outside the container. But I can't. What I am doing is.

-Pulling a ubuntu image:

docker pull ubuntu:14.04

-Starting a container:

docker -t -i -p 49200:2375 [image-id]

and after installing apache2 in newly started container I can ping the apache2 server inside container, I can also do curl and see results in container terminal, but no luck outside on host machine. I have tried many times. (I am using Virtual Box on windows)

Screen shots:

docker 1

docker 2

Questions:

  1. Am I using right docker commands and port numbers to create container.
  2. Is there anything wrong in apache configuration. (I am using apache in default mode without any configurations)
  3. Is there anything wrong with ports on my virtual box or on my pc. I have very low knowledge of networking.

Can someone help!!!

1条回答
一纸荒年 Trace。
2楼-- · 2019-07-18 00:30

Problem 1: Apache default port

2375 port is not the apache default port, it is the docker daemon default port, the command shall be

docker -t -i -p 49200:80 [image-id]

Problem 2: port in different machine

49200:80 This is the mapping from internal port 80 to docker host 49200, the docker host is actually your virtual box VM (guess is your boot2docker shell)

In your virtual box VM console, you can

curl localhost:49200

The picture "Port Forwarding rules" are helping your port forward to your virtualbox host, for you it is MacOS, where you mapped 49200 to MacOS 49200 as well

So in your MacOS shell console, you can also

curl localhost:49200

If you know the virtualbox VM (docker host) IP address, for example it is 192.168.59.103 as default for boot2docker, then you can

curl 192.168.59.103:49200

If you want to access the apache docker container via 172.17.0.7, remember this is the private network inside, you need to start another container like

docker -t -i busybox bash
# curl 172.17.0.7:80

Summary

docker -p is port forwarding your internal port to docker host "Port forwarding Rules" in virtualbox setting is port forwarding your VM port to VM host

  • apache docker container: 172.17.0.7:80
  • docker host=virtualbox VM: 192.168.59.103:49200
  • virtualbox host=MacOS: xxxx:49200
查看更多
登录 后发表回答