简体   繁体   中英

docker compose - mysql - password

I have docker-compose file

version: '3.5'

services:
  db:
    image: itaybeyder/mysql:2
    # secrets:
    #   - MYSQL_ROOT_PASSWORD
    hostname: mysql
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: run/secrets/MYSQL_ROOT_PASSWORD.txt

      MYSQL_DATABASE: crud
    networks:
        - app
    ports:
      - "3306:3306"
    command: --init-file /data/application/crud.sql

    volumes:
      - ./data/crud.sql:/data/application/crud.sql
      - ./data/MYSQL_ROOT_PASSWORD.txt:/run/secrets/MYSQL_ROOT_PASSWORD.txt
networks:
    app:
        external: false

# secrets:
#   MYSQL_ROOT_PASSWORD:
#     external: true

and in the volume ( local folder in my computer ) i have password for mysql ROOT

volumes:
      
      - ./data/MYSQL_ROOT_PASSWORD.txt:/run/secrets/MYSQL_ROOT_PASSWORD.txt

i'm trying to pass it to the env - MYSQL_ROOT_PASSWORD, but im getting error:

mysql  | 2021-12-26 16:27:10+00:00 [ERROR] [Entrypoint]: Both MYSQL_ROOT_PASSWORD and MYSQL_ROOT_PASSWORD_FILE are set (but are exclusive)

when im using docker stack its working but i want to use it with docker-compose.

MY QUESTION : how can i pass, password of the ROOT with the ENV

environment: MYSQL_ROOT_PASSWORD

with file that will be sitting in the container himself,, ? im coping file from the host to the container and i want him to read if from the local path , is it possible ?

other way , simplest, how can i transfer local password i have in the host to the ENV of MYSQL_ROOT_PASSWORD?

You can create a .env file at same directory of your docker-compose.yaml file.

.env :

MYSQL_ROOT_PASSWORD=SUPER_STRONG_PASSWORD

Then at your docker-compose.yaml you can specify it like this:

    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

You can use docker-compose config to check if your configuration was applied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM