how to use docker with wordpress and nginx - mysql

i try to use docker for use wordpress with https, but that not work,
i have the message :
wordpress | MySQL Connection Error: (2002) Connection refused
version: "3.8"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
container_name: wordpress
image: wordpress:php7.4-apache
restart: always
stdin_open: true
tty: true
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- ./wordpress:/var/www/html
nginx:
container_name: nginx
image: nginx:latest
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf:/etc/nginx/conf.d

I had the same problem today and after lots of unsuccessful attempts, I think adding expose helped :
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
expose:
-"3306"
Reference : https://docs.docker.com/compose/compose-file/#expose

firstly, your title is "how to use wp with nginx" but in your docker compose file, you are clearly pulling a wordpress:apache image.
secondly, you can delete the whole content related to nginx since that container runs alone and outside the scope of your wordpress.
now, that you only have wordpress and db as services in your yml file (which is how it should be), you must add a ports directive to wordpress:
ports:
- 80:80
And finally, for the containers to be able to resolve their ips within the same network, you need to add this at the end of your yml file:
volumes:
wordpress:
db:
Now this will ensure you're running a wordpress with a db, and the wordpress image runs apache (which is httpd, not nginx).
Thirdly, if you're not satisfied with httpd and want an nginx container, you'll need to look for a wordpress image on hub.docker.com that runs on nginx.

Related

Docker WordPress image can't connect to database

I have an issue with a docker image.
I have just followed this tutorial to get custom local domains: https://medium.com/#francoisromain/set-a-local-web-development-environment-with-custom-urls-and-https-3fbe91d2eaf0
This seem to work good. But now I can not seem to connect to the Mysql database.
I'm using the WordPress image and my docker-compose.yml file looks like this:
version: '3.3'
services:
db:
image: mysql:5.7
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
expose:
- 80
environment:
VIRTUAL_HOST: phpmyadmin.local
VIRTUAL_PORT: 80
PMA_HOST: db
MYSQL_ROOT_PASSWORD: p4ssw0rd!
wordpress:
depends_on:
- db
image: wordpress:latest
restart: unless-stopped
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
restart: always
expose:
- 80
environment:
VIRTUAL_HOST: domain.local
VIRTUAL_PORT: 80
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wp_name
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: p4ssw0rd!
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
volumes:
db_data: {}
networks:
default:
external:
name: nginx-proxy
When I start running the container, the error I'm getting is this:
So now I know my WordPress container and proxy are working (domain.local), but the only thing missing is the connection to the database.
I think this has to do with the 'network' and that he simply can't find the database. But I have no idea how to connect it.
Hope one of you guys know what I'm doing wrong.
Thanks in advance.
Okay, so I finally figured it out.
The WORDPRESS_DB_USER should be set to: root
Then everything will work.

Setup Docker containers for existing Wordpress site

I'm trying to setup a dev environment for an existing wordpress website hosted on cPanel.
I've exported the test data from the existing pre-production database to be imported into the mysql running in one of the containers.
version: '3.3'
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: P#ssw0rd
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- /local/path/to/wordpress/src/:/var/www/html
volumes:
db_data: {}
everything starts up fine. Now I'm inserting the db dump into the mysql db in the container
cat dump.sql | docker exec -i docker_db_1 /usr/bin/mysql -u wordpress --password=wordpress wordpress
which finishes without error. When trying to access the website now on localhost:8000 the Apache Ubuntu default page pops up but I can't see anything from the existing wordpress site.
This setup worked for me
docker-compose.yml
version: '3.6'
services:
wordpress:
image: wordpress:${WORDPRESS_VERSION}
container_name: wordpress
volumes:
- ${WORDPRESS_DATA_DIR}:/var/www/html
environment:
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
depends_on:
- mysql
restart: always
mysql:
image: mysql:${MYSQLDB_VERSION}
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
- ./mysql_config:/tmp/mysql_config
nginx:
image: nginx:${NGINX_VERSION:-latest}
container_name: nginx
ports:
- '80:80'
- '443:443'
volumes:
- ${NGINX_CONF_DIR}:/etc/nginx/conf.d
- ${NGINX_LOG_DIR}:/var/log/nginx
- ${WORDPRESS_DATA_DIR}:/var/www/html
depends_on:
- wordpress
restart: always
All used variables are set as environment variables first.
To connect to any container with use the following command:
docker exec -i -t <wordpress|mysql|nginx> /bin/bash

docker mysql wordpress port doesn't connect

