Real Dreams
Real Dreams

Reputation: 18010

Run a command without entry point

How can I run a command against a container and tell docker not to run the entry point? e.g.

docker-compose run foo bash

The above will run the entry point of the foo machine. How to prevent it temporarily without modifying Dockerfile?

Upvotes: 4

Views: 4911

Answers (2)

David Maze
David Maze

Reputation: 158908

If you control the image, consider moving the entire default command line into the CMD instruction. Docker concatenates the ENTRYPOINT and CMD together when running a container, so you can do this yourself at build time.

# Bad: prevents operators from running any non-Python command
ENTRYPOINT ["python"]
CMD ["myapp.py"]

# Better: allows overriding command at runtime
CMD ["python", "myapp.py"]

This is technically "modifying the Dockerfile" but it won't change the default operation of your container: if you don't specify entrypoint: or command: in the docker-compose.yml then it will run the exact same command, but it also allows running things like debug shells in the way you're trying to.

I tend to reserve ENTRYPOINT for two cases. There's a common pattern of using an ENTRYPOINT to do some first-time setup (e.g., running database migrations) and then exec "$@" to run whatever was passed as CMD. This preserves the semantics of CMD (your docker-compose run bash will still work, but migrations will happen first). If I'm building a FROM scratch or other "distroless" image where it's actually impossible to run other commands (there isn't a /bin/sh at all) then making the single thing in the image be the ENTRYPOINT makes sense.

Upvotes: 2

Rémy
Rémy

Reputation: 454

docker-compose run --entrypoint=bash foo bash

It'll run a nested bash, a bit useless, but you'll have your prompt.

Upvotes: 4

Related Questions