Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Set Up mysql db environment

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?

like image 518
mY777 Avatar asked Aug 15 '26 15:08

mY777


2 Answers

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 # [...]
like image 94
Jim Wright Avatar answered Aug 17 '26 23:08

Jim Wright


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
like image 34
questionto42standswithUkraine Avatar answered Aug 17 '26 23:08

questionto42standswithUkraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!