I downloaded the mysql and wordpress images. Mysql ports are
3306 localhost:32781
33060 localhost:32780
Wordpress configuration is
WORDPRESS_DB_HOST 192.168.99.100:32774
MYSQL_ROOT_PASSWORD and WORDPRESS_DB_PASSWORD are the same
I try to connect to wordpress with
http://192.168.99.100:32774/
I get the message
This site can’t be reached
How do I have to configure the ports of mysql and wordpress?
CONFIGURATION MYSQL
WORDPRESS
Error trace
From what you can find on the docker configuration page, you should take this example and modify it to your needs.
There is the following docker-compose file that will launch a wordpress in a minute:
https://docs.docker.com/compose/wordpress/
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
From that file you have various options like, using docker compose tool (https://docs.docker.com/compose), or if you have a swarm running you could use docker stack command(https://vsupalov.com/difference-docker-compose-and-docker-stack/) or you can divide the configuration of both elements and create separate Dockerfile's(the configuration of a Docker file differs from what you can see on docker-compose so take the information an create your own) and launch them separated, you should launch mysql first as wordpress depends on a bbdd running first.
The easiest way to implement this is using docker-compose. Here is an example:
version: '3.2'
services:
database:
image: mysql:5.7
volumes:
- my_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: password
wordpress:
depends_on:
- database
image: wordpress:php7.3-apache
ports:
- '8000:80'
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: password
working_dir: /var/www/html
volumes:
my_data: {}
A few notes: the database doesn't mount any port on host because it doesn't need to. If you don't want to use docker-compose you can run docker run commands for this but then you have to create your own network for the containers and attach them to it.
Wordpress will be available on http://localhost:8000.
WORDPRESS_DB_HOST is the connection to the database and you won't be able to access that through http anyway.
Hope this helps you.

Docker Wordpress keeps redirecting to online version of site

I'm trying to set up docker wordpress as my dev environment on Ubuntu 17.10. I've made a copy of the db and placed it into the docker mysql service on port 8080 (using a wp plugin, which changes the home and site url)
I've arrived at the docker-compose.yml file below, but everytime I go to localhost:8000 or port 80 I get redirected to the original site online. I'm at a loss as to know what is wrong?
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootPword
MYSQL_DATABASE: xyz_wp
MYSQL_USER: xyz_2015
MYSQL_PASSWORD: userPword
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
# generally need to use non-default values
WORDPRESS_DB_HOST: db:3306
# next line often not in tutorials - https://stackoverflow.com/questions/46117771/issue-getting-docker-to-access-my-database-properly-with-wordpress
WORDPRESS_DB_NAME: xyz_wp
WORDPRESS_DB_USER: xyz_2015
WORDPRESS_DB_PASSWORD: userPword
WORDPRESS_TABLE_PREFIX: "af_"
working_dir: /var/www/html
volumes:
- /home/simon/code/wp_af2015/wp-content:/var/www/html/wp-content
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: rootPword
restart: always
ports:
- 8080:80
links:
- db
volumes:
db_data:
I think there is a little misunderstanding here.
when you add : after the domain it specifies the port.
From the Screenshot of PhpMyAdmin, it seems you set the Wordpress on port 8000. Because after :, 8000 has been set.
From your docker file, there is a mistake, because from what you written, it is wrong.
The ports should be or :80 or :8000, but not both.
If you want your docker to listen to localhost port 8000 (your WP),
then on your docker config, you should set:
ports:
- "8000"
Restart Docker, and it should work as expected.
A day later I tried to reach the dev site using an incognito window and everything worked! So the solve was to delete my browsing data from my normal browser. I do not understand what happened but the issue is fixed

Issue getting docker to access my database properly with wordpress

I'm new to docker all together - but am trying to setup a local test environment to play with some wordpress things.
So I went to the docker site and pulled up a default docker .yml file on how to get it going easily.
I've made just a couple changes, but mostly this is a straight forward document.
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somerootwordpresspw
MYSQL_DATABASE: testdatabase
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./WP-TEST/:/var/www/html/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
When I run docker-compose up with the above .yml file, I see this error:
MySQL "CREATE DATABASE" Error: Access denied for user 'wordpress'#'%' to database 'wordpress'
Which I find odd, because I'm naming the database testdatabase, so why is it trying to create a database named wordpress?
When I connected with SQL Pro, I could see testdatabase, but according to the console it's trying to create wordpress db.
How do I get it to connect to my named DB, instead of constantly failing to create wordpress?
So I think I got it.
It was really simple. In my wordpress portion of my .yml file I needed to include WP_DB_NAME: testdatabase
By doing that, it used my named testdatabase to install wordpress to.
Hope this helps people who might stumble across this.
Now the .yml file looks like this:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somerootwordpresspw
MYSQL_DATABASE: testdatabase
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./WP-TEST/:/var/www/html/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: testdatabase
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: