0

I am just starting out with Docker. I have this Dockerfile:

FROM jonathonf/manjaro 

CMD ["pacman", "-S", "--noconfirm", "git"]

When I build the image with

sudo docker build -t uname/description:tag  .

and then run it with

sudo docker run IMAGE_ID 

, where IMAGE_ID is the ID I get from sudo docker images command, the command in the Dockerfile CMD ["pacman", "-S", "--noconfirm", "git"] runs, git is installed, a container is created (that I can commit).

If I run the image with

sudo docker run IMAGE_ID /bin/bash

the CMD from the Dockerfile is not executed.

I expected it to run the commands from the Dockerfile, make git available in the container and let me work further in the shell.

tmaric
  • 5,347
  • 4
  • 42
  • 75

3 Answers3

2

When you use CMD ["pacman", "-S", "--noconfirm", "git"] in your dockerfile, you are setting pacman -S --noconfirm git as pid-1 process of your container.

Now when you run container sudo docker run IMAGE_ID the first process will be the one specified in the CMD. You can verify this by running docker exec -it container-id ps -ef

When you run sudo docker run IMAGE_ID /bin/bash the pid-1 process of your docker container is replaced by /bin/bash.

[user@jumphost ~]$ docker run -itd -p 3666:3306 alpine sh
dcef6d1cc121bfd195552fa7639038ac513a74eaa035a855bb7917dd620be642
[user@jumphost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
dcef6d1cc121        alpine              "sh"                2 seconds ago       Up 2 seconds        0.0.0.0:3666->3306/tcp   fervent_euclid
[user@jumphost ~]$ docker exec -it dcef6d1cc121 ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 sh
    7 root      0:00 ps -ef
[user@jumphost ~]$

Also to know more about CMD check this out.

Also check docker entrypoint and its difference with CMD.

Hope this helps.

mchawre
  • 10,744
  • 4
  • 35
  • 57
2

Couple of things here:

  1. If you want git installed always, why run it as a CMD and then manually commit as a new image, rather than just running in a Dockerfile RUN instruction?

  2. Anything you put after docker run... is going to run as the CMD and override it. If you don't want to override it, you should put it as an ENTRYPOINT instead. But really you should do 1.

johnharris85
  • 17,264
  • 5
  • 48
  • 52
  • I see I need to go back to the documentation. To answer the question: I thought that this is the way I can incrementally write the Dockerfile. The software I want to put into a container has many dependencies and is complicated to build, I can't write a Dockerfile for it from scratch. I was thinking I could execute the steps in a container and then append stuff to the Dockerfile, then build the image with the new Dockerfile, log in with bash, check everything works + repeat. – tmaric Aug 04 '19 at 09:48
1

Thats how Docker works. You override the CMD from your Dockerfile. If you want to achive both, a git Install and your bash command, move your CMD command to RUN command. Please consider Dockerfile documentation https://docs.docker.com/engine/reference/builder/#run

dschuldt
  • 657
  • 4
  • 8