Home > OS >  Can't run MYSQL container using docker-compose
Can't run MYSQL container using docker-compose

Time:01-11

I'm trying to run a MYSQL container for my project using docker-compose.

When I use the following command, everything works fine:

docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:latest

But when I built this docker-compose file:

version: '3.8'
services:
  mysql-db:
    container_name: mysql-db
    image: mysql:latest
    environment:
      - MYSQL_DATABASE='crm'
      - MYSQL_ROOT_PASSWORD='password'
    expose:
      - "3306"
    ports:
      - "3306:3306"

The command:

docker-compose up --build

I got this message error:

mysql-db | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock' mysql-db | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. mysql-db | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it. mysql-db | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it. mysql-db | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. mysql-db
| Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. mysql-db | ERROR 1064 (42000) at line 5: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1 mysql-db exited with code 1

I also notice that there is this log right here says that I didn't set the MYSQL_ROOT_PASSWORD although I've already declared that

mysql-db | 2023-01-10T07:29:03.521464Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

I'm extremely new to docker so hope you guys can help me.

CodePudding user response:

I tried to reproduce with your compose file locally on my machine and it resulted into same error.

Only solution for this problem currently lies in the link shared by @SelVazi. We would need to build your custom mysql image and then use that to create mysql container.

Here is an example

Create a Dockerfile

I picked mysql image with tag 8 as I am running on Mac M1.

FROM mysql:8.0.31-debian

RUN apt-get update \
 && apt-get install --no-install-recommends -y tzdata \
 && apt-get clean \
 && rm -r /var/lib/apt/lists/*

Create Docker Compose file

version: '3.8'
services:
  mysql-local:
    build: .
    image: mysql-local:latest
    ports:
      - "3307:3307"
    container_name: mysql-local
    # I am running on Mac M1 Apple Silicon, You might not need it.
    platform: linux/x86_64
  mysql-db:
    container_name: mysql-db
    image: mysql-local:latest
    depends_on:
      - mysql-local
    environment:
      - MYSQL_DATABASE='crm'
      - MYSQL_ROOT_PASSWORD='password'
    expose:
      - "3306"
    ports:
      - "3306:3306"

Run compose file

docker-compose -f mysql-compose.yaml up --build
  • Related