I'm currently learning laravel and new to docker.
I have MySQL previously installed and wanted to run a server using docker and adminer with docker-compose.yml
below
version: "3.9"
services:
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
I can successfully log in to localhost:8080 and able to create new database in adminer. But the problem is that I cannot have database from my previous project in locally installed MySQL.
I want to connect MySQL databases (or directory) in docker so it refers to MySQL installed in my computer.
I ran the server and try migrating from cli using php artisan migrate
with database name aan
, it created new database. However, the database is not showing in docker.
here's the output in cli and dbeaver as expected
mysql> SHOW DATABASES
-> ;
+--------------------+
| Database |
+--------------------+
| aan |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
and here's the databases in adminer (I also tried creating test1
in adminer but it doesn't show in cli)
sorry I don't have enough amount of reputation point to post the images instead of links
To persist data on docker containers you can use bind mounts. Meaning you will mount a directory from localhost in the container. In your example if you bind mount /path/to/local/directory/mysql
to /var/lib/mysql
you can persist mysql data between mysql container removal/creations.
services:
mysql:
image: mysql
volumes:
- type: bind
source: /path/to/local/directory/mysql
target: /var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
Another issue that seems you have as mentioned in the comments, you seem to have a mysql service running on your local pc. In order to use the same port you either need to stop the local mysql service and then do the docker-compose up
or you need to change the localport in the docker-compose.yml:
services:
mysql:
image: mysql
volumes:
- type: bind
source: /path/to/local/directory/mysql
target: /var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3307:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
Now with the new docker-compose.yml the mysql service inside the container will be available on localhost:3307
. You just need to configure your project to connect to mysql on that host/port combination and then do the migrate command again.