Run Command When Container Starts
Create Dockerfile
You can use ENTRYPOINT
and CMD
to run a command when a container starts. ENTRYPOINT
defines the program to run while CMD
defines the argument to pass. Note that if ENTRYPOINT
is not defined, CMD
will send commands to /bin/sh -c
# Build from base image
FROM ubuntu:latest
# Maintainer label
LABEL maintainer="[email protected]"
# Ping Google.com five times
ENTRYPOINT ["/bin/echo"]
CMD ["Hello", "World"]
Build Image From Dockerfile
Build the Dockerfile (docker build
) in the current directory (.
) and call the image chrisalbon/ubuntu:hello-world (--tag chrisalbon/ubuntu:hello-world
).
docker build --tag chrisalbon/ubuntu:hello-world .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM ubuntu:latest
---> 1e4467b07108
Step 2/4 : LABEL maintainer="[email protected]"
---> Using cache
---> 74a6f636b9c1
Step 3/4 : ENTRYPOINT ["/bin/echo"]
---> Running in d43915bb2075
Removing intermediate container d43915bb2075
---> b7c93aeb23d0
Step 4/4 : CMD ["Hello", "World"]
---> Running in 9dc2e4b32004
Removing intermediate container 9dc2e4b32004
---> b1ef4c66453f
Successfully built b1ef4c66453f
Successfully tagged chrisalbon/ubuntu:hello-world
Run Docker Container From Image
Start and create (docker run
) an interative (-it
) Docker container from the image called chrisalbon/ubuntu:hello-world
. Remove the container after it stops (-rm
)
docker run --rm -it chrisalbon/ubuntu:hello-world
Hello World