Docker Community Forums

Share and learn in the Docker community.

NATing fails when the host listens on another port than the container


(Edoardo) #1

Hi all,

I have a question about the ‘port forwarding’ of docker. I can not seem to be able to get it to work. I must be doing something wrong. I hope someone can set me back on the right path!

I built a wordpress environment that works fine when all is set on port 80 (host and container), but as soon as I get the host to listen on port 8080, it fails.

My settings:

docker-compose yaml file

[...]
wp:
    build: httphp
    name: wp
    hostname: wp
    ports:
        - "0.0.0.0:8080:80"
    volumes_from:
        - wpdata
    links:
        - db
[...]

The Docker file of the wordpress container

FROM php:5.6-apache

[...]
COPY webapp.conf /etc/apache2/sites-available/
COPY security.conf /etc/apache2/conf-available/security.conf
COPY entrypoint.sh /
CMD ["/entrypoint.sh"]

The webapp.conf file

<VirtualHost *:80>
    ServerName 192.168.1.4
[...]

the entrypoint file

[...]
wp core install --allow-root --path=/var/www/html --url=http://192.168.1.4/ --title="${WP_TITLE}" --admin_user=${WP_USER} --admin_password="${WP_PASSWORD}" --admin_email="${WP_EMAIL}"
[...]

where ‘192.168.1.4’ is the IP of the host.

My result

(1) From my computer, I browse to 192.168.1.4:8080 and get a “Can’t connect to the server” in the browser but a response from the container
wp_1 | 192.168.1.10 - - [08/Aug/2015:19:55:54 +0000] “GET / HTTP/1.1” 301 329 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12”

I’m guessing this means that the container wants to display the page and does so on port 80 and the port forwarding does not relay the info.

$ curl -LI http://192.168.1.4:8080/
HTTP/1.1 301 Moved Permanently
Date: Sat, 08 Aug 2015 20:00:46 GMT
Server: Apache/2.4.10 (Debian) PHP/5.6.11
X-Powered-By: PHP/5.6.11
X-Pingback: http://192.168.1.4/xmlrpc.php
Location: http://192.168.1.4/
Content-Type: text/html; charset=UTF-8

curl: (7) Failed to connect to 192.168.1.4 port 80: Connection refused

(2) From my computer, I browse to 192.168.1.4:80 nd get absolutely nothing. Figures.

$ curl -LI http://192.168.1.4/
curl: (7) Failed to connect to 192.168.1.4 port 80: Connection refused

(3) I get the exact same result on the host.

(4) From the host I query the IP of the container directly on port 80 and it of course works.

$ curl -LI http://172.17.0.6:80/
HTTP/1.1 200 OK
Date: Sat, 08 Aug 2015 20:05:23 GMT
Server: Apache/2.4.10 (Debian) PHP/5.6.11
X-Powered-By: PHP/5.6.11
X-Pingback: http://192.168.1.4/xmlrpc.php
Content-Type: text/html; charset=UTF-8

Please help. What am I doing wrong. I’m so close but no cigar!


(Jeanepaul) #2

Greetings,

is this on a boot2docker? could you share more info about how you run it or can you docker ps -a ?


(Edoardo) #3

Hi,

Thanks for taking an interest!

I can do one better.

