6

I'm trying to move my wordpress blog from a hoster to my own server. I wanted to use docker for that task.

On my server runs nginx which hosts a number of services not relevant to this question. I use the following script to create a container.

#!/bin/bash

docker create --name blog \
--net bridge \
-e WORDPRESS_DB_HOST=192.168.170.11 \
-e WORDPRESS_DB_USER=USER \
-e WORDPRESS_DB_PASSWORD=PASSWORD \
-e WORDPRESS_DB_NAME=wordpress \
-v /var/www/wordpress:/var/www/html \
-p 8000:80 \
wordpress

The DB connection works and apache2 is running. But I can't access wordpress.

When I try to access localhost:8000, is redirects me to port 80 where nginx is listening. Why? I want to connect to port 80 (apache2) inside the container.

Same when I try this from outside (if firewall down).

Another problem is the firewall: port 8000/tcp is open for all IPs but my requests from outside are still blocked. What do I have to open for this docker container?

Running Docker version 17.05.0-ce, build 89658be on Debian Stretch.

Edit:

root@server:~/docker# curl -v http://localhost:8000
* Rebuilt URL to: http://localhost:8000/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Mon, 12 Feb 2018 20:08:48 GMT
< Server: Apache/2.4.25 (Debian)
< X-Powered-By: PHP/7.2.1
< Set-Cookie: PHPSESSID=b03c4c1ba164bef366c49e1b1b5abc1c; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
< Set-Cookie: PHPSESSID=7293f22e5c860504a429b070d0ad21e4; path=/
< Location: http://localhost/
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< 
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact

I have no glue whats forwarding me here. The Apache2 is inside docker. There is no .htaccess file and I did not modify the image.

3
  • Please edit the question and paste curl -v http://localhost:8000/
    – kubanczyk
    Feb 11, 2018 at 9:58
  • I added the requested command. Feb 12, 2018 at 20:14
  • 2
    This redirect came from PHP, not from nginx. Check your WordPress configuration. Feb 12, 2018 at 21:25

2 Answers 2

6

For a minute stop your nginx, run this container with docker run -p 80:80 [...]. Then your browser will be able to access http://localhost/wp-admin/; there should be a property called something like "Site URL". Change it to http://localhost:8000/ and save.

This will cause Wordpress to redirect (it will use HTTP 301) any visitor to http://localhost:8000/ whether or not the Apache is listening on that port.

Then docker commit your customization, and run the committed image with docker run -p 8000:80 [...]

4
  • 1
    After changing the main URL you need to update the permalinks as well. Wordpress stores the permalink per post/page/... and this procedure updates them. And /wp-admin/ does not use this main URL, it should always work. No need to change the port. Feb 13, 2018 at 8:58
  • @GeraldSchneider Ahhhhhh, that secret about /wp-admin/ makes it really trivial. Please answer so I could delete my cruft.
    – kubanczyk
    Feb 13, 2018 at 13:41
  • Updating Permalinks? But I guess this domain is also used to generate all the links inside wordpress. Links to localhost will be no good in the WWW. My blog should be accessible from outside, too. And the entries in '/wp-admin' are write protected. I can change them in the config-file. Hint: my blog is a copy of a live website, not a fresh install. Feb 13, 2018 at 18:34
  • @Corni Migrating your dns domain from hosting to your own IP is already answered on this site. There are also countless question on having multiple domains served by one nginx. Ditto for directing traffic from nginx to docker for one specific domain. Maybe start there. This question is valid and deserves an answer, but your problem is elsewhere.
    – kubanczyk
    Feb 13, 2018 at 20:53
2

Edit the wp-config.php configuration file

It is possible to set the URL site manually in the wp-config.php configuration file.

Add these two lines to your wp-config.php, where "example.com" is the domain for your site.

define( 'WP_HOME', 'http://example.com' );
define( 'WP_SITEURL', 'http://example.com' );
1
  • Because we're talking about a docker container, it's not always easy to have that file (especially at the beginning). Mounting it as a volume works only, when the file already exists. But the latest versions of dockerized wordpress also have a look at the environment vars. So defining WP_HOME and WP_SITEURL should work better here... WP_HOME=http://localhost:8080/ and WP_SITEURL=http://localhost:8080/
    – TRW
    Jan 9 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .