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

  • Docker service Limits and Reservations
  • salt master don't find minion key
  • service docker start fails (daemon is not a docker command)
  • unable execute arm-cc on docker images ( ubuntu 12.04 base )
  • docker-machine + openstack: proxy
  • TLS failed in Docker
  • How do I go about doing this? my attempt at exec mode fails miserably… docker run hello --cmd=["ls", "-al"]

  • Getting docker pull to default pull from a private registry?
  • What's the default url that docker using when run `docker pull`?
  • Running a Python script automatically when launching a Docker container
  • How to configure Jenkins in Docker?
  • Build Docker image only if Code Repository has been updated (Jenkins)
  • When I am going to extend mysql docker image, I can't add my own entrypoint shell
  • 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.