skip to Main Content

I have a issue while developing using Laravel in Windows 10 with WSL & docker since I reinstalled Ubuntu on WSL.

If I run any command that requires DB access in the terminal such as php artisan migrate I get the following error:

 IlluminateDatabaseQueryException

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known (Connection: mysql, SQL: select * from information_schema.tables where table_schema = test and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +39 vendor frames

  40  artisan:35
      IlluminateFoundationConsoleKernel::handle()

I can fix this error by changing the value of DB_HOST in my .env file to 127.0.0.1 however this then breaks the application in the browser as this works with the standard DB_HOST=mysql. This is getting a little annoying to have to keep changing this value, how can I fix this issue?

Many thanks.

2

Answers


  1. You can not run artisan commands in your linux terminal since your laravel app is inside a docker container. In order to run any commands related to your app you must access your container first.

    First step is to get a list of your containers, you can do that by running in your linux terminal the command docker ps.

    This will list you all the containers you got running.

    Now in order to access a container you need the name of it, you can find if from your docker-compose.yml file you got OR you can use the container_id.

    In order to access a container you use:

    docker exec -it <container> bash

    or

    docker exec -it <container> sh

    Now you should see in your terminal that you are in a new environment, you are inside the container. Inside there you can run all the artisan commands or composer commands or any command related to your project.

    Your DB_HOST should stay as mysql this is the current IP of the mysql container you have, so don’t change that.

    Login or Signup to reply.
  2. Solution

    Set <container-name> name to your mysql container, it work around as an alias name, and you and access the container with <container-name> instead of <container-id>, even in cli.

    Once you set done, it can also alias as IP to container, you can set DB_HOST as container name, it will alias to container ip which docker assigned.

    Docker compose

    Docker compose will auto prefix container name with folder name and service name.

    For example:

    .
    ├── ...
    └── project
        ├── ...
        └── docker-compose.yml
    

    If your service named mysql, it will build up container with a container name project-mysql-1 as default.

    Solve by add container_name option.

    Add container_name to your mysql service, then you set .env to DB_HOST=mysql to work around.

    version: '3.5'
    
    services:
      mysql:
        image: mysql
        container_name: mysql # Set container name with this
        networks:
          - backend
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: root
        volumes:
          - ./mysql-data:/var/lib/mysql
    networks:
      backend:
        driver: bridge
    

    Docker cli

    If you are using docker run, it won’t set container name if you didn’t specify, you can only access from the <container-id>.

    Solve by use --name flag.

    Run up container with command like docker run mysql --name=mysql.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search