How do I make Docker (or podman
, for that matter - interested in a solution for both or just one) re-run the CMD
of a stopped container?
I've got this barebone Dockerfile
:
FROM alpine
CMD ["date"]
I build it:
$ podman build -t reruncmd .
STEP 1: FROM alpine
STEP 2: CMD ["date"]
STEP 3: COMMIT reruncmd
--> 32ef88d23c0
Successfully tagged localhost/reruncmd:latest
32ef88d23c04eeb8b8bbafb1dc2851e9ce046fb88dbfddea020c16c3a1944461
Then I run it:
$ podman run --name re-run-cmd reruncmd
Fri Jun 18 06:20:33 UTC 2021
Now it's obviously stopped:
$ podman ps -a --filter 'name=^/?re-run-cmd$'
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2795e08162e1 localhost/reruncmd:latest date 2 minutes ago Exited (0) 2 minutes ago re-run-cmd
But when I restart the container, the CMD
isn't run again:
$ podman container restart re-run-cmd
2795e08162e1089eb639098a804ec7d8743ed274d4d7acbdc97f6b07ec1ecdfe
What do I need to change?
I used podman
in my examples above; get the exact same behaviour with docker
:
$ docker build -t reruncmd .
[+] Building 14.4s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.2s
=> => transferring dockerfile: 69B 0.2s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:latest 10.7s
=> [auth] library/alpine:pull token for registry-1.docker.io 0.0s
=> [1/1] FROM docker.io/library/alpine@sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0 3.1s
=> => resolve docker.io/library/alpine@sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0 0.0s
=> => sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0 1.64kB / 1.64kB 0.0s
=> => sha256:1775bebec23e1f3ce486989bfc9ff3c4e951690df84aa9f926497d82f2ffca9d 528B / 528B 0.0s
=> => sha256:d4ff818577bc193b309b355b02ebc9220427090057b54a59e73b79bdfe139b83 1.47kB / 1.47kB 0.0s
=> => sha256:5843afab387455b37944e709ee8c78d7520df80f8d01cf7f861aae63beeddb6b 2.81MB / 2.81MB 1.0s
=> => extracting sha256:5843afab387455b37944e709ee8c78d7520df80f8d01cf7f861aae63beeddb6b 1.9s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:c5faac680f09542b5efbfc9f9f9fe40265ea17e0654a47ac67040cf2f14473fc 0.0s
=> => naming to docker.io/library/reruncmd 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
$ docker run --name re-run-cmd reruncmd
Fri Jun 18 06:25:47 UTC 2021
$ docker ps -a --filter 'name=^/?re-run-cmd$'
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a3bfcc42814 reruncmd "date" 11 seconds ago Exited (0) 8 seconds ago re-run-cmd
$ docker container restart re-run-cmd
re-run-cmd
$ docker container start re-run-cmd
re-run-cmd
Actually, CMD
is executed at every start. You have been misled by the fact that docker start
does not show you the result of CMD
. If you run docker start
with -a
or --attach
key, you will see the output.
❯ docker run --name test debian echo hi
hi
❯ docker start test
test
❯ docker start -a test
hi
❯ docker logs test
hi
hi
hi
As you see from the last command, there were exactly three runs.