1

I need to execute these commands on every startup since it looks to be overwritten every time I set and restart it

mysql -uroot -padmin;
set global general_log = 1;

I start the docker container with docker-compose for development purposes only and it looks like this.

version: "3.8"
services:
  mysql_service:
    container_name: db_container
    build:
        context: .
        dockerfile: ./db/Dockerfile.dev
    # needed for mysql 8+
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    hostname: db
    ports:
      - target: 3306
        published: 3306
        protocol: tcp
        mode: host
    environment:
      - MYSQL_ROOT_PASSWORD=admin
      - MYSQL_DATABASE=example
      - MYSQL_ROOT_HOST=localhost
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - ./db/:/docker-entrypoint-initdb.d/
      - data_volume:/var/lib/mysql/
    cap_add:
      - ALL
      
volumes:
  data_volume:
    driver: local

and the Dockerfile

FROM mysql:8.0

COPY ./DevOps/Docker/db/set_logging.sh /usr/local/bin
ENTRYPOINT ["docker-entrypoint.sh", "./usr/local/bin/set_logging.sh"]

However the copy goes through but the script is never executed. Where the script looks like

#!/bin/bash
mysql -uroot -padmin -e "set global general_log = 1"

Any suggestions on getting this command to go through? This is only for development

2
  • 2
    Not the only option but the easiest here. Drop all of what you've done to try to fix this and simply add --general-log=1 to your service command in your docker-compose file. To see a list of all config option you can set on the docker command line for mysql:8.0 without having to push a custom config to your container: docker run -it --rm mysql:8.0 --verbose --help. The official mysql docker image documentation has much more info on how to customize your container for various use cases. Commented Dec 3, 2021 at 16:24
  • Oh man I didn't know you could have multiple commands on the - command section of a docker-compose file. Commented Dec 3, 2021 at 16:40

1 Answer 1

0

In order to tell MYSQL container to run that script once it starts you can either mount the script into the image's /docker-entrypoint-initdb.d folder using docker file, or docker-compose file using bind mount volume.

Dockerfile

FROM mysql:8.0
COPY ./DevOps/Docker/db/script.sh /docker-entrypoint-initdb.d/script.sh
ENTRYPOINT ["docker-entrypoint.sh"]

docker-compose

version: "3.8"
services:
  mysql_service:
    container_name: db_container
    build:
        context: .
        dockerfile: ./db/Dockerfile.dev
    # needed for mysql 8+
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    hostname: db
    ports:
      - target: 3306
        published: 3306
        protocol: tcp
        mode: host
    environment:
      - MYSQL_ROOT_PASSWORD=admin
      - MYSQL_DATABASE=example
      - MYSQL_ROOT_HOST=localhost
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - "./DevOps/Docker/db/script.sh:/docker-entrypoint-initdb.d/script.sh"  
      - data_volume:/var/lib/mysql/
    cap_add:
      - ALL
      
volumes:
  data_volume:
    driver: local

You can check how scripts are launched in /docker-entrypoint-initdb.d read the Initializing a fresh instance Section

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried having set global general_log = 1; in the initdb.d script and that doesn't work. I have in that file all the initialization of my DB. This answer is also something already implemented in my code as you can see.
This only runs the first start-up, not every start-up, like OP asks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.