DEV Community

Ibrar Hussain
Ibrar Hussain

Posted on

Setup Docker Container for MySQL to use it with Laravel app

This article is the continuation of my previous article in which we setup docker container for MailHog to use it with your Laravel app.

In this article, I am going to explain how to add new container for MySQL 8.0 that can be used with your Laravel app.

After adding MySQL container, your final configuration should look like this:

version: "3.7"
services:

  # SMTP Server
  smtp:
    platform: linux/x86_64
    image: mailhog/mailhog
    container_name: docker-workspace-smtp
    logging:
      driver: 'none'
    ports:
      - "8003:1025"
      - "8100:8025"
    networks:
      - docker_workspace_network

  # MySQL Service
  db:
    platform: linux/x86_64
    image: mysql:8.0.28
    container_name: mysql-8
    environment:
      #MYSQL_USER: 'foo'
      MYSQL_ROOT_PASSWORD: 'secret'
    ports:
      - 3307:3306
    volumes:
      - docker_workspace_mysql:/var/lib/mysql
    networks:
      - docker_workspace_network

volumes:
  docker_workspace_mysql:
    external: false

networks:
  docker_workspace_network:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

To setup your Laravel app to use this MySQL container, open your .env file and change the DB credentials to the following:

DB_HOST=127.0.0.1
DB_DATABASE=db-name-here
DB_USERNAME=root
DB_PASSWORD=secret
DB_PORT=3307
Enter fullscreen mode Exit fullscreen mode

Now, open your Laravel app and now it should be connected to MySQL from docker container

Top comments (0)