Skip to content
Advertisement

How to setup psycopg2 in a docker container running on a droplet?

I’m trying to wrap a scraping project in a Docker container to run it on a droplet. The spider scraps a website and then writes the data to a postgres database. The postgres database is already running and managed by Digitalocean.

When I run the command locally to test, everything is fine:

JavaScript

I can visualize the spider writing on the database.

Then, I use github action to build and push my docker image on a registry each time I push the code with the script:

JavaScript

Then I ssh root@ipv4 manually to my droplet in order to install docker, docker compose and run the image from the registry with:

JavaScript

As soon as the python script starts on the droplet I have the error:

psycopg2.OperationalError: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

It seems like I’m doing something wrong and I can’t find how to fix this so far. I would appreciate some help explanations.

Thanks,

My Dockerfile:

JavaScript

My docker-compose.yaml

JavaScript

Advertisement

Answer

Problem solved!

The .env file with all my credentials was in the .dockerignore. It was then impossible to locate this .env when building the image.

Advertisement