I'm using a Docker Compose Lamp Stack environment from this github repo . It works well, however I can't connect to mysql via localhost. While testing database connection I had to set my server name to "database" for it to work.
I'm sure this is caused by something within the docker-compose.yml set up, but despite trying to change container_name: 'database'
to container_name: 'localhost', I couldn't get it to change.
While this isn't a massive problem, the application I'm developing doesn't allow me to easily change its server name settings, hence why I can't use 'database'.
What is causing this? Do I need to completely reset docker compose for changes to docker-compose.yml to take effect? What are the commands to do so?
Edit: My current docker-compose.yml is:
version: "3"
services:
webserver:
build:
context: ./bin/${PHPVERSION}
container_name: ${PHPVERSION}
restart: 'always'
ports:
- "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
- "${HOST_MACHINE_SECURE_HOST_PORT}:443"
network_mode: host
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
database:
build:
context: "./bin/${DATABASE}"
container_name: 'database'
restart: 'always'
ports:
- "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'phpmyadmin'
environment:
PMA_HOST: database
PMA_PORT: 3306
PMA_USER: ${MYSQL_USER}
PMA_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- '8080:80'
volumes:
- /sessions
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
redis:
container_name: 'redis'
image: redis:latest
ports:
- "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"
However if I try to connect via localhost like this:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
It's the service name in docker-compose.yml
which is database
. https://github.com/sprintcube/docker-compose-lamp/blob/master/docker-compose.yml#L19
Changing container name won't affect the connection string.
If you will change the service name
on that Line, make sure you change the --links
section as well in the docker-compose.
https://github.com/sprintcube/docker-compose-lamp/blob/master/docker-compose.yml#L38
and PMA_HOST
as well.
Ref: https://github.com/sprintcube/docker-compose-lamp/blob/master/docker-compose.yml#L40
Once all changes are done, Execute the below command.
docker-compose up --build --remove-orphans
In case, you face any error, please run this command
docker-compose down
and then run the first command again.
Updated Docker-compose.yml to be able to connect via localhost
version: "3"
services:
webserver:
build:
context: ./bin/${PHPVERSION}
container_name: ${PHPVERSION}
restart: 'always'
ports:
- "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
- "${HOST_MACHINE_SECURE_HOST_PORT}:443"
network_mode: host # Changed
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
database:
build:
context: "./bin/${DATABASE}"
container_name: 'database'
restart: 'always'
ports:
- "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'phpmyadmin'
links:
- database
environment:
PMA_HOST: database
PMA_PORT: 3306
PMA_USER: ${MYSQL_USER}
PMA_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- '8080:80'
volumes:
- /sessions
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
redis:
container_name: 'redis'
image: redis:latest
ports:
- "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.