1

I create a docker image with two echos but only the last one is displayed:

dockerfile:

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive

CMD echo "HELLO WORLD"
CMD echo "GOODBYE WORLD"

build and run:

docker build -t junk:junk .
docker run -it junk:junk

The output is

GOODBYE WORLD

How can I control the output of docker run -it?

Leevi L
  • 1,275
  • 10
  • 22
  • 3
    You can only have a single `CMD` in your `Dockerfile`. If you have more than one, only the last one matters. If you want to run more than a single command, put them in a script and arrange for the `CMD` to run the script. – larsks Nov 19 '20 at 12:40
  • @larsks if you write an answer i'll accept – Leevi L Nov 19 '20 at 12:44

1 Answers1

2

A Docker image can only have a single CMD. If you have multiple CMD declarations in your Dockerfile, only the last one will matter. If you want to run multiple commands, you can combine them into a single CMD, like this:

CMD echo HELLO WORLD; echo GOODBYE WORLD

Or you can put them into a script file and then have your CMD run the script:

COPY myscript.sh /myscript.sh
CMD sh /myscript.sh
larsks
  • 228,688
  • 37
  • 348
  • 338