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?

2 Answers 2

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 # [...]
Sign up to request clarification or add additional context in comments.

2 Comments

i owuld like to store them in my dockerfile where i do get my sql version "FROM mysql:latest" if possible
Show your Dockerfile in the question.
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

Comments

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.