Skip to content

Instantly share code, notes, and snippets.

@jcavat
Last active November 14, 2024 21:19
Show Gist options
  • Save jcavat/2ed51c6371b9b488d6a940ba1049189b to your computer and use it in GitHub Desktop.
Save jcavat/2ed51c6371b9b488d6a940ba1049189b to your computer and use it in GitHub Desktop.
docker-compose with php/mysql/phpmyadmin/apache
Create this directories structure:
.
├── docker-compose.yml
├── Dockerfile
├── dump
│   └── myDb.sql
├── sessions
└── www
└── index.php
version: "2"
services:
www:
build: .
ports:
- "8001:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
FROM php:7.1.2-apache
RUN docker-php-ext-install mysqli
<!-- put in ./www directory -->
<html>
<head>
<title>Hello...</title>
<meta charset="utf-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php echo "<h1>Hi! I'm happy</h1>"; ?>
<?php
$conn = mysqli_connect('db', 'user', 'test', "myDb");
$query = 'SELECT * From Person';
$result = mysqli_query($conn, $query);
echo '<table class="table table-striped">';
echo '<thead><tr><th></th><th>id</th><th>name</th></tr></thead>';
while($value = $result->fetch_array(MYSQLI_ASSOC)){
echo '<tr>';
echo '<td><a href="#"><span class="glyphicon glyphicon-search"></span></a></td>';
foreach($value as $element){
echo '<td>' . $element . '</td>';
}
echo '</tr>';
}
echo '</table>';
$result->close();
mysqli_close($conn);
?>
</div>
</body>
</html>
-- put in ./dump directory
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
CREATE TABLE `Person` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Person` (`id`, `name`) VALUES
(1, 'William'),
(2, 'Marc'),
(3, 'John');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
@CharlesDerek
Copy link

Although I really believe that the decision of the MySQL maintainers is wrong, I do believe that they are not going to change it back. So, I verified out how it can work without downgrading. It is a bit more of work, but needs to be done only once:

  1. Change the Dockerfile to read

FROM php:7.2.6-apache

  1. Bring everything up, then login into the mysql container:

$ docker exec -t -i container_mysql_name /bin/bash

  1. Change the password storage for the user of interest:

#mysql -uroot -p [isempty]
mysql> ALTER USER 'user' IDENTIFIED WITH mysql_native_password BY 'test';

  1. Change/Create all users you need with the option "mysql_native_password"
  2. logout

This works fine for me, as the mysql data is persistent, and you can use the latest versions of the packages.

I followed your instructions but when i try logging into mysql as root with
~ mysql -uroot -p
Attempt 1: (blank)
returned: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
Attempt 2: test (the password in yml file for root)
returned: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Can't get into mysql directly to change settings as root.

@tjarbo
Copy link

tjarbo commented Nov 11, 2019

Although I really believe that the decision of the MySQL maintainers is wrong, I do believe that they are not going to change it back. So, I verified out how it can work without downgrading. It is a bit more of work, but needs to be done only once:

1. Change the Dockerfile to read

FROM php:7.2.6-apache

1. Bring everything up, then login into the mysql container:

$ docker exec -t -i container_mysql_name /bin/bash

1. Change the password storage for the user of interest:

#mysql -uroot -p [isempty]
mysql> ALTER USER 'user' IDENTIFIED WITH mysql_native_password BY 'test';

1. Change/Create all users you need with the option "mysql_native_password"

2. logout

This works fine for me, as the mysql data is persistent, and you can use the latest versions of the packages.

A other solution that works for me is to add following line into docker-compose.yml

db:
        ....
        command: mysqld --default-authentication-plugin=mysql_native_password
        .....

@goodboy761
Copy link

Hello, do you know how to configure additional password protection phpmyadmin at apache level?

@spoddar13
Copy link

Solved

Dockerfile -->
FROM php:8.0-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y

Docker-Composer-->
version: "2"
services:
www:
build: .
container_name: webhost
ports:
- "8001:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql
restart: always
container_name: DataBaseServer
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: PHP_MySQL
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:

@Alldbg
Copy link

Alldbg commented May 25, 2022

Bellow my fix to resolve connection issues ,

FROM php:7.2-apache
RUN a2enmod rewrite
RUN docker-php-ext-install pdo pdo_mysql
RUN apt-get update
&& apt upgrade -y
&& apt-get install -y libzip-dev
&& apt-get install -y zlib1g-dev
&& apt-get install -y iputils-ping
&& apt-get install -y mycli
&& rm -rf /var/lib/apt/lists/*
&& docker-php-ext-install zip
&& docker-php-ext-install mysqli
&& docker-php-ext-enable mysqli

@yohanes-ai
Copy link

yohanes-ai commented Nov 21, 2022

if someone had a problem with Laravel with an error
could not find driver (SQL: select * from

don't forget to add pdo pdo_mysql after RUN docker-php-ext-install

so it become

FROM php:8.0.2-apache 
RUN docker-php-ext-install mysqli pdo pdo_mysql

to Dockerfile

@adhirmndl
Copy link

where is access and error log path

@varaskkar
Copy link

Dockerfile

FROM php:8-apache

RUN apt-get update && apt-get upgrade -y
RUN apt-get install sudo unzip wget -y
RUN docker-php-ext-install mysqli
 
RUN a2enmod rewrite
RUN a2enmod ssl
RUN service apache2 restart
 
EXPOSE 80

docker-compose.yml

version: "3.6"
services:
    php:
        build: .
        restart: always
        ports:
            - "8080:80"
        volumes:
            - ./src:/var/www/html
            - ./log:/var/log/apache2
    mysql:
        image: mysql:8.0
        restart: always
        ports:
            - "3306:3306"
        command: --default-authentication-plugin=mysql_native_password
        environment:
            - MYSQL_DATABASE=myDb
            - MYSQL_USER=user
            - MYSQL_PASSWORD=test
            - MYSQL_ROOT_PASSWORD=test
        volumes:
            - ./dump:/docker-entrypoint-initdb.d
            - ./conf:/etc/mysql/conf.d
            - persistent:/var/lib/mysql
    phpmyadmin:
        image: phpmyadmin
        restart: always
        ports:
            - 8081:80
        environment:
            - PMA_HOST=mysql
volumes:
    persistent:

Create this structure:

├── docker-compose.yml
├── Dockerfile
├── /dump
│   └── myDb.sql
├── /log
└── /src
    └── index.php

And run "docker-compose up". Then go to localhost:8080 and you should see a table with the data loaded from the myDb.sql file.

You can also go to localhost:8081 and log in to the database with phpmyadmin.

Server: mysql
User: user
Password: test
Database: myDb

@asiandevs
Copy link

Hi @varaskkar - Thank you.

I think, a small correction. Tree structure would be for this one is as below —

Before executing docker-compose [no need to create directory | or if you create then not "/"]
[root@ip-10-0-8-114 ~]# tree dockercompose03
dockercompose03
├── conf
├── docker-compose.yml
├── dockerfile
├── dump
│ └── myDb.sql
└── www
└── index.php

After executing docker-compose up
[root@ip-10-0-8-114 ~]# tree dockercompose03
dockercompose03
├── conf
├── docker-compose.yml
├── dockerfile
├── dump
│ └── myDb.sql
├── log
│ ├── access.log
│ ├── error.log
│ └── other_vhosts_access.log
├── src
└── www
└── index.php

5 directories, 7 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment