Build docker image run normally but push then pull it back run failed

I have a docker file callflow_database/dockerfile like this:

FROM mysql:8.2.0

WORKDIR /db
COPY init.sql /docker-entrypoint-initdb.d/

# Update my.cnf for more detailed error logging
RUN sed -i 's/bind-address\s*=\s*127\.0\.0\.1/bind-address = 0.0.0.0/' /etc/my.cnf && \
    sed -i '/^#\s*log_error/s/^# //' /etc/my.cnf || \
    echo "log_error = /var/log/mysql/error.log" >> /etc/my.cnf

and a docker compose file like this:

  callflow_database:
    container_name: callflow_database
    ports:
      - 3307:3306
    restart: unless-stopped
    healthcheck:
         test: ["...."]
         timeout: 20s
         interval: 30s
         start_period: 300s
         retries: 5
    build: ./callflow_database

When i run it with docker compose. It run normally. But when i try to push the image to docker hub and rebuild it again with another compose_production.yaml file like this:

  callflow_database:
    image: yukari268/primasbot:callflow_database_JAN
    container_name: callflow_database
    ports:
      - 3307:3306
    restart: unless-stopped
    healthcheck:
         test: ["..."]
         timeout: 20s
         interval: 30s
         start_period: 300s
         retries: 5

I got this error:

callflow_database     | 2024-01-19T04:54:10.518204Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
callflow_database     | 2024-01-19T04:54:10.518213Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
callflow_database     | 2024-01-19T04:54:10.524279Z 0 [ERROR] [MY-010119] [Server] Aborting

Don’t know about the error, how did you tag and push?

Still want to leave some remarks:

  • Why do you change the port with sed? I would expect it to be default to bind to 0.0.0.0.

  • You are logging inside the container, that is rather unusual. The container will grow and when you re-do the container all logs are gone, so you can’t check anymore.

  • Furthermore the database will create data files inside container, same issue. That way you can very easily lose your database content, rather use bind mount or volume (doc).

I hope you didn’t commit a running MySQL container as an image and pushed that.

  1. Somehow when I don’t set that value other services can ping to my database but cannot connect to my database. I always got CONNECTION REFUSE
  2. I can bind the logs folder with the host directory so I think it’s fine
  3. Yes i do use volume i just didn’t show it in my code snippet because i didn’t want to include irrelevant information but apparently it’s the cause. Because when i remove the volume options. then my container works again.

No. I don’t i have a separated shell file. Go to every single folder , build the image and then commit the image to my repo.