0

I'm trying to run a bash script after a Postgres container starts which 1) creates a new table within the Postgres DB, and 2) runs a copy command that dumps the contents of a csv file into the newly created table.

Currently, I'm specifying the execution of the script within my docker-compose.yml file using the "command" argument, but I find that it doesn't allow the Postgres container to succesfully start. I receive the following information from the log:

    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

When I remove the "command" argument everything is fine. Here is what my docker-compose.yml files looks like now:

# docker-compose.yml

version: '3'

services:
  web:
    build: .
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; uvicorn app.main:app --host 0.0.0.0'
    volumes:
      - .:/app
    expose:  # new
      - 8000
    environment:
      - DATABASE_URL=postgresql://fastapi_traefik:fastapi_traefik@db:5432/fastapi_traefik
    depends_on:
      - db
    labels: # new
      - "traefik.enable=true"
      - "traefik.http.routers.fastapi.rule=Host(`fastapi.localhost`)"
  db:
    image: postgres:13-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
      - "/Users/theComputerPerson/:/tmp"
    expose:
      - 5432
    environment:
      - POSTGRES_USER=fastapi_traefik
      - POSTGRES_PASSWORD=fastapi_traefik
      - POSTGRES_DB=fastapi_traefik
    command: /bin/bash -c "/tmp/newtable.sh"
  traefik: # new
    image: traefik:v2.2
    ports:
      - 8008:80
      - 8081:8080
    volumes:
      - "./traefik.dev.toml:/etc/traefik/traefik.toml"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

volumes:
  postgres_data:

It may be worth noting that I'm trying to customize some of the aspects of this FastAPI project, and to turn your attention to the development files and not the production files. Please let me know if I can provide any additional information in the comments.

IamTrying
  • 39
  • 6
  • 1
    take a look at `docker exec --help` – Josh Beauregard Dec 06 '22 at 18:39
  • I'd recommend doing this exactly the same way you would without Docker: use the database migration system that goes with your application framework. – David Maze Dec 06 '22 at 20:59
  • Thanks for your comments. I've just found a SO question that also discusses this: https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres – IamTrying Dec 06 '22 at 21:35

1 Answers1

3

You are overriding the default container image startup command.

According to PostgreSQL official container image page, you can extend initialization adding your sh scripts (or even sql files) to /docker-entrypoint-initdb.d directory.

See https://hub.docker.com/_/postgres.

This approach has a caveat that this script could not be executed.

Another approach is to override default container image command adding yours in bash style: postgres; /bin/bash -c "/tmp/newtable.sh";

Claudio Weiler
  • 589
  • 2
  • 15
  • As I mentioned in a comment of my question, I found another SO question that discussed this very same topic. I do, however, like your alternative method in the second part of your answer. – IamTrying Dec 06 '22 at 21:36