Docker Set Up mysql db environment
Asked Answered
D

2

3

Hello guys im setting up an new wordpress docker machine im on the point to configure my sql db :

  db:
    build:
      context: ./Docker/mysql
      dockerfile: Dockerfile
    container_name: mysql
    volumes:
       - db_data:/var/lib/mysql
    environment:
        MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
        MYSQL_DATABASE: "${DB_NAME}"
        MYSQL_USER: "${DB_USERNAME}"
        MYSQL_PASSWORD: "${DB_PASSWORD}"
    networks:
      - back

Dockerfile for sql:

FROM mysql:latest

this also works fine problem is i don't realy know where i should set the environment props like db name user and so on. any suggestions?

Darnel answered 17/1, 2018 at 11:1 Comment(0)
L
12

You could set that as environment vars on the server, but if you don't want to do that you could set them for the compose command:

DB_ROOT_PASSWORD=asdf DB_NAME=mydb DB_USERNAME=user DB_PASSWORD=pw docker compose

In the comments you said that you wanted to set the vars in the Dockerfile. Below is an example:

FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD=asdf
ENV MYSQL_DATABASE=mydb
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=pw

If you do not want these hardcoded in the Dockerfile, you could combine them with build time arguments.

FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD=$db_root_pw
ENV MYSQL_DATABASE=$db_name
ENV MYSQL_USER=$db_username
ENV MYSQL_PASSWORD=$db_pw

Make sure to include the arguments when building the image:

docker build --build-arg db_root_pw=rootpw --build-arg db_name=mydb --build-arg db_username=user --build-arg db_pw=pw # [...]
Lapsus answered 17/1, 2018 at 11:6 Comment(2)
i owuld like to store them in my dockerfile where i do get my sql version "FROM mysql:latest" if possibleDarnel
Show your Dockerfile in the question.Lapsus
B
3

Instead of environment, use:

        env_file:
          - env_file.env

and add the env_file.env (or "whateverfilename.env") to your project directory with the following content:

MYSQL_ROOT_PASSWORD=myrootpw
MYSQL_DATABASE=mydb
MYSQL_USER=myuser
MYSQL_PASSWORD=mypw
MYSQL_HOST=myhost
Berns answered 4/1, 2022 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.