Integrate VW in exisiting mariadb container

Hey so I’m using nginx pm which already uses an mariadb so I want vaultwarden to be managed by that one as well.

I’m currently receiving this error message when I want to deploy the container:

Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
vaultwarden is up-to-date
Recreating c306e8f58c71_mariadb-vaultwarden ... error

ERROR: for c306e8f58c71_mariadb-vaultwarden  Cannot start service mariadb: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/var/lib/docker/volumes/localtime/_data" to rootfs at "/etc/localtime": mount /var/lib/docker/volumes/localtime/_data:/etc/localtime (via /proc/self/fd/6), flags: 0x5001: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

ERROR: for mariadb  Cannot start service mariadb: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/var/lib/docker/volumes/localtime/_data" to rootfs at "/etc/localtime": mount /var/lib/docker/volumes/localtime/_data:/etc/localtime (via /proc/self/fd/6), flags: 0x5001: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: Encountered errors while bringing up the project.

this is my docker-compose.yml

version: "3.7"
services:
 mariadb:
  image: "mariadb"
  container_name: "mariadb-bw"
  hostname: "mariadb"
  restart: always
  env_file:
   - ".env"
  volumes:
   - "/var/lib/docker/volumes/mariadb/_data:/var/lib/mysql"
   - "/var/lib/docker/volumes/localtime/_data:/etc/localtime:ro"
  environment:
   - "MYSQL_ROOT_PASSWORD=changeme"
   - "MYSQL_PASSWORD=foo_bar"
   - "MYSQL_DATABASE=vaultwarden"
   - "MYSQL_USER=js"

 vaultwarden:
  image: "vaultwarden/server:latest"
  container_name: "vaultwarden"
  hostname: "vaultwarden"
  restart: always
  env_file:
   - ".env"
  volumes:
   - "/var/lib/docker/volumes/vaultwarden/_data:/data/"
  environment:
## Had issues when using single parentheses around the mysql URL as in the plain docker example 
   - "DATABASE_URL=mysql://js:foo_bar@mariadb/vaultwarden"
   - "ADMIN_TOKEN=foo_bar"
   - "RUST_BACKTRACE=1"
  ports:
   - "36549:80"

volumes:
 vaultwarden_vol:
 mariadb_vol:

docker version

Client: Docker Engine - Community
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:28:22 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:26:14 2022
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          1.6.14
  GitCommit:        9ba4b250366a5ddde94bb7c9d1def331423aa323
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Thanks for your help!

Seems like VW is running but the database is broken.
Here is the log of the VW container:

[CAUSE] BadConnection(

    "Unknown MySQL server host 'mariadb' (-2)",

)

/--------------------------------------------------------------------\

|                        Starting Vaultwarden                        |

|                           Version 1.26.0                           |

|--------------------------------------------------------------------|

| This is an *unofficial* Bitwarden implementation, DO NOT use the   |

| official channels to report bugs/features, regardless of client.   |

| Send usage/configuration questions or feature requests to:         |

|   https://vaultwarden.discourse.group/                             |

| Report suspected bugs/issues in the software itself at:            |

|   https://github.com/dani-garcia/vaultwarden/issues/new            |

\--------------------------------------------------------------------/

[INFO] No .env file found.

[2023-01-09 11:46:13.022][vaultwarden::util][WARN] Can't connect to database, retrying: DieselCon.

[CAUSE] BadConnection(

    "Unknown MySQL server host 'mariadb' (-2)",

)

[2023-01-09 11:46:19.474][vaultwarden::util][WARN] Can't connect to database, retrying: DieselCon.

[CAUSE] BadConnection(

    "Unknown MySQL server host 'mariadb' (-2)",

)

[2023-01-09 11:46:20.587][vaultwarden::util][WARN] Can't connect to database, retrying: DieselCon.

[CAUSE] BadConnection(

    "Unknown MySQL server host 'mariadb' (-2)",

)

How did you start the existing mariadb container? You have to make sure that the containers are in the same network. By default containers in a docker-compose.yml are started in their own network. If you have an existing container you need to be more explicit about this:

1 Like

