Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why CMD is not executed through neither the docker create nor docker start command?

Consider the following Dockerfile being simple

FROM ubuntu

RUN apt update && apt install neofetch -y

CMD ["echo", "Hello From Docker to Ubuntu"]

According with some research the RUN instruction is executed only when the image is build through the docker build command and the CMD instruction is executed when the container is created and run from that custom image.

Therefore executing the docker build command as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

docker build -t my-ubuntu:001 .

Works as expected and executing the docker run command as follows:

docker run --name my-ubuntu-001-001 -it my-ubuntu:001

Works as expected, it mostly about the Hello From Docker to Ubuntu message that is displayed in the tty. Until here fine.

Now, exists the following equivalence:

  • docker run = docker create + docker start

Therefore is possible create and run a container from the same custom image as follows:

docker create --name my-ubuntu-001-002 my-ubuntu:001
docker start my-ubuntu-001-002

The curious of this pair is that the Hello From Docker to Ubuntu message is not displayed in the tty. So why here is not shown the message? Is it the expected behavior or is missing something?

>Solution :

You’ll need the -a flag to attach the containers stdout/stderr to your terminal. Otherwise, the container will print the message, but it will not be shown in your terminal.

Fixed command:

docker start -a my-ubuntu-001-002

Documentation.

See also the -i flag to docker start, which is required if you want stdin to be attached.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading