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

  • Access host database from a docker container
  • Nginx SSL inside a docker container
  • writing liberty logs directly to graylog
  • Continuous deployment & AWS autoscaling using Ansible (+Docker ?)
  • docker-machine behind corporate proxy
  • How use my docker-compose configuration with gitlab CI
  • How do I go about doing this? my attempt at exec mode fails miserably… docker run hello --cmd=["ls", "-al"]

  • Questions about Mesos Linux container and Docker
  • Spark in docker parquet error No predefined schema found
  • PyCharm remote debug in a docker container
  • Docker-compose on Windows 7: how to import a db dump when using a yaml file?
  • Docker fixed date
  • How to set the name of a Docker container using REST API
  • 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.