Thanks for your reply! I actually start every docker-compose.yml with their more or less recommended template without any network specified. My npm and mariadb are running in the nginx-pm_default network with their default ports.

Okay. Also I forgot to mention that it’s probably not a good idea to start a new container with the same volumes mounted and at least performance-wise I don’t think there’s a benefit of trying to have two mariadb servers accessing the same data set (rather there’s probably the possibility of data corruption if you can manage to get it to work).

So (assuming that this is what’s happening here) you should either remove the mariadb service completely (and link the existing container/network) or start a separate mariadb container with it’s own volume to keep the services separate.

This is actually what I’m trying to achieve but since I’m a beginner I don’t know how to change the docker-compose.yml accordingly. Do you know how to do that?

I thought about the depends on funtion but this is apparently only for the compose file.
This is my docker-compose.yml for the nginx proxy manger which uses mariadb.
Sadly ChatGPT is not useable for me right now so I can’t get any help there.


version: "3"
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    container_name: nginx-pm
    restart: unless-stopped
    ports:
      # These ports are in format <host-port>:<container-port>
      - '80:80' # Public HTTP Port
      - '443:443' # Public HTTPS Port
      - '81:81' # Admin Web Port
      # Add any other Stream port you want to expose
      #- '58120:58120' # Wireguard
    environment:
      DB_MYSQL_HOST: ""
      DB_MYSQL_PORT: XXXX
      DB_MYSQL_USER: ""
      DB_MYSQL_PASSWORD: ""
      DB_MYSQL_NAME: ""
      # Uncomment this if IPv6 is not enabled on your host
      DISABLE_IPV6: 'true'
    volumes:
      - /var/lib/docker/volumes/nginx-pm/_data:/data
      - /var/lib/docker/volumes/letsencrypt/_data:/etc/letsencrypt
    depends_on:
      - db

  db:
    image: 'jc21/mariadb-aria:latest'
    container_name: mariadb
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ''
      MYSQL_DATABASE: ''
      MYSQL_USER: ''
      MYSQL_PASSWORD: ''
    volumes:
      - /var/lib/docker/volumes/mariadb/_data:/var/lib/mysql

credentials are censored

I think by default Docker Compose calls the network <folder_name>_default (you should be able to confirm this by running docker network ls) so you could use that as a pre-existing network.The hostname of the mariadb container should just be resolvable by db once you link the vaultwarden container to the pre-existing network.

You should also be able to add a new user/database to the running mariadb container.

1 Like

Thanks for your help Stefan!
I tried to change the docker-compose.yml but I’m having some problems with YAML sometimes. Do you know what’s my problem when I get the following error message:
In file './docker-compose.yml', network 'image' must be a mapping not a string.

As shown in the docker wiki example
image
I added the section above to my mariadb section.

version: "3.7"
services:
 mariadb:
  image: "mariadb"
  container_name: "mariadb-bw"
  hostname: "mariadb"
  restart: always
  env_file:
   - ".env"
  volumes:
   - "/var/lib/docker/volumes/mariadb/_data:/var/lib/mysql"
   - "/var/lib/docker/volumes/localtime/_data:/etc/localtime:ro"
  environment:
   - "MYSQL_DATABASE=vaultwarden"
   - "MYSQL_PASSWORD=xxx"
   - "MYSQL_USER=vw"
networks:
  network1:
    name: "nginx-pm_default"
    external: true
 
  vaultwarden:
  image: "vaultwarden/server:latest"
  container_name: "vaultwarden"
  hostname: "vaultwarden"
  restart: always
  env_file:
   - ".env"
  volumes:
   - "/var/lib/docker/volumes/vaultwarden/_data:/data/"
  environment:
## Had issues when using single parentheses around the mysql URL as in the plain docker example 
   - "DATABASE_URL=mysql://js:foo_bar@mariadb/vaultwarden"
   - "ADMIN_TOKEN=foo_bar"
   - "RUST_BACKTRACE=1"
  ports:
   - "XXX:80"

volumes:
 vaultwarden_vol:
 mariadb_vol:

I’m unsure how I would add another user to the db. Does this happen automatically when I enter other credentials like in this compose file?