[Fixed] Error response from daemon: failed to create task for container – Python

by
Liam Thompson
boot2docker django django-celery docker-compose llama-cpp-python

Quick Fix: In your docker-compose.yml file, ensure that the entrypoint key is defined as a single string, not as a list. This will fix the syntax and allow Docker to create the container task successfully.

The Problem:

When dockerizing a Django application with Celery, Flower, Redis, and Gunicorn, I am encountering an error: "Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "celery -A core worker -P eventlet –autoscale=10,1 -l INFO": executable file not found in $PATH: unknown". This error occurs while trying to start Celery and Flower but the Redis and Django application starts successfully.

The Solutions:

Solution 1: Single-Line Entrypoint Definition in docker-compose.yml

In your `docker-compose.yml` file, you had previously defined the entrypoint for your Celery container as follows:

entrypoint:
      - "celery -A core worker -P eventlet --autoscale=10,1 -l INFO"

However, the correct way to specify the entrypoint in a single line is as follows:

entrypoint: "celery -A core worker -P eventlet --autoscale=10,1 -l INFO"

By making this change, you ensure that the Celery container will start correctly and execute the specified command.

Solution 2: Use double quotes in the `CMD` line of the Dockerfile

In the Dockerfile, the CMD line is currently using single quotes instead of double quotes. This causes Docker to interpret it as a command string and wrap it in /bin/sh -c. To fix this, use double quotes instead.

CMD ['gunicorn', 'core.wsgi:application', '--bind', '0.0.0.0:8000']

This will ensure that Docker recognizes the command correctly and doesn’t try to wrap it in a shell script.

Additionally, consider cleaning up the docker-compose.yml file by removing unnecessary volume mounts and using the default network.

Here’s a revised version of your docker-compose.yml file:

version: "3.8"

services:
  mailgrass_backend:
    restart: unless-stopped
    ports:
      - "8000:8000"
    env_file: .env
    build:
      context: .
      dockerfile: ./Dockerfile

  redis:
    container_name: redis
    image: redis:7.0-alpine
    restart: unless-stopped
    env_file: .env
    ports:
      - "6379:6379"
    command:
      - 'redis-server'

  celery:
    restart: unless-stopped
    env_file: .env
    build:
      context: .
      dockerfile: ./Dockerfile
    depends_on:
      - redis
    entrypoint: "celery -A core worker -P eventlet --autoscale=10,1 -l INFO"

  flower:
    restart: unless-stopped
    ports:
      - "5555:5555"
    env_file: .env
    build:
      context: .
      dockerfile: ./Dockerfile
    depends_on:
      - redis
      - celery
    entrypoint: "celery -b redis://redis:6379 flower"

volumes:
  postgres:
networks:
  default:

By making these changes, you can ensure that your containers start correctly and that celery and flower are able to function properly.

Q&A

How to fix "failed to create task for container" error in celery with docker-compose?

Use "" instead of "".

What is the issue here when using single quotes in dockerfile?

The single quotes in Dockerfile’s CMD line make it invalid JSON, causing errors.

How to cleanup and simplify docker-compose.yml?

Remove named volume mounts, use default network, and simplify build section.

Video Explanation:

The following video, titled "”[Solved", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.

How To Fix Permission Denied Error inside Docker …” description=”[Solved] How To Fix Permission Denied Error inside Docker Container? Docker Non-Root User Error Note: If you click on one of the link, …”]