1

I am making my first steps with docker-compose. I created a very basic docker-compose.yml file with this content:

version: '2'
  services:
    webserver:
      build: ./docker/webserver
      image: runwaytest_web
      ports:
        - "80:80"
        - "443:443"
      volumes:
        - /myhome/Docker/simple-docker/www:/var/www/html
        - /myhome/Docker/simple-docker/symfony3:/var/www/symfony3
      links:
        - mysql

     mysql:
       # mysql stuff

I also have a very basic Dockerfile in ./docker/webserver. Servers are created correctly. If I ssh to the container, apache is running and the config file is correct.

When I inspect my container from the host, the IP is 172.18.0.3, but I can't ping it, and virtual host for symfony3 does not work (actually I can't neither reach the base http-document folder in /var/www).

I am using Docker for Mac.

What I am doing wrong?

1 Answer 1

4

See https://stackoverflow.com/a/24149795/99189 for why you can't ping your container. In general, don't expect to be able to do that. The only network access you have to the container is through the ports that you expose, 80 and 443 in this case.

From the perspective of running this in a docker container and using virtual hosts, you'll need your http client to send a Host: header when making requests to localhost:80/localhost:443.

Assuming you are testing with a browser, and that your vhost is user3174311.com, try the following:

  • add the line 127.0.0.1 user3174311.com to your /etc/hosts
  • visit user3174311.com in your browser

This is what should be happening:

  • browser looks up user3174311.com in /etc/hosts and resolves it to 127.0.0.1
  • browser sends an http request with a Host: user3174311.com header to 127.0.0.1:80
  • docker is listening on this address and forwards the connection to port 80 in your container
  • apache sees the request, looks at the Host: header and determines the correct virtual host to use

After that, it depends on your apache/symphony3 configuration. You'll have to post more details if it's not working.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer, of course I added the ip in my /etc/hosts file, and I also created a virtual host on the guest. I am not a networking expert, but I expected to be able to ping the IP if I want the browser on the host to access the vhost on the guest? this is what I do usually when working with vagrant but it now seems everybody is moving to docker...
Which ip did you add? It should not be that of the docker container, because you can't route to it. It should be localhost. Docker is listening on localhost:80 and localhost:443 and forwarding the traffic to the container.
ok I got it now, thanks. So how can I point the browser to a vhost I defined inside the container?
In /etc/hosts add: 127.0.0.1 thevhost. In the browser, visit http://thevhost. The browser (after reading the hosts file) will initiate a connection to 127.0.0.1:80 and send an http request with a header Host: thevhost. Docker will accept this connection and forward it to port 80 of the container, where the request will be received by apache, which will interpret the Host header to identify the vhost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.