Home > OS >  Python executable not found in Docker images based on Ubuntu
Python executable not found in Docker images based on Ubuntu

Time:08-31

I'm trying to build 4 images with docker-compose by using a Ubuntu base and my problem is that the endpoints script fails because python executable isn't found in 3 of the images: celery_worker, celery_beat and flower.

I entered the 3 containers and noticed that python3.10 is the correct name of the binary, so I tried to create a symbolic link in the Dockerfile but unfortunnatly the link isn't created in these images. How can I solve this issue ?

Image web:

$ docker-compose exec web bash
root@9feff05c4ea1:/app# python
Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Image flower :

$ docker-compose exec flower bash
root@fa9d93403410:/app# python
bash: python: command not found

Please find de files below.

docker-compose.yml

version: '3.8'

services:
  web:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: front_office
    command: /start
    volumes:
      - .:/app
    ports:
      - 8010:8000
    env_file:
      - ./front_office/.env
    depends_on:
      - redis
      - db

  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_DB=hello_django
      - POSTGRES_USER=hello_django
      - POSTGRES_PASSWORD=hello_django

  redis:
    image: redis

  celery_worker:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: front_office_worker
    command: /start-celeryworker
    volumes:
      - .:/app
    env_file:
      - ./front_office/.env
    depends_on:
      - redis
      - db

  celery_beat:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: front_office_beat
    command: /start-celerybeat
    volumes:
      - .:/app
    env_file:
      - ./front_office/.env
    depends_on:
      - redis
      - db

  flower:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: front_office_flower
    command: /start-flower
    volumes:
      - .:/app
    env_file:
      - ./front_office/.env
    ports:
      - 5557:5555
    depends_on:
      - redis
      - db

volumes:
  postgres_data:

Dockerfile

FROM ubuntu

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

RUN apt-get update \
  && apt-get install -y build-essential \
  && apt-get install -y libpq-dev \
  && apt-get install -y gettext \
  && apt-get install -y python3.10 \
  && apt-get install -y python-pip \
  && apt-get install -y python3-pip \ 
  && apt-get install -y git \
  && apt-get install -y openssh-client \
  && apt-get install -y libcurl4-openssl-dev libssl-dev \
  && apt-get install -y python3-requests \
  && apt-get install -y cargo \
  && apt-get install -y procps \
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && rm -rf /var/lib/apt/lists/*

RUN ln -s /usr/bin/python3.10 /usr/bin/python

# Requirements are installed here to ensure they will be cached.
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

COPY ./compose/local/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod  x /entrypoint

COPY ./compose/local/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod  x /start

COPY ./compose/local/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r$//g' /start-celeryworker
RUN chmod  x /start-celeryworker

COPY ./compose/local/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r$//g' /start-celerybeat
RUN chmod  x /start-celerybeat

COPY ./compose/local/django/celery/flower/start /start-flower
RUN sed -i 's/\r$//g' /start-flower
RUN chmod  x /start-flower

WORKDIR /app

ENTRYPOINT ["/entrypoint"]

entrypoint

#!/bin/bash

# if any of the commands in your code fails for any reason, the entire script fails
set -o errexit
# fail exit if one of your pipe command fails
set -o pipefail
# exits if any of your variables is not set
set -o nounset

postgres_ready() {
python << END
import sys

import psycopg2

try:
    psycopg2.connect(
        dbname="${SQL_DATABASE}",
        user="${SQL_USER}",
        password="${SQL_PASSWORD}",
        host="${SQL_HOST}",
        port="${SQL_PORT}",
    )
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)

END
}
until postgres_ready; do
  >&2 echo 'Waiting for PostgreSQL to become available...'
  sleep 1
done
>&2 echo 'PostgreSQL is available'

exec "$@"

log

front_office-celery_worker-1  | Waiting for PostgreSQL to become available...
front_office-celery_worker-1  | /entrypoint: line 11: python: command not found
front_office-celery_worker-1  | Waiting for PostgreSQL to become available...
front_office-celery_beat-1    | /entrypoint: line 11: python: command not found
front_office-celery_beat-1    | Waiting for PostgreSQL to become available...
front_office-celery_worker-1  | /entrypoint: line 11: python: command not found
front_office-celery_worker-1  | Waiting for PostgreSQL to become available...
front_office-flower-1         | /entrypoint: line 11: python: command not found
front_office-flower-1         | Waiting for PostgreSQL to become available...
front_office-celery_beat-1    | /entrypoint: line 11: python: command not found
front_office-celery_beat-1    | Waiting for PostgreSQL to become available...
front_office-flower-1         | /entrypoint: line 11: python: command not found
front_office-flower-1         | Waiting for PostgreSQL to become available...
front_office-celery_worker-1  | /entrypoint: line 11: python: command not found

CodePudding user response:

I see you tried to make python command be known with this line

RUN ln -s /usr/bin/python3.10 /usr/bin/python

But apt-get install python3.10 should ideally do that on its own. Otherwise, it's not a safe operation to override any pre-installed system Python executable.

Instead, rather than start from bare-Ubuntu image, you can start FROM python:3.10 then remove related install steps for Python, Pip, etc. And use RUN pip install commands directly

  • Related