1

So I am working on a project where I created a logger service that uses prisma to post some logs to a postgres database. The logger service is in a docker container and the postgres db is also in a contanier. Both are on a testing linux computer.

I can use DBeaver to connect to the databse and creating tables and everythign as normal. When I spin up the logger service with node on my main computer, everything works as expected posting logs to the database in the container on the linux computer.

When I hit the logging service in a container on the linux computer, from my main computer. I get the following error:

error: Error validating datasource db: the URL must start with the protocol postgresql:// or postgres://.

For the docker file I am running the following:

FROM node:18-alpine
WORKDIR /usr/app
COPY ./ /usr/app

ARG EXPOSE_PORT=3000
ARG LOGGER_ENV="DEV"
ARG LOG_DATABASE_URL="postgresql://postgres:exmaple@localhost:5432/log"
ENV LOGGER_PORT=${EXPOSE_PORT}
ENV LOG_DATABASE_URL=${LOG_DATABASE_URL}
ENV DB_LOGGER_ENVIORMENT=${LOGGER_ENV}
RUN npm install
RUN npm uninstall prisma
RUN npm install prisma
RUN npx prisma generate
EXPOSE ${EXPOSE_PORT}
CMD ["npm", "start"]

I am using portainer and the template allows for the user of docker compose and that file looks like this:

services:
  database:
    image: postgres
    environment:
      - POSTGRES_PASSWORD= {{ DB_PASSWORD }}
      - POSTGRES_USER={{ DB_USER }}
    ports:
      - 5432:5432
    restart: always
    networks:
      - backend
  logger-service:
    environment:
      - LOG_DATABASE_URL= postgresql://{{ DB_USER }}:{{DB_PASSWORD}}@database:5432/log
      - LOGGER_PORT= {{ LOGGER_PORT }}
      - DB_LOGGER_ENVIORMENT= {{ LOGGER_ENV }}
    image: ghcr.io/bednaz98/db-logger:main
    ports:
      - {{ LOGGER_PORT }}:{{ LOGGER_PORT }}
    restart: always
    depends_on:
      - database
networks:
  backend:
    driver: bridge

When the logger service starts up, I can print that a db url string does exist in the contanier an I can print out the following when the prisma client is inialized and the string is added to the class constructor:

prisma client db string:  
- db type:  postgresql
- host ip: localhost:5432

I am using git packages to create a the docker container, and I event tried supplying a dumby db url string in the yaml env: postgresql://test:test@localhost:3000/log in hopes that maybe the prisma generated file needed it at that point. However it seems the program running in teh container can find the generate functions as expected.

Why would this not work in a container but but work running it directly though node (ts-node technically)?

I tried looking into the previous post and they did not work:

1 Answer 1

0

There are extra spaces in the environment: values

environment:
  - LOG_DATABASE_URL= postgresql://{{ DB_USER }}:{{DB_PASSWORD}}@database:5432/log
  #                  ^
  #                  like this one

This format is KEY=value, and so that leading space is included as the first character in the environment variable value. The URL is postgresql://..., starting with a space, which doesn't have the syntax described in the error message.

In addition to deleting the extra spaces, your other syntactic option is to use a YAML dictionary instead of a list of strings. YAML allows an arbitrary amount of whitespace here (at least one character) so the extra space won't cause a problem. The trick here is that you may need to explicitly quote the values that include punctuation, since { ... } looks like an inline YAML mapping.

environment:
  LOG_DATABASE_URL: postgresql://{{ DB_USER }}:{{DB_PASSWORD}}@database:5432/log
  LOGGER_PORT: '{{ LOGGER_PORT }}'

(You might consider using Compose's native environment variable substitution over an external templating engine. Also remember that containers must agree on their networks: to be able to communicate with each other, where the easiest solution is to delete all of the networks: blocks and use the Compose-provided default network.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, removing the white space and trimming the env string in the code for redundancy helped. Also, I was trying to be fancy with the network stuff but it did end up being more trouble than it's worth.

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.