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:
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
See also the -i flag to docker start, which is required if you want stdin to be attached.