question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can't connect db in container (node 16)

See original GitHub issue

Discussed in https://github.com/prisma/prisma/discussions/14970

<div type='discussions-op-text'>

Originally posted by Atsuyoshi-Funahashi August 24, 2022 Hi. I am currently creating an application in Next.js and using mysql for the DB. I am building the environment with docker. Originally I was using node version 17.6.0 and it was working fine.

Problem

I have lowered my node version to 16.13.0 in order to install NextAuth. After that, when I launch the container with docker compose up --build, the following error occurs during migration.

# error
Error: P1001: Can't reach database server at `mysql`:`3306`
Please make sure your database server is running at `mysql`:`3306`

Since this did not occur when node version was 17, I would like to know how to solve this problem.

Version

node  16.13.0
prisma 4.2.1
mysql 5.7

reproduction method

1  create next app
2 install and init prisma
3 Copy-paste the file below.
4 docker compose up --build

File

Dockerfile, docker-compose.yml, run.sh, and .env are as follows

## Dockerfile
FROM node:16.13.0

RUN mkdir /test
WORKDIR /test
COPY . /test
RUN yarn add create-next-app
COPY run.sh /usr/bin/run
RUN chmod +x /usr/bin/run
EXPOSE 3000

CMD ["run"]
## docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5
    platform: linux/x86_64
    container_name: mysql
    ports:
      - '3306:3306'
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - ./tmp/db:/var/lib/mysql
  app:
    build: .
    volumes:
      - .:/test
    ports:
      - '3000:3000'
    restart: always
    depends_on:
      - db

This is schema.prisma.

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model users {
  id               Int       @id @default(autoincrement())
  email            String    @unique
 # other value
}

This is seed.ts.

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
    await prisma.users.createMany({
        data: [
            {
                email: 'test1@example.com',
            },
            {
                email: 'test2@example.com',
            },
        ],
    });
}

main()
    .then(async () => {
        await prisma.$disconnect();
    })
    .catch(async e => {
        console.error(e);
        await prisma.$disconnect();
        process.exit(1);
    });

This is run.sh. Please set under the project root.

# run.sh
#!/bin/sh

bash -c "sleep 30 && yarn -s run prisma migrate dev --name init && yarn install && yarn dev "

This is .env

# .env
MYSQL_HOST = 'mysql'
MYSQL_DATABASE = 'database_name'
# MYSQL_USER = 'root'
MYSQL_ROOT_PASSWORD = ''
DATABASE_URL="mysql://root:@mysql:3306/database_name"

```</div>

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
goda-kazukicommented, Sep 14, 2022

I don’t know if this is related, but I added connect_timeout and now I can connect stably.

DATABASE_URL="mysql://docker:docker@db:3306/docker?connect_timeout=30"
0reactions
Sunironcommented, Sep 15, 2022

I don’t know if this is related, but I added connect_timeout and now I can connect stably.

DATABASE_URL="mysql://docker:docker@db:3306/docker?connect_timeout=30"

What is the equivalent in PostgreSQL?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS can't connect to MySQL database [duplicate]
This is all built and used with a docker-compose.yml, and a dockerfile for nodejs. Problem is, the server can't connect to the database, ......
Read more >
NodeJS Docker container can't connect to MySQL (on host)
I'm a beginner with Docker and trying it out with Ubuntu 20.04.3 LTS , Docker 20.10.8 , MySQL 8.0.26 (installed on the host)....
Read more >
Can't reach database server which run in Docker #1385 - GitHub
I currently try run Postgresql in Docker, and use that database in prisma2 in my docker list, my Postgresql running on localhost:5555 and...
Read more >
Troubleshoot common connection issues to Azure SQL ...
These connection problems can be caused by reconfiguration, firewall settings, a connection timeout, incorrect login information, or failure to ...
Read more >
Dockerizing a Node.js Web Application - Semaphore Tutorial
Connect containers using a virtual network. Handle persistence of data using Docker Volumes. Set environment variables. Build or download ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found