2

I am trying to run tests in docker as part of my build process. What i'd like to do is start the docker container, ignore the normal entry point, run a test command, and immediately exit with the test status.

Something like:

results=`docker run my_image --entrypoint python -m unittest discover`

When I try this I get: entrypoint requires the handler name to be the first argument

  • Which I believe is a specific to the image I am building off of (aws lambda).

So far I'm only seeing options to each A) start the container and issue an arbitrary command, or B) have a second Dockerfile just for testing.

Is it possible to run a docker image with an arbitrary command (ignoring the default entrypoint) where after the command is executed the container is killed?

1 Answer 1

0

Ideally you should restructure your application to avoid needing to override the entrypoint.

Remember that, when you run an image, the ENTRYPOINT and CMD are combined to form a single command. If you'll frequently be replacing this (combined) command string, it's best to put the whole command into CMD. If you have ENTRYPOINT at all, it should be a wrapper that runs the command passed to it as arguments (in a shell script, with exec "$@").

# Optional entrypoint -- MUST be JSON-array syntax, and MUST `exec "$@"`
# ENTRYPOINT ["/entrypoint.sh"]
CMD python ... whatever you had before

Then once you do this, you can easily override the command part at the docker run command

docker run my_image python -m unittest discover

(There are two other ENTRYPOINT patterns I've seen. One is a "container as command" pattern, where the entire command line is in ENTRYPOINT, and the command part is used to take additional arguments; this supports a docker run imagename --extra-args pattern. If you really need this pattern, see below to override the whole thing. The second arbitrarily splits ENTRYPOINT ["python"], CMD ["script.py"], but there's no particular reason to do this; just combine them into CMD.)

If you can't refactor your image's Dockerfile, then you need to override --entrypoint. This option only takes a single command word, though, and it's treated as a Docker option so it needs to come before the image name. That leads to this awkward construction (split into multiple lines for readability):

docker run \
  --entrypoint python \
  my_image \
  -m unittest discover

Also consider the possibilities of using a non-Docker host virtual environment for routine tasks like running your service's unit tests.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.