Search code examples
dockerdocker-cmd

Does 'docker start' execute the CMD command?


Let's say a docker container has been run with docker run and then stopped with docker stop. Will the CMD command be executed after a docker start?


Solution

  • I believe @jripoll is incorrect, it appears to run the command that was first run with docker run on docker start too.

    Here's a simple example to test:

    First create a shell script to run called tmp.sh:

    echo "hello yo!"
    

    Then run:

    docker run --name yo -v "$(pwd)":/usr/src/myapp -w /usr/src/myapp ubuntu sh tmp.sh 
    

    That will print hello yo!.

    Now start it again:

    docker start -ia yo
    

    It will print it again every time you run that.

    Same thing with Dockerfile

    Save this to Dockerfile:

    FROM alpine
    
    CMD ["echo", "hello yo!"]
    

    Then build it and run it:

    docker build -t hi .
    docker run -i --name hi hi
    

    You'll see "hello yo!" output. Start it again:

    docker start -i hi
    

    And you'll see the same output.