search

Home  >  Q&A  >  body text

Docker: Encountered connection refused error while executing Laravel artisan command

<p>I'm running Laravel 5.4 in Docker. This is my dock composition. yml file:</p> <pre class="brush:php;toolbar:false;">version: '2' services: app: container_name: laravel_app image: webdevops/php-apache-dev:ubuntu-16.04 links: - mysql depends_on: - mysql ports: - 8888:80 volumes: - .:/app environment: docker: 'true' WEB_DOCUMENT_ROOT: '/app/public' WEB_NO_CACHE_PATTERN: '.(.*)$$' working_dir: '/app' mysql: image: mariadb:latest ports: - 8889:80 environment: MYSQL_ROOT_PASSWORD: 'dev' MYSQL_DATABASE: 'dev' MYSQL_USER: 'dev' MYSQL_PASSWORD: 'dev'</pre> <p>This is the relevant part of my <code>.env</code> file:</p> <pre class="brush:php;toolbar:false;">DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=8889 DB_DATABASE=dev DB_USERNAME=dev DB_PASSWORD=dev</pre> <p>I can see Laravel's welcome page - this is working. But when I run php artisan migrate I get this error: </p> <blockquote> <p>SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = dev and table_name = migrations)</p> </blockquote> <p>I have tried modifying the host and port parameters in the .env file. </p>
P粉787820396P粉787820396553 days ago621

reply all(2)I'll reply

  • P粉295728625

    P粉2957286252023-08-08 13:47:57

    Put the name of my mysql container instead of 127.0.0.1

    NAME CONTAINERS
    
    project-db --> container mysql
    project-app --> container laravel
    
    DB_CONNECTION=mysql
    DB_HOST=project-db
    DB_PORT=3306
    DB_DATABASE=project
    DB_USERNAME=root
    DB_PASSWORD=root

    reply
    0
  • P粉225961749

    P粉2259617492023-08-08 12:59:00

    First, edit your docker-compose.yml file

    mysql:
        image: mariadb:latest
        ports:
          - 8889:3306

    Then, set the correct database port in the .env file

    Your database port is wrong. You are trying to connect to an exposed port through a database instance running inside docker. In this case you should use DB_PORT=3306

    in the .env file

    reply
    0
  • Cancelreply