Docker Wordpress - Warning: Cannot modify header information - headers already sent - php

I created a simple WordPress app in Docker by following this tutorial. After docker-compose up -d, I was treated with a simple setup. Once done, I am getting this error:
Warning: An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums. (WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.) in /var/www/html/wp-includes/update.php on line 209 Warning: An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums. (WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.) in /var/www/html/wp-includes/update.php on line 447 Warning: An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums. (WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.) in /var/www/html/wp-includes/update.php on line 728 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/update.php:209) in /var/www/html/wp-admin/includes/misc.php on line 1416 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/update.php:209) in /var/www/html/wp-includes/functions.php on line 6865 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/update.php:209) in /var/www/html/wp-admin/admin-header.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/update.php:209) in /var/www/html/wp-includes/option.php on line 1138 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/update.php:209) in /var/www/html/wp-includes/option.php on line 1139
The option.php file looks something like this (starting line 1137):
$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
}
// ...
Here's the docker-compose.yaml file:
version: '3'
# Defines which compose version to use
services:
# Services line define which Docker images to run. In this case, it will be MySQL server and WordPress image.
db:
image: mysql:5.7
# image: mysql:5.7 indicates the MySQL database container image from Docker Hub used in this installation.
restart: always
environment:
MYSQL_ROOT_PASSWORD: <root_password>
MYSQL_DATABASE: <db>
MYSQL_USER: <user>
MYSQL_PASSWORD: <password>
volumes:
- ./db:/usr/lib/mysql:rw
# Previous four lines define the main variables needed for the MySQL container to work: database, database username, database user password, and the MySQL root password.
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
restart: always
environment:
PMA_HOST: db
PMA_USER: <user>
PMA_PASSWORD: <password>
ports:
- '8080:80'
wordpress:
depends_on:
- db
image: wordpress:latest
restart: always
# Restart line controls the restart mode, meaning if the container stops running for any reason, it will restart the process immediately.
ports:
- '8000:80'
# The previous line defines the port that the WordPress container will use. After successful installation, the full path will look like this: http://localhost:8000
environment:
# debug mode
WORDPRESS_DEBUG: 1
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: <user>
WORDPRESS_DB_PASSWORD: <password>
WORDPRESS_DB_NAME: <userdb>
# Similar to MySQL image variables, the last four lines define the main variables needed for the WordPress container to work properly with the MySQL container.
volumes:
- ./wordpress:/var/www/html:rw
volumes:
mysql: {}
How do I solve this problem?
I found out about the problem after WordPress site failed to connect to the Internet. The error became visible once I started the debug mode with WORDPRESS_DEBUG: 1 based on some stackoverflow answer. This problem seems a bit weird to me since I have not made a single modification to the site.
The issue persists even after deleting all the files, containers and starting the docker-compose again.

Nevermind. Turns out, it was an issue with my institute's firewall. They have blocked the access to Wordpress, for some reason. It worked like a charm once I used my personal hotspot.

Related

