0

I am trying to connect to a mysql database on my host using a docker container (so I can use it in other containers).

I would like to do it this way, because I cannot connect to the database from a docker container -> throws Connection refused because the IP is not allowed.

I tried mounting the sock file using following compose:

version: '3'
services:
  mysql:
    image: mariadb:10.3
    volumes:
      - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
    networks:
      - database
networks:
  database:
    external: true

but it's failing on (even though the host database already contains many databases):

2022-06-07 20:14:46+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD and MARIADB_RANDOM_ROOT_PASSWORD,

and when I added the volume: - /var/lib/mysql:/var/lib/mysql, I am getting a Connection refused error (as if I was connecting to it normally and not via unix)

Majksa
  • 26
  • 3

1 Answers1

0

Here you go, consider checking the documentation before asking thoses types of questions https://hub.docker.com/_/mariadb

version: 3

services:
  sql:
    image: mariadb:10.3
    container_name: <container_name>
    restart: always
    networks:
      - database
    volumes:
      - <volume>:/var/lib/mysql
      - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
    environment:
      MARIADB_RANDOM_ROOT_PASSWORD: 1
      MARIADB_USER: <USER_TO_BE_CREATED>
      MARIADB_DATABASE: <DATABASE_TO_BE_CREATED>
      MARIADB_PASSWORD: <PASSWORD_TO_BE_CREATED>

networks:
  database:
    external: true
SCcagg5
  • 576
  • 3
  • 15