I’m creating a postgres container and after db service is healthy I run psql service which simply creates database if it doesn’t exist.
Now in psql service command directive, I expect the left side of OR operator to exit with non zero code and then the right side of the command should be executed. This is exactly what happens if I run the command directly inside postgres image. But here in docker compose file, short circuit doesn’t work. When the left side exits with non zero code, it doesn’t execute right side.
I’m aware that POSTGRES_DB env var can be passed to postgres service, but I’ve to create the database this way. I’ve already tried running this command with sh -c “COMMAND HERE”
The entrypoint of that image is the psql command so you can’t execute anything like that you tried, as the command is the argument of the entrypoint. In this case even if you use sh -c, that will also be the argument of psql, which will not work of course. It should be logged in the container log, but you redirected the errors to /dev/null. When you see something doesn’t work, it is useful to enable logging until you fix it.
You can override the entrypoint in compose:
entrypoint: ""
command: "..."
At least overriding works with the docker run command so I assume it should work in compose.
Update:
On second thought, since everything is the argument of psql, the redirection is also included as an argument, so it should log something in the container
Hi, thankyou for your reply. I literally found the solution to the problem 30 min ago while trying out different things. But I didn’t have an explanation for that. Thats where your answer helps. You’re absolutely right. using entrypoint instead of command fixed the issue.
That is not what I suggested, but in your case, it was also an option
My suggestion was to override the entrypoint with an empty string and use the original command. When you build an image, it matters, but not when you just run the container.