file_get_contents not working with a local domain in docker container [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 11 months ago.
I'm struggling with the docker setup on localhost. When calling a PHP function file_get_contents() with a local domain, I'm getting three warnings:
1. SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
2. Failed to enable crypto
3. failed to open stream: Cannot assign requested address
Originally I have a relatively complex setup with nginx as reverse proxy, selfsigned SSL certs, mariadb, wordpress, wpcli, phpmyadmin, mailhog and redis.
For simplicity, I'm pasting a simple docker-compose file. On this setup, I'm getting: file_get_contents() failed to open stream: Cannot assign requested address
version: '3.6'
services:
mysql:
container_name: docker-test-mysql
image: mariadb:latest
volumes:
- ./db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_DATABASE: docker-test
restart: always
ports:
- 3306:3306
networks:
- webnet
wordpress:
container_name: docker-test-wordpress
image: wordpress:latest
ports:
- 8000:80
volumes:
- wp_data:/var/www/html:rw,cached
- ./wordpress:/var/www/html:rw,cached
depends_on:
- mysql
restart: always
environment:
WORDPRESS_DB_NAME: docker-test
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
WORDPRESS_DEBUG: 1
networks:
- webnet
networks:
webnet:
external: true
driver: bridge
volumes:
db_data: {}
wp_data: {}
The function works when calling with an external domain, but fails only with local domains (https://my-local-domain.local or http://localhost:8000).
What am I missing? Any help is appreciated!
Best regards
That's how docker works.
Inside a container localhost is itself.
You're mapping port 80 of the container on port 8000 of your local machine.
But inside the same container the correct url is simply localhost or the name of the container inside your docker-compose file (in this case wordpress)
because docker compose create also a network and uses the alias of your service inside the network to find them.
So you can use file_get_contents('http://localhost/...') or file_get_contents('http://wordpress/...')
You can also add extra host in the docker compose file if you want to use another alias

"Temporary failure in name resolution": Symfony 5.3 Connection to MariaDB with Doctrine and Docker

I've got my dev environment running in docker (nginx, php and mariadb) and try to create a database in symfony with doctrine. When I run php bin/console doctrine:database:create , I will get the following error:
[critical] Error thrown while running command "doctrine:database:create". Message: "An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution"
My configuration in .env looks like this:
DATABASE_URL="mysql://admin:symfony-admin#db/symfony_test?serverVersion=mariadb-10.1"
And this is my docker-compose.yml:
version: "3.6"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
php:
build: .
volumes:
- ./code:/code
links:
- db
db:
image: mariadb:latest
restart: always
ports:
- "33006:3306"
volumes:
- ./db:/docker-entrypoint-initdb.d/
environment:
MYSQL_ROOT_PASSWORD: 'symfony-root-pwd'
MYSQL_DATABASE: 'symfony_db'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'symfony-admin'
I also use adminer to have access to the database and there the login works.
Does someone know why I can't create a database with doctrine?
Cheers,
Michael
Solution:
I found the solution by myself. The command php bin/console doctrine:database:create need to be run within the php docker container and not in the local terminal.
So at first docker-compose exec php /bin/bash and then php bin/console doctrine:database:create
You can add
links:
- db
to php service config and change your DATABASE_URL to
DATABASE_URL="mysql://admin:symfony-admin#db/symfony_db?serverVersion=mariadb-X.X.X"
Service db will be exposed to php service, and in php container db will be host (link) to db service
Usually, this problem is in the password. As the Connection String is a URL, that encodes the special characters thinking it is a parameter.
To make it clear, fixing this requires one of these 2 steps:
URL encoding of the password so that you get quoted (% encoded) characters, e.g. in .env.local or .env:
For example, by replacing character # by %23 (its encoded version).
-DATABASE_URL=mysql://user:special#password#localhost:3306/db?serverVersion=8.0
+DATABASE_URL=mysql://user:special%23password#localhost:3306/db?serverVersion=8.0
Or Removing resolve: from doctrine.dbal.url in config/packages/doctrine.yaml:
doctrine:
dbal:
url: '%env(DATABASE_URL)%'
The first one works for me!
Answer reference: Malformed parameter "url". Symfony 5 does not accept special characters. #35568

Access denied when making connection from php container to mysql container

I’ve been searching the web for hours but do not find any solution to my problem.
I have 3 containers:
- nginx
- php
- mysql
They are all connected to the same network.
If I try to log into mysql from the mysql container, all works fine.
If I try to make a pdo connection from the php container to the mysql container, I always receive the message “Access denied for user”. I have no idea why. Let me show you guys what happens.
This is my docker-compose file:
version: ‘3’
networks:
danvers:
services:
#WebServer Service
danvers-nginx:
image: nginx:stable
container_name: danvers-nginx
ports:
- ‘8080:80’
volumes:
- ./:/var/www
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- danvers-php
- danvers-mysql-app
networks:
danvers:
#PHP Service
danvers-php:
build:
context: ./
dockerfile: Dockerfile
container_name: danvers-php
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
depends_on:
- danvers-mysql-app
networks:
danvers:
#MySQL Service for App Database
danvers-mysql-app:
image: mysql:5.7.22
container_name: danvers-mysql-app
restart: unless-stopped
tty: true
ports:
- “33061:3306”
environment:
- MYSQL_ROOT_PASSWORD=securerootpassword
- MYSQL_DATABASE={DB_APP_DATABASE}
- MYSQL_USER={DB_APP_USERNAME}
- MYSQL_PASSWORD=${DB_APP_PASSWORD}
volumes:
- danvers-app-data:/var/lib/mysql
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
danvers:
volumes:
danvers-app-data:
If I go inside the danvers-php container and open a interactive shell for php and try to make a pdo connection I get this:
php > new PDO(‘mysql:host=danvers-mysql-app;dbname=danvers-app’, ‘d-app-conn-user’, ‘cable’);
Warning: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user ‘d-app-conn-user’#‘danvers-php.projectdanvers_danvers’ (using password: YES) in php shell code:1
Stack trace:
#0 php shell code(1): PDO->__construct(‘mysql:host=danv…’, ‘d-app-conn-user’, ‘cable’)
#1 {main}
thrown in php shell code on line 1
The connection is made as I see the incoming request in the log from danvers-mysql-app.
What seems odd here is that the user-format in the error looks strange. I have no clue where the “danvers-php.projectdanvers_danvers” comes from. This is a concatenation of the php container and the network the containers are on. I think this is why the access is denied by have no clue how to change this as I assumed this should be the mysql containers name instead.
Does anybody have a clue to what I am doing wrong here?
Thanks for any reply or feedback.
EDIT:
In my mysql container I logged in with root and get this users list:
+-----------------+-----------+
| user | host |
+-----------------+-----------+
| d-app-conn-user | % |
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+-----------------+-----------+
Output from select user, host from mysql.user
The % for host name means no matter what the hostname is, as long as the username is correct, I should be able to login. The credentials are working from inside the mysql container but when the connection is made from another container, it fails.
If I try to make a connection with SequelPro from my local machine through port 33061, it also works. The problem only exist when it's an inter container connection.
EDIT2:
So I changed the nginx and php configuration with a php:7.4-apache configuration in my Dockerfile and now this problem does not exist anymore. I am able to log into the mysql container from the php container. The problem is related to the Nginx web server.

Docker php DPO to MariaDB - Error: Could not find driver

I'm learning PDO now and I found it better to learn it in a LEMP docker stack (Nginx, php-fpm, MariaDB, phpMyadmin) on my Ubuntu 18.04LTS.
This is my php file:
<?php
try {
$mydb = new PDO('mysql:host=database;dbname=mysql;charset=utf8', 'root', 'admin');
} catch (Exception $e) {
die('Error : ' . $e->getMessage());
}
?>
As you can see, I try to make a PDO in my php code to recover some datas from my db.
But everytime I got that message on my browser (Firefox 69.0.2):
Error : could not find driver
I saw that post here: "Docker can't connect to mariadb with PHP". The problem was quite similar to mine but it didn't work for me.
Note: php-fmp and Nginx work perfeclty together. Same for MariaDB and phpMyAdmin.
Here is my docker-compose.yml file:
version: "3"
services:
nginx:
image: tutum/nginx
ports:
- "7050:80"
links:
- phpfpm
volumes:
- ./nginx/default:/etc/nginx/sites-available/default
- ./nginx/default:/etc/nginx/sites-enabled/default
- ./logs/nginx-error.log:/var/log/nginx/error.log
- ./logs/nginx-access.log:/var/log/nginx/access.log
phpfpm:
image: php:fpm
links:
- database:mysql
ports:
- "7051:9000"
volumes:
- ./public:/usr/share/nginx/html
database:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: admin
ports:
- "7052:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
links:
- database:mysql
ports:
- "7053:80"
environment:
PMA_HOST: mysql
PMA_USER: root
PMA_PASSWORD: admin
PMA_ARBITRARY: 1
If it is possible to solve this without building my own Dockerfiles, it would be great.
But if I must, I will. This isn't a problem.
docker-compose is an "api" of sorts for a Dockerfile. You need to add those libraries (apt-get etc...) in the Dockerfile
Dockerfile is your friend!
Is your PHP file inside a docker container or is it running outside docker, in the host machine?
If it is running inside the docker container, which service is it in? Please note that the nginx service does not have the "links" configuration, meaning it only accesses the database through the "database" hostname. Check the port as well (in the end of this post).
If your PHP file is running outside, then you have to use localhost instead of mysql in your connection string, like so: 'mysql:host=localhost;dbname=mysql;charset=utf8'. This is because docker's internal DNS is just that: internal. You can't access this hostname (database or mysql) outside docker.
Equally important, your connection string is not specifying the port, which is 7052 in your case. Since you're redirecting from 7052 to 3306, I think 3306 is mysql's default port, and the driver assumes 3306 if you do not specify it. It's always a good idea to be explicit about hosts and ports. Check the documentation on PHP databse connection strings about it (as I know nothing about php). It's probably ...;port=7052 or something.
Also, read up on docker-compose links, which you are using. It's deprecated now, so I advise to not use it in future projects, I even advise to spend some time removing it. Should take like 30 seconds to 5 minutes if everything goes well, and it won't haunt you anymore.
A found the solution.
First of all, the host must be mysql and not the name of my container (which is database):
$mydb = new PDO('mysql:host=mysql;dbname=mysql;charset=utf8', 'root', 'admin');
Inside the phpfpm container (accessible via the command docker-compose run --rm <container-name> bash), I had to enable the extension=php_pdo_msql line in my config file php.ini by removing the semicolon at the beginning of its line.
To avoid doing this manually every time after a docker-compose up, I replaced the phpfpm service in my docker-compose.yml file the following Dockerfile:
FROM php:fpm
RUN docker-php-ext-install pdo pdo_mysql
Finally, just build the image with the command docker-compose build . (replace the . by the path to the directory containing the docker-compose.yml file).
It works perfectly for me.

Can't connect to between mysql container and php in docker

Trying to connect to mysql 8 (mysql:latest) from a php:latest image (without apache or nginx) and I'm getting this annoying error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /src/conn.php:5
Stack trace:
#0 /src/conn.php(5): PDO->__construct('mysql:host=data...', 'root', '12345678')
#1 {main}
thrown in /src/conn.php on line 5
I've looked for +20 solutions here on stackoverflow but none of them was able fix my problem. I can connect without a problem using mysql workbench or DataGrid using the following credentials:
server: localhost user: root pass: 12345678
But any configuration I use on the php image I can't make them connect with each other.
My docker-compose file:
version: '3'
services:
composer:
container_name: composer
networks:
- backend
image: composer
volumes:
- .:/app
command: install
database:
container_name: mysql
networks:
- backend
image: mysql:latest
volumes:
- ./database/mysqldata:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 12345678
MYSQL_DATABASE: testdb
ports:
- "3306:3306"
php:
build:
context: .
container_name: php
networks:
- backend
image: php:apache
links:
- database
env_file:
- .env
volumes:
- .:/
command:
php /src/conn.php
depends_on:
- composer
- database
networks:
backend:
My conn.php:
<?php
$db = new PDO('mysql:host=database;dbname=testdb', 'root', '12345678');
What I did until now to try to fix the problem:
Change conn.php host to 127.0.0.1
Change conn.php host to localhost
Tried to use mysqli (got also connection refused)
Tried another php images (with apache, nginx etc)
Tried to use mysql as a host
PS: I'm using docker for mac
After 5 hours of research I really don't know what else to do and as I'm not a docker expert, would appreciate any help to point me the direction of the fix.
Thank you!
In your case, you can't connect to mysql via localhost or 127.0.0.1. If you connect like this. It call to localhost inside php container. But there is no mysql install in your php container.
You must connetc via container name. Or docker inspect you mysql container and get the container IP. For example with your docker-compose
database:
container_name: mysql
Now your mysql database host is mysql - container name instead localhost
Was able to find the solution. Turns out I need to wait for mysql service to be available before I start the php script. So I used the wait-for-it solution and did this on docker-compose:
command: >
bash -c "chmod +x /docker/./wait-for-it.sh
&& /docker/./wait-for-it.sh database:3306 -- echo 'database is up'
&& php phpscript.php"
Thank you for the help

Categories