Docker-compose service restarting continuously with warnings about unset variables

I am encountering an issue with my Docker-compose setup where one of the services, specifically the profiles service, keeps restarting continuously. I’m seeing warnings in the logs related to unset variables, but I’m not sure if they are the root cause of the problem. Here’s an overview of my docker-compose.yml configuration:

x-variables: &variables
  ENV_STAGE: local

services:
  profiles:
    build:
      context: .
      dockerfile: ./compose/dev/web/Dockerfile
    volumes:
      - ./web/:/usr/src/web/
    ports:
      - "8000:8000"
    environment:
      <<: *variables
    env_file:
      - ./compose/dev/env/.env
      - ./compose/dev/env/.db.env
      - ./compose/dev/env/.data.env
    depends_on:
      - postgres
    restart: unless-stopped

  postgres:
    image: postgres:latest
    restart: always
    volumes:
      - ./compose/dev/db/pg.conf:/etc/postgresql/postgresql.conf
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=test
      - PGDATA=/var/lib/postgresql/data
    ports:
      - 5432:5432
    command: postgres -c config_file=/etc/postgresql/postgresql.conf
    env_file:
      - compose/dev/env/.db.env

The postgres service seems to be running fine, but profiles keeps restarting.

The logs show warnings about unset variables like "o58":

PS C:\Users\admin\profile> docker-compose logs --tail 50 --follow profiles
time="2024-02-25T12:17:28+02:00" level=warning msg="The \"o58\" variable is not set. Defaulting to a blank string."
time="2024-02-25T12:17:28+02:00" level=warning msg="The \"o58\" variable is not set. Defaulting to a blank string."
time="2024-02-25T12:17:28+02:00" level=warning msg="The \"o58\" variable is not set. Defaulting to a blank string."
PS C:\Users\admin\profile> 

I think that the problem is not from these logs. :slight_smile:

Could someone please assist me in troubleshooting this issue? Any insights or suggestions would be greatly appreciated.

It is very urgent task! I MUST be ready by the end of the day.

Thank you!

Why do you think it is not related to the warnings? Do you recognize the variable name? For me it doesn’t look like a variable name. And it is exactly what Docker Compose shows when you have an unset variable. I don’t remember seeing it in a container log.

If the container restarts, then the aplication fails inside the container. If it doesn’t log the reason, it is hard to say anything unfortunately, but the undefined variable is something I wouldn’t ignore.

1 Like

Hello!

Thank you so much for your reply!

Actually, the only place where I found something like o58 is here: .env: SECRET_KEY=22+sg%5+##a=^-$o58(1q9(^r@cjl-p0r3m^x9@-#i=1qcs2y12asdwqz2341casdf%zx When I remove the $ sign before o58, the log disappear, but it keeps restarting (not running).

:slight_smile:

You didn’t share the real secret with us, did you?

When variables are used in compose, every $ character needs to be escaped with another $ character. I am not sure if this still applies if the value is taken from an .env file, but it could be very much the case.

If possible I would recommend testing with a SECRET_KEY without special characters and isolate if the special characters are the problem.

Furthermore, you will want to add additional debug output to your entrypoint script and/or application, to get an idea of where and why exactly something is not working.

1 Like

As @meyay wrote, you need to escape the dollar characters. You can do it by adding a second dollar character or just using apostrophes around the value like you would in a shell script

SECRET='secret$abc'

This works only i you want to escape all the dollar characters and don’t want to use any variable in the value. Otherwise the double dollars are what you need.

1 Like

Thank you so much for your reply!

I just used apostrophes, but this doesn’t solved my problem.

The application is restarting (not working).

Can you help me somehow?

I can share everything that you need (Every detail), even the whole source code.

It is very urgent task. :slight_smile:

Thank you so much for your reply!

But the application is restarting (not working), even after this. :slight_smile:

Since you already had an incorrect variable definition, it could be already saved in a database or in a cache file, so I would recommend removing everything and starting from scratch.

Again, without error messages, there is not much we can say. If the password was not the only problem, and you have no error messages, then it is now about application debugging and we don’t know that application, only you.

One important thing which is related to Docker: You always need to run a process in the foreground in the container to keep it alive. If you don’t have that, then your container will just stop without error messages.

1 Like

Thank you so much for your reply!

Are there any commands to help me start from scratch, I am asking because I am beginner about Docker and everything will be helpful to me.

Thank you in advance. You are nice!

If you mean deleting all the data in the compose project, this is the one:

docker compose down -v

The flag -v will delete all the volumes as well, so run it only if you don’t have anything to save before deleting it.

1 Like

Do you have an idea where can be the problem if it is not from this?

Can I share the whole application or it will not help you?

You can share the Dockerfile, which could contain the command and entrypoints. If you have a command or entrypoint script, you can share that too. The rest of the application can be useful too, but unless the error is obvious, I don’t think I will recognize the problem.

1 Like

Thank you so much for your support.

To share it as a ZIP?
:slight_smile:

It would be better if you could share it as a code block directly in your message. If the application has too many files or too long source code, I won’t have time to check it anyway. So let’s start with the Dockerfile and optionally with the application. Maybe you can share the entrypoint of the application source code as well, like a main file or main method but not all of it.

1 Like

Thank you so much!

So, actually, this is the Dockerfile located in ./compose/dev/web/Dockerfile, which is used in docker-compose.yml:

# pull official base image
FROM python:3.9-alpine

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    LANG=C.UTF-8 \
    HOME=/usr/src/web

WORKDIR $HOME

ARG GID=1000
ARG UID=1000
ARG USER=ubuntu

# install dependencies
RUN apk update \
    && apk add --no-cache curl postgresql-dev gcc python3-dev musl-dev openssl libffi-dev openssl-dev build-base \
    # install Pillow dependencies
    jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev harfbuzz-dev fribidi-dev \
    # pygraphviz
    graphviz graphviz-dev \
    && pip install --upgrade pip \
    && addgroup -g $GID -S $USER \
    && adduser -S $USER -G $USER --disabled-password --uid "$UID"

COPY ./web/config/requirements  ./requirements
RUN pip install -r ./requirements/local.txt

COPY --chown=$USER:$USER . /
COPY --chown=$USER:$USER ./web $HOME

RUN chmod +x /*.sh \
    && mkdir -p $HOME/static /redis_socket \
    && chown -R $USER:$USER $HOME/static \
    && chmod -R 777 /redis_socket \
    && flake8 .

USER $USER

Let me know what do you think! :slight_smile:

Your Dockerfile has not ENTRYPOINT or CMD instruction. How is the container supposed to know what to execute when the container is started?

1 Like

Thank you so much for your reply!

Can you guide me how to fix this, because I don’t know. :slight_smile: :slight_smile:

This blogpost from me explains what an entrypoint and a cmd is and how you can use them:

For a python app, you would set something like this:

CMD ["python", "/path/to/app.py"]

You can add more parameters too if necessary

1 Like

Thank you! Actually, my application is in Django, is there something specific that I should know?

You do understand that you are the only one that has the full context information, and you share them one by one. If you want us to help efficiently you need to start sharing enough context information so we understand what you try to achieve, what you did and how you did it.

Please make the answer to “what does someone who doesn’t know the context need to know to understand what I am responding?” part of all your posts.

We know the mechanics of docker, but you must provide the details of your programming language and your application needs.

1 Like