0

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>

dbeaver preview

and here's the databases in adminer (I also tried creating test1 in adminer but it doesn't show in cli)

adminer preview

Edit1:

sorry I don't have enough amount of reputation point to post the images instead of links

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • If you want to connect to the MySQL instance on the host from Adminer (in Docker) you need to specify `host.docker.internal` as the hostname. Currently, you have `3306:3306` as the port mapping for your Docker MySQL instance, but `3306` is already being used by your local instance of MySQL. If you want your local Laravel project to connect to your Docker MySQL instance you need to map the Docker MySQL instance to a port not currently used - `3307:3306` and tell your Laravel project to use the new port number (3307). Removing your local (host) MySQL instance will reduce confusion. – user1191247 Jul 21 '23 at 07:50

1 Answers1

1

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.

Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70