Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-compose database connection error #65

Closed
tehniemer opened this issue Aug 14, 2020 · 13 comments
Closed

docker-compose database connection error #65

tehniemer opened this issue Aug 14, 2020 · 13 comments
Labels
docker Related to deployment with Docker help wanted Extra attention is needed

Comments

@tehniemer
Copy link

tehniemer commented Aug 14, 2020

I'm getting the following error when starting the container. I am using the hostname of the database because it doesn't use a static IP. I've tried different formatting unsuccessfully .

2020/08/14 12:21:43 Could not connect to database: Could not parse mysql url: parse "photoviewdb_user:photoviewdb_pass@tcp(mariadb)/photoview": first path segment in URL cannot contain colon,
panic: Could not connect to database: Could not parse mysql url: parse "photoviewdb_user:photoviewdb_pass@tcp(mariadb)/photoview": first path segment in URL cannot contain colon
@viktorstrate
Copy link
Member

Are you using docker-compose, if so could you post your config?
If you are running the container directly from Docker, could you then please post the full command you used to run it?

@tehniemer
Copy link
Author

Sure thing, here's the snippet

## Photoview - Image Gallery
  photoview:
    image: viktorstrate/photoview:latest
    container_name: photoview
    restart: unless-stopped
    networks:
      - proxy
    depends_on:
      - mariadb
    security_opt:
      - no-new-privileges:true
    ports:
      - "$PHOTOVIEW_PORT:80"
    volumes:
      - "$DOCKER_DIR/photoview:/app/cache"
      - "$PHOTO_DIR/_EXPORTS/Collections:/photos:ro"
      - "/etc/timezone:/etc/timezone:ro"
      - "/etc/localtime:/etc/localtime:ro"
    environment:
      MYSQL_URL: "$PHOTOVIEW_MYSQL_USER:$PHOTOVIEW_MYSQL_PASSWORD@tcp($DB_HOST)/photoview"
      API_LISTEN_IP: photoview
      API_LISTEN_PORT: 80
      PHOTO_CACHE: /app/cache
      PUBLIC_ENDPOINT: "https://photoview.$DOMAINNAME/"
    labels:
      - "traefik.enable=false"
      ## HTTP Routers
      - "traefik.http.routers.photoview-rtr.entrypoints=https"
      - "traefik.http.routers.photoview-rtr.rule=HostHeader(`photoview.$DOMAINNAME`)"
      - "traefik.http.routers.photoview-rtr.tls=true"
      ## Middlewares
#      - "traefik.http.routers.photoview-rtr.middlewares=chain-no-auth@file" #No Authentication
#      - "traefik.http.routers.photoview-rtr.middlewares=chain-basic-auth@file" #Basic Authentication
#     - "traefik.http.routers.photoview-rtr.middlewares=chain-oauth@file" #Google OAuth 2.0
#      - "traefik.http.routers.photoview-rtr.middlewares=chain-organizr@file" #Organizr Authentication
      - "traefik.http.routers.photoview-rtr.middlewares=chain-authelia@file" #Authelia Authentication
      ## HTTP Services
      - "traefik.http.routers.photoview-rtr.service=photoview-svc"
      - "traefik.http.services.photoview-svc.loadbalancer.server.port=80"

@viktorstrate
Copy link
Member

It seems to have problems parsing the MYSQL_URL environment variable.

I'm not entirely sure why this happends, maybe the quotes around the MYSQL_URL value trips it up, could you try to delete them? Like so:

      - "/etc/localtime:/etc/localtime:ro"
    environment:
-     MYSQL_URL: "$PHOTOVIEW_MYSQL_USER:$PHOTOVIEW_MYSQL_PASSWORD@tcp($DB_HOST)/photoview"
+     MYSQL_URL: $PHOTOVIEW_MYSQL_USER:$PHOTOVIEW_MYSQL_PASSWORD@tcp($DB_HOST)/photoview
      API_LISTEN_IP: photoview

@tehniemer
Copy link
Author

I did try that and it behaves the same.

@viktorstrate
Copy link
Member

I read here that someone had similar problems because of whitespaces, could you try to format the config like so (again just a guess):

      - "/etc/localtime:/etc/localtime:ro"
    environment:
-      MYSQL_URL: "$PHOTOVIEW_MYSQL_USER:$PHOTOVIEW_MYSQL_PASSWORD@tcp($DB_HOST)/photoview"
-      API_LISTEN_IP: photoview
-      API_LISTEN_PORT: 80
-      PHOTO_CACHE: /app/cache
-      PUBLIC_ENDPOINT: "https://photoview.$DOMAINNAME/"
+      MYSQL_URL=$PHOTOVIEW_MYSQL_USER:$PHOTOVIEW_MYSQL_PASSWORD@tcp($DB_HOST)/photoview
+      API_LISTEN_IP=photoview
+      API_LISTEN_PORT=80
+      PHOTO_CACHE=/app/cache
+      PUBLIC_ENDPOINT=https://photoview.$DOMAINNAME/
    labels:

Hope it helps

@tehniemer
Copy link
Author

Same thing. I have a few other containers that use mysql databases and they are all configured with environment variables like below and work great. Not sure how difficult that would be to implement.

    environment:
      MYSQL_DB_HOST: $DB_HOST
      MYSQL_DB_PORT: $DB_PORT
      MYSQL_DB_NAME: $DB
      MYSQL_DB_USER: $MYSQL_USER
      MYSQL_DB_PASS: $MYSQL_PASSWORD

