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

  • Why does Django not find the admin/base_site.html file when I dockerize the app?
  • How to demonize one image of a docker-compose composition?
  • Docker volume content does not persist
  • Scaling Docker containers in the real world
  • In Docker Swarm mode is there any point in replicating a service more than the number of hosts available?
  • Docker how to create container
  • How do I go about doing this? my attempt at exec mode fails miserably… docker run hello --cmd=["ls", "-al"]

  • How to change docker daemon root directory in CentOS7
  • Magento 2 on docker - no css loaded
  • How to run local server in host?
  • How to run same container on all Docker Swarm nodes
  • When does a docker container stop?
  • What is the difference between the template section in replicator.yml and pod.yml in Kubernetes?
  • 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.