override default docker run from host with options/arguments

FROM alpine:3.5
CMD ["echo", "hello world"]

So after building docker build -t hello . I can run hello by calling docker run hello and I get the output hello world.

Now let’s assume I wish to run ls or sh – this is fine. But what I really want is to be able to pass arguments. e.g. ls -al, or even tail -f /dev/null to keep the container running without having to change the Dockerfile

  • Kitchen and Kitchen-docker
  • Running a docker container to execute python script with minimal footprint
  • Redis sentinel Docker setup
  • How to provide external url to docker image
  • Collect only from STDERR when using Docker syslog logging driver
  • Docker Service won't start Windows Server 2016
  • How do I go about doing this? my attempt at exec mode fails miserably… docker run hello --cmd=["ls", "-al"]

  • Docker - Execute a bash when running a container
  • Flag provided but not defined -d while running docker container
  • Docker environment for two similar projects
  • Why am I getting ECONNREFUSED connecting to localhost MySQL from docker node app?
  • Running Windows Server Core in Docker Container
  • Run VirtualBox in Docker to achieve running Genymotion in Docker
  • 2 Solutions collect form web for “override default docker run from host with options/arguments”

    Anything after the image name in the docker run command becomes the new value of CMD. So you can run:

    docker run hello ls -al
    

    Note that if an ENTRYPOINT is defined, the ENTRYPOINT will receive the value of CMD as args rather than running CMD directly. So you can define an entrypoint as a shell script with something like:

    #!/bin/sh
    
    echo "running the entrypoint code"
    
    # if no args are passed, default to a /bin/sh shell
    if [ $# -eq 0 ]; then
      set -- /bin/sh
    fi
    
    # run the "CMD" with exec to replace the pid 1 of this shell script
    exec "$@"
    

    Q. But what I really want is to be able to pass arguments. e.g. ls -al, or even tail -f /dev/null to keep the container running without having to change the Dockerfile

    This is just achieved with:

     docker run -d hello tail -f /dev/null
    

    So the container is running in background, and it let you to execute arbitrary commands inside it:

    docker exec <container-id> ls -la
    

    And, for example a shell:

    docker exec -it <container-id> bash
    

    Also, I recommend you what @BMitch says.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.