@viktorstrate
Copy link
Member

I would have to look further into that

@viktorstrate viktorstrate added the docker Related to deployment with Docker label Aug 14, 2020
@tehniemer
Copy link
Author

Cool, let me know what you figure out. I've tried just about every format I can think of for the environment variable to get it to work and I end up with the same error every time.

@tehniemer
Copy link
Author

So Photoprism is using pretty much the same format as you are for database connection and I am able to spin up that container using the example docker-compose.yml they have provided. I'm still unable to get yours to work, maybe something in there will clarify what the issue is, but I'm not savvy enough to dig into the code unfortunately.

I have my database set up like this, which differs from their example, but it still works.

  mariadb:
    image: linuxserver/mariadb:latest
    container_name: mariadb
    restart: always
    networks:
      - proxy
    security_opt:
      - no-new-privileges:true
    volumes:
      - "$DOCKER_DIR/mariadb:/config"
      - "/etc/timezone:/etc/timezone:ro"
      - "/etc/localtime:/etc/localtime:ro"
    environment:
      TZ: $TZ
      PUID: $PUID
      PGID: $PGID
      MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASS

@tehniemer tehniemer changed the title Docker install error docker-compose database connection error Aug 16, 2020
@viktorstrate viktorstrate added the help wanted Extra attention is needed label Aug 16, 2020
@viktorstrate
Copy link
Member

I just looked at the way Photoprism does it, and it seems that they use a different library for connecting to the database, which makes it hard to compare.

I found this article describing the use of variables in docker compose:
https://medium.com/better-programming/using-variables-in-docker-compose-265a604c2006

It mentions the command docker-compose config which will read the docker-compose.yml file and parse all the variables and prints the result. It will also show syntax errors if it finds any.
Could you try to run that command on your config and post the result?

@tehniemer
Copy link
Author

Here's the output of that command with sensitive info removed.

photoview:
    container_name: photoview
    depends_on:
    - mariadb
    environment:
      API_LISTEN_IP: photoview
      API_LISTEN_PORT: 80
      MYSQL_URL: photoviewdb_user:dbpass@tcp(mariadb:3306)/photoview
      PHOTO_CACHE: /app/cache
      PUBLIC_ENDPOINT: https://photoview.mydomain.com/
    image: viktorstrate/photoview:latest
    labels:
      traefik.enable: "false"
      traefik.http.routers.photoview-rtr.entrypoints: https
      traefik.http.routers.photoview-rtr.middlewares: chain-authelia@file
      traefik.http.routers.photoview-rtr.rule: HostHeader(`photoview.mydomain.com`)
      traefik.http.routers.photoview-rtr.service: photoview-svc
      traefik.http.routers.photoview-rtr.tls: "true"
      traefik.http.services.photoview-svc.loadbalancer.server.port: '80'
    networks:
      database: null
      proxy: null
    ports:
    - published: 8247
      target: 80
    restart: unless-stopped
    security_opt:
    - no-new-privileges:true
    user: 1001:100
    volumes:
    - /mnt/photography/Photoview:/app/cache:rw
    - /mnt/photography/_EXPORTS/Collections:/photos:ro
    - /etc/timezone:/etc/timezone:ro
    - /etc/localtime:/etc/localtime:ro

@viktorstrate
Copy link
Member

viktorstrate commented Aug 17, 2020

I just tried to reproduce the error, but everything worked as expected.
I modified the docker-compose.example.yml to the following:

version: "3"

services:
  db:
    image: mariadb
    restart: always
    environment:
      - MYSQL_DATABASE=photoview
      - MYSQL_USER=$PHOTOVIEW_MYSQL_USER
      - MYSQL_PASSWORD=$PHOTOVIEW_MYSQL_PASSWORD
      - MYSQL_RANDOM_ROOT_PASSWORD=1
    volumes:
      - db_data:/var/lib/mysql

  photoview:
    image: viktorstrate/photoview:latest
    restart: always
    ports:
      - "8000:80"
    depends_on:
      - db

    environment:
#      - MYSQL_URL=photoview:photo-secret@tcp(db)/photoview
      - MYSQL_URL=$PHOTOVIEW_MYSQL_USER:$PHOTOVIEW_MYSQL_PASSWORD@tcp($DB_HOST)/photoview
      - API_LISTEN_IP=photoview
      - API_LISTEN_PORT=80
      - PHOTO_CACHE=/app/cache

      # Change This: The publicly exposed url
      # For example if the server is available from the domain example.com,
      # change this value to http://example.com/
      - PUBLIC_ENDPOINT=http://localhost:8000/

    volumes:
      - api_cache:/app/cache

      # Change this to the directory where your photos are located on your server.
      # If the photos are located at `/home/user/photos`, then change this value
      # to the following: `/home/user/photos:/photos:ro`.
      # You can mount multiple paths, if your photos are spread across multiple directories.
      - ./photos_path:/photos:ro

volumes:
  db_data:
  api_cache:

And exported the following environment variables:

export PHOTOVIEW_MYSQL_USER=photoview
export PHOTOVIEW_MYSQL_PASSWORD=photo-secret
export DB_HOST=db:3306

You said that you've removed sensetive information, which is understandable, but does your real db username, password or host contain a colon symbol :?

@tehniemer
Copy link
Author

So I think I figured it out. It wasn't any characters in the string, it turns out it was the database container that was causing the problem. I switched from linuxserver/mariadb:latest to mariadb:latest and it was able to make the connection along with the rest of my services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker Related to deployment with Docker help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants