Docker Wordpress Container Exits after ~30 seconds

1.6k Views Asked by At

I am new to using Docker, and am currently trying to create a Wordpress website locally using a Wordpress Docker container, and a MySQL database (Note: I am also new to MySQL). The MySQL database is not running in a container, but I would rather like to make my own local database on my machine.

When I try creating a Wordpress container using this command:

sudo docker run --name wordpress_local -e WORDPRESS_DB_HOST=localhost -e WORDPRESS_DB_USER=test -e WORDPRESS_DB_PASSWORD=password -d wordpress

The container runs for exactly 27 seconds, then exits.

I am not sure if this is because it cannot connect to my database or not, however, when I create Wordpress containers linked to a MySQL container I do not have this problem. Steps to setting up my database:

  • Installed MySQL server and client on my machine
  • Created a user called test@localhost and gave it a password
  • Granted all wordpress privileges to this user

Any help would be appreciated!

EDIT:

The log for the container looks as such:

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on                  line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on   line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10

MySQL Connection Error: (2002) No such file or directory

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in - on line 10 

EDIT 2:

Running sudo docker events --filter wordpress_local produces:

2015-06-10T13:57:11.000000000Z e9000a3a085731d527f0e64a26f681126f5885f9ef0d4a845361d581ae2d111a: (from wordpress:latest) die
1

There are 1 best solutions below

0
On BEST ANSWER

I have found the solution. Docker offers two networking modes: bridge, and host.

The bridge networking mode is default, and when using this mode, MySQL must be listening for connections on the Docker host IP address (172.17.42.1). In other words, the bind-address in the MySQL config file must be set to: 172.17.42.1

My solution was to use the host network mode by adding the --net="host" flag to my command. This allows me to connect to localhost from my containers, and allows my to keep 127.0.0.1 as my MySQL bind address. The correct command is shown below.

sudo docker run --name wordpress_local --net="host" -e WORDPRESS_DB_HOST=127.0.0.1 -e WORDPRESS_DB_USER=test -e WORDPRESS_DB_PASSWORD=password -d wordpress

EDIT: It is good to note that this is not a very safe method, as it allows the container to access local network services. A better method would be to compromise database flexibility and have it bind to the Docker host, or to allow the Database to accept connections from all services, and use a Firewall to filter out unwanted access.