Home > Net >  ERROR: unrecognised command `sh`, or `python`, or `bash` while all can be executed inside the image
ERROR: unrecognised command `sh`, or `python`, or `bash` while all can be executed inside the image

Time:02-01

I want to run a script after a docker image has been initialized. The image in question is a node:16 with python and other stuff

https://github.com/Flagsmith/flagsmith/blob/main/Dockerfile

Anyway, if I run the image without commands or entry-point it does start successfully. If I login using docker exec -it ###### /bin/bas I can then run either sh, bash or even python

However having:

  flagsmith:
      image: flagsmith/flagsmith:latest
      environment:
          # skipping for readibility
      ports:
          - "9000:8000"
      depends_on:
          - flotto-postgres
      links:
          - flotto-postgres
      volumes: ['./init_flagsmith.py:/init_flagsmith.py', './init_flagsmith.sh:/init_flagsmith.sh']
      command: /bin/bash '/init_flagsmith.sh'  # <-------- THIS GUY IS NOT WORKING

it does not run, and the returned error is 0 with this message (depending on the tool I run on init_flagsmith.sh :

ERROR: unrecognised command '/bin/bash'

CodePudding user response:

If you look at the end of the Dockerfile you link to, it specifies

ENTRYPOINT ["./scripts/run-docker.sh"]
CMD ["migrate-and-serve"]

In the Compose file, the command: overrides the Dockerfile CMD, but it still is passed as arguments to the ENTRYPOINT. Looking at the run-docker.sh script, it does not accept a normal shell command as its arguments, but rather one of a specific set of command keywords (migrate, serve, ...).

You could in principle work around this by replacing command: with entrypoint: in your Compose file. However, you'll still run into the problem that a container only runs one process, and so your setup script runs instead of the normal container process.

What you might do instead is set up your initialization script to run the main entrypoint script when it finishes.

#!/bin/sh
# init_flagsmith.sh

...

# at the very end
exec ./scripts/run-docker.sh "$@"

I also might package this up into an image, rather than injecting the files using volumes:. You can create an image FROM any base image you want to extend it.

# Dockerfile
FROM flagsmith/flagsmith:latest
COPY init_flagsmith.sh init_flagsmith.py ./
ENTRYPOINT ["./init_flagsmith.sh"]  # must be JSON-array syntax
CMD ["migrate-and-serve"]           # must repeat from the base image
                                    # if changing ENTRYPOINT

Then you can remove these options from the Compose setup (along with the obsolete links:)

  flagsmith:
      build: .
      environment:
          # skipping for readibility
      ports:
          - "9000:8000"
      depends_on:
          - flotto-postgres
      # but no volumes:, command:, or entrypoint:
  • Related