Could I ask your opinion on the following? I tried setting up the whole environment now and I got Redis to work. I don’t do any tasks for Redis during deployment but I’m having trouble getting Postgres connected and the prisma migration through.
I saw that init containers exist for docker compose but they aren’t even documented, lol. When I try to use them in my compose Coolify creates its own service container which isn’t something I want for my service stack. There shouldn’t be an init that just continously creates logs and isn’t actually relevant to production.
I had something like this:
init-db:
image: node:20-bookworm-slim
command: sh -c 'yarn rw prisma migrate deploy'
environment:
- YARN_VERSION=4.3.0
- NODE_ENV=production
- DATABASE_URL=postgres://postgres:password@docker-container:5432/postgres
depends_on:
api:
condition: service_healthy
Sadly it returned a bunch of errors in the init-db container logs:
yarn run v1.22.22
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Couldn't find a package.json file in "/"
So I decided to try it with the Dockerfile and I added the following without changing the compose file at all:
# api build
# ---------
FROM base as api_build
COPY --chown=node:node api api
COPY --chown=node:node scripts/seed.ts scripts/seed.ts
RUN yarn rw build api
ENV DATABASE_URL=postgres://postgres:password@postgres-c4gkckk:5432/postgres
RUN yarn rw prisma migrate deploy
RUN yarn rw prisma db seed
Now I know you said you can’t run the cli from api_serve, but my deployment seems to run the commands it just can’t reach the postgres database container:
[COMMAND] docker exec l44ggwk bash -c 'SOURCE_COMMIT=<hash> COOLIFY_BRANCH=main docker compose --env-file /artifacts/l44ggwk/.env --project-directory /artifacts/l44ggwk -f /artifacts/l44ggwk/docker-compose.prod.yml build'
[OUTPUT]
#23 2.460 Running Prisma CLI...
#23 2.460 $ yarn prisma migrate deploy --schema /app/api/db/schema.prisma
#23 2.460
[COMMAND] docker exec l44ggwk bash -c 'SOURCE_COMMIT=<hash> COOLIFY_BRANCH=main docker compose --env-file /artifacts/l44ggwk/.env --project-directory /artifacts/l44ggwk -f /artifacts/l44ggwk/docker-compose.prod.yml build'
[OUTPUT]
#23 3.461 Prisma schema loaded from api/db/schema.prisma
[COMMAND] docker exec l44ggwk bash -c 'SOURCE_COMMIT=<hash> COOLIFY_BRANCH=main docker compose --env-file /artifacts/l44ggwk/.env --project-directory /artifacts/l44ggwk -f /artifacts/l44ggwk/docker-compose.prod.yml build'
[OUTPUT]
#23 3.501 Datasource "db": PostgreSQL database "postgres", schema "public" at "postgres-c4gkckk:5432"
#23 3.562
[COMMAND] docker exec l44ggwk bash -c 'SOURCE_COMMIT=<hash> COOLIFY_BRANCH=main docker compose --env-file /artifacts/l44ggwk/.env --project-directory /artifacts/l44ggwk -f /artifacts/l44ggwk/docker-compose.prod.yml build'
[OUTPUT]
#23 3.564 Error: P1001: Can't reach database server at `postgres-c4gkckk:5432`
#23 3.564
#23 3.564 Please make sure your database server is running at `postgres-c4gkckk:5432`.
#23 ERROR: process "/bin/sh -c yarn rw prisma migrate deploy" did not complete successfully: exit code: 1
[COMMAND] docker exec l44ggwk bash -c 'SOURCE_COMMIT=<hash> COOLIFY_BRANCH=main docker compose --env-file /artifacts/l44ggwk/.env --project-directory /artifacts/l44ggwk -f /artifacts/l44ggwk/docker-compose.prod.yml build'
[OUTPUT]
------
> [api api_build 4/5] RUN yarn rw prisma migrate deploy:
2.458
2.460 Running Prisma CLI...
2.460 $ yarn prisma migrate deploy --schema /app/api/db/schema.prisma
2.460
3.461 Prisma schema loaded from api/db/schema.prisma
3.501 Datasource "db": PostgreSQL database "postgres", schema "public" at "postgres-c4gkckk:5432"
3.562
3.564 Error: P1001: Can't reach database server at `postgres-c4gkckk:5432`
3.564
3.564 Please make sure your database server is running at `postgres-c4gkckk:5432`.
------
I tried substituting the IP of the container for the container name but that didn’t work either. I should mention that I am mapping the ports to the host system with 3000:5432 because Coolify already has an instance of postgres for their own server on 5432. I tried the connection string with both 5432 and 3000 as the port.
What am I missing here? I have a very similar setup for my redis container and once my api is built it can connect to redis no problem. Using docker exec on the container I can see through psql the database is running and even nmap shows me the port is listening. At the very top of my deployment I can see that coolify is using its network to set things up with their helper image (Coolify adds some environment variables to the docker-compose so it can internally manage the container).
[COMMAND] docker run -d --network coolify --name l44ggwk --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/coollabsio/coolify-helper:latest
I tried using nmap during the base build and indeed it can’t reach the postgres container (I also tried it on my redis container and indeed it can’t reach the redis container from here either):
[COMMAND] docker exec y0gsc8k bash -c 'SOURCE_COMMIT=hash COOLIFY_BRANCH=main docker compose --env-file /artifacts/y0gsc8k/.env --project-directory /artifacts/y0gsc8k -f /artifacts/y0gsc8k/docker-compose.prod.yml build'
[OUTPUT]
#10 [api base 5/16] RUN nmap 172.18.0.8
[COMMAND] docker exec y0gsc8k bash -c 'SOURCE_COMMIT=hash COOLIFY_BRANCH=main docker compose --env-file /artifacts/y0gsc8k/.env --project-directory /artifacts/y0gsc8k -f /artifacts/y0gsc8k/docker-compose.prod.yml build'
[OUTPUT]
#10 0.272 Starting Nmap 7.93 ( https://nmap.org ) at 2024-06-23 15:50 UTC
[COMMAND] docker exec y0gsc8k bash -c 'SOURCE_COMMIT=hashCOOLIFY_BRANCH=main docker compose --env-file /artifacts/y0gsc8k/.env --project-directory /artifacts/y0gsc8k -f /artifacts/y0gsc8k/docker-compose.prod.yml build'
[OUTPUT]
#10 3.333 Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
#10 3.333 Nmap done: 1 IP address (0 hosts up) scanned in 3.09 seconds
I can clearly see my container running in docker ps -a:
19fxa34b5fa postgres:16-alpine "docker-entrypoint.s…" 37 minutes ago Up 37 minutes (healthy) 0.0.0.0:3000->5432/tcp, :::3000->5432/tcp postgres-c4gkckk
Docker inspect shows me that the container is using the coolify network config:
"Ports": {
"5432/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3000"
},
{
"HostIp": "::",
"HostPort": "3000"
}
]
},
"coolify": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"postgres-c4gkckk"
],
"MacAddress": "....",
"NetworkID": "...",
"EndpointID": "...",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.8",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": {},
"DNSNames": [
"postgres-c4gkckk",
"19fxa34b5fa"
]
}
I’m at a loss for why I can’t reach it. Clearly once it’s deployed it’s able to reach the container just fine, as I’ve tested with redis and ultimately the api container doesn’t complain about not being able to connect to postgres once it’s running. The coolify docker helper image I mentioned earlier has the following network config:
"Networks": {
"coolify": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "...removed",
"NetworkID": "...removed",
"EndpointID": "...removed",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.9",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": [
"jc0oo4k",
"f0a31fc9a2a5"
]
}
It’s on the same Gateway. It should be able to reach the other containers, no?
// EDIT: Upon digging through Coolify’s issues it seems this is a known bug. There’s a fix in the work for that, when using the internal docker hostnames. Will report back when it works! Sorry for the long post