5

I have a docker-compose file with more than one service(WordPress,nocodb) required a mysql database, but I don't want to create individual containers(mysql) for each services. I'm planing to use a single mysql container with multiple users. So, how can add multiple users using docker-compose

3
  • The whole point of docker-compose is to assemble multiple out-of-the-box images into a "cluster" as needed, without tweaking each image. Running multple services in a single container is certainly possible and easy to do, but you would be missing the point of docker-compose. Commented Feb 1, 2022 at 17:19
  • It's a bit hard to understand your question. You want one single container on the machine to be re-used by all your docker-compose stacks? I don't want to create individual containers(mysql) for each services <- This doesn't make much sense. You would have one db service for all the other containers to use normally. Commented Feb 1, 2022 at 17:19
  • i want to use one sql container to run multiple services Commented Feb 1, 2022 at 17:38

1 Answer 1

5

First of all the whole concept of docker is to isolate each service into separate containers.

However, by using a volume to add sql files into the /docker-entrypoint-initdb.d folder, all the .sql files would be called by the mysql image entrypoint script.

version: '3'

services:
  db:
    image: mysql:5.7
    volumes:
      - ./<your-path>/init:/docker-entrypoint-initdb.d

You could then add a file ./<your-path>/init/01-users.sql

# create your root user
CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
# create other users
CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';
# grand appropriated rights

and a file ./<your-path>/init/02-databases.sql

# create databases
CREATE DATABASE IF NOT EXISTS `<first>`;
CREATE DATABASE IF NOT EXISTS `<second>`;
Sign up to request clarification or add additional context in comments.

1 Comment

is there is any way to connect containers running with docker-compose to mysql localhost without exposing ip to public

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.