Docker & Sqlalchemy

I have a Fastapi application which I am trying to run in a docker which uses Sqlalchemy to interact with a Postgres database. If I use “docker run” it works just fine with my URL db connection string if I use “docker-compose” it does not connect at all and complains about a particular socket not being available. So it is ignoring the TCP/IP host URL when run with docker-compose. For the life of me I can’t understand what is different in running the same image between “docker run” and “docker-compose” which makes Sqlalchemy mad…

We can not guess what you did, and can not tell you why one thing is working and another is not, without knowing what you exactly did.

Please provide the exact docker run command and content of the docker-compose.yml you used.

Yes of course. Didn’t mean to hit post. Info provided.

Docker Compose

version: “3.8”

services:
stexts-db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: ${DB_CONNECTION_PW}
POSTGRES_USER: ${DB_CONNECTION_USER}
POSTGRES_DB: ${DB_CONNECTION_DB_NAME}
expose:
- 5432/tcp
volumes:
- ./data/db:/var/lib/postgresql/data
networks:
- stexts-net

stexts-db-admin:
    image: dpage/pgadmin4
    restart: always
    environment:
        PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
        PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PW}
        PGADMIN_LISTEN_PORT: 80
    ports:
        - 15432:80
    volumes:
        - ./data/db-admin:/var/lib/pgadmin
    depends_on:
        - stexts-db
    networks:
        - stexts-net

stexts:
    build:
        context: .
        dockerfile: Dockerfile
    volumes:
        - ./app/:/code/app/
    command: uvicorn app:app --reload --host localhost --port 8000
    restart: always
    env_file:
        - .env
    ports:
        - 8000:8000
    depends_on:
        - stexts-db
    networks:
        - stexts-net

networks:
stexts-net:
driver: bridge


Dockerfile

BUILD BACKEND

BASE

FROM python:3.11

SETUP SOFTWARE

ARG NIDAQMX_INSTALL_VER=ni-ubuntu2204-drivers-2023Q3

RUN apt update

RUN apt purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false

RUN apt clean -y && rm -rf /var/lib/apt/lists/*

RUN pip install poetry

SET WORKING DIRECTORY

WORKDIR /code/app/

SETUP POETRY & PYTHON DEPENDENCIES

ARG INSTALL_DEV=false

COPY poetry.lock pyproject.toml /code/

RUN poetry config virtualenvs.create false

RUN bash -c “if [ $INSTALL_DEV == ‘true’ ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi”

INSTALL NIDAQMX

COPY ./install/nidaqmx/ /code/install/nidaqmx/

RUN apt install /code/install/nidaqmx/${NIDAQMX_INSTALL_VER}.deb


Docker Run > docker run -p 8000:8000 stexts

My formatting sucks. Apologies. Don’t know the tricks of that yet.

Regarding the formatting, here is everything there is to know about it:

I will look at your post, once its properly formatted. It makes it way easier to reader.

1 Like

Thank you for the education. I think this is much better.

My Dockerfile:

# BUILD BACKEND

# BASE

FROM python:3.11

# SETUP SOFTWARE

ARG NIDAQMX_INSTALL_VER=ni-ubuntu2204-drivers-2023Q3

RUN apt update

RUN apt purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false

RUN apt clean -y && rm -rf /var/lib/apt/lists/*

RUN pip install poetry

# SET WORKING DIRECTORY

WORKDIR /code/app/

# SETUP POETRY & PYTHON DEPENDENCIES

ARG INSTALL_DEV=false

COPY poetry.lock pyproject.toml /code/

RUN poetry config virtualenvs.create false

RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"

# INSTALL NIDAQMX

COPY ./install/nidaqmx/ /code/install/nidaqmx/

RUN apt install /code/install/nidaqmx/${NIDAQMX_INSTALL_VER}.deb

My Docker Compose File:

version: "3.8"

services:
    stexts-db:
        image: postgres
        restart: always
        environment:
            POSTGRES_PASSWORD: ${DB_CONNECTION_PW}
            POSTGRES_USER: ${DB_CONNECTION_USER}
            POSTGRES_DB: ${DB_CONNECTION_DB_NAME}
        expose:
            - 5432/tcp
        volumes:
            - ./data/db:/var/lib/postgresql/data
        networks:
           - stexts-net

    stexts-db-admin:
        image: dpage/pgadmin4
        restart: always
        environment:
            PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
            PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PW}
            PGADMIN_LISTEN_PORT: 80
        ports:
            - 15432:80
        volumes:
            - ./data/db-admin:/var/lib/pgadmin
        depends_on:
            - stexts-db
        networks:
            - stexts-net

    stexts:
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - ./app/:/code/app/
        command: uvicorn app:app --reload --host localhost --port 8000
        restart: always
        env_file:
            - .env
        ports:
            - 8000:8000
        depends_on:
            - stexts-db
        networks:
            - stexts-net
  
networks:
  stexts-net:
    driver: bridge

Docker Run Command

docker run -p 8000:8000 stexts

So it is just about the stexts image?

Is this really the exact docker run command you use to start the container, if so, why is your docker compose configuration for the service so different?

You docker command translates to this compose service definition:

  stexts:
        image: stexts
        ports:
            - 8000:8000

Most likely the problem is caused by either the volume mapping, the command, or parameters in the env:file you specify. Comment them out and test, if it’s working, re-add them one-by-one, test, repeat.

1 Like

After so many hours of staring it was a little issue with the env file and coming over. You were on the spot and thank you for your patience with me. Cheers!

What was the specific issue. I am going up the wall and the .env file is looking more like the culprit.