I am running docker host on VM Workstation. The host in CentOS 7, docker is version 1.7 and it’s the latest version of docker-compose. The host is bridged and has an IP of 192.168.1.4 (which you’ll find mentioned in a few files.

The following link points to a tar file with my entire environment for wordpress. Feel free to use and play with it.

docker.tar

What am I doing wrong?

Thanks.
-Ed


(Jeanepaul) #4

Greetings,

It should work though. I suppose your IPtables are off?


(Edoardo) #5

Indeed, iptables is disabled. I also tried on another distro (ubuntu) with the same result. It looks like the web server listening on port 80, gets the request and sends it back to port 80 but nating never happens.

Do you have a test environment to test it on? a simple ‘docker-compose up’ in the docker folder should do it.


(Jeanepaul) #6

Greetings,

give me a sec, I’ll check it again :smiley:


(Jeanepaul) #7

Greetings,

could you try this?, I just clean up some double quotes. this works to me now. I’m using OS X with boot2docker.

then http://192.168.59.103:8080/wp-admin/setup-config.php

dbdata:
    image: busybox
    name: dbdata
    hostname: dbdata
    volumes:
        - /var/lib/mysql
wpdata:
    image: busybox
    name: wpdata
    hostname: wpdata
    volumes:
        - /var/www/html
db:
    image: mariadb
    name: db
    hostname: db
    ports:
        - 3306:3306
    volumes_from:
        - dbdata
    environment:
        MYSQL_ROOT_PASSWORD: mysecretpassword
        MYSQL_USER: wpuser
        MYSQL_PASSWORD: wppass
        MYSQL_DATABASE: wordpress
wp:
    build: httphp
    name: wp
    hostname: wp
    ports:
        - 0.0.0.0:8080:80
    volumes_from:
        - wpdata
    links:
        - db
    environment:
        WEBAPP: wordpress
        WP_TITLE: "My Blog"
        WP_USER: "admin"
        WP_PASSWORD: "p@ssw0rd"
        WP_EMAIL: "myemail@domain.com"

(Edoardo) #8

Hi,

It still fails for me. When I mention the setup-config.php in the URL I get the content but without css. Same goes for the login

wp_1 | 192.168.244.10 - - [13/Aug/2015:13:12:42 +0000] "GET /wp-admin/setup-config.php HTTP/1.1" 500 3458 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0"
wp_1 | 192.168.244.10 - - [13/Aug/2015:13:12:45 +0000] "GET /wp-admin/install.php HTTP/1.1" 200 995 "http://192.168.244.128:8080/wp-admin/setup-config.php" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0"
wp_1 | 192.168.244.10 - - [13/Aug/2015:13:12:47 +0000] "GET /wp-login.php HTTP/1.1" 200 1601 "http://192.168.244.128:8080/wp-admin/install.php" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0"

However, If I connect directly (http://192.168.59.103:8080/) I get

wp_1 | 192.168.244.10 - - [13/Aug/2015:13:16:01 +0000] "GET / HTTP/1.1" 301 337 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) konqueror/4.14.4 Safari/534.34"

The requested operation could not be completed
Connection to Server Refused

I think I might have to start from scratch with a basic apache server with nating and go from there. :\

You got it working though which is odd. If you get any other idea, let me know. In the meantime I’ll have to do a bit of work on it tonight. If I find anything, I’ll post it.

Thanks!


(Jeanepaul) #9

That’s odd. I’ve used it, installed wp, and tried posting few… Try other
wp using docker-compose. There are lots out there. I’ll get back to you
later, I’m home bound from work :smiley:


(Edoardo) #10

Hi,

This is part of my learning process. I wanted to build it myself so that I’d wrap my head round the docker way of doing things. You should not have to install docker, I do it in my entrypoint script. Maybe that’s where it all goes wrong.

I’m stuck at work so it will have to be for later today.

Thanks for your help!


(Edoardo) #11

OK, so I managed to do a few tests before the kids had enough of me on the computer.

  1. FYI, in docker-compose, in the yml file, the ports should be in between quotes so that they are passed as string or yml might interpret ports under 60 the wrong way,

  2. a simple php-apache image with a basic index.php showing php info works fine on port 8080.

  3. As soon as I install wordpress, port 8080 fails. I’ll have to look at the wordpress official image and see how they do it.

Thank you for your help. Nice picture btw! :wink:


(Jeanepaul) #12

Greetings,

thanks!

glad you make it out, awesome. if you are still looking around, you might have stumbled on this? http://www.sitepoint.com/how-to-use-the-official-docker-wordpress-image/ its a docker-compose wordpress. I tried it also, and its working, worth to see/ try though.

goodluck with you!


(Edoardo) #13

Thanks for the link! I’ll have a look at it tonight for sure! :wink:


(Edoardo) #14

For the sake of completing this post, I found my problem and it had nothing to do with docker.

I was using the wordpress CLI to prepare my website and it seems I was using them incorrectly. I removed them from my entrypoint script and now I have a working wordpress. :smiley: