1

Does every docker container directly present only a shell ?

I have a python image:

$ sudo docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              2.7-slim            48e3247f2a19        10 days ago         120MB

I originally thought it presents Python to me.

But I can't run python commands directly in the container:

$ sudo docker run 48e3 print("hello")
bash: syntax error near unexpected token `('

and I can run commands directly in the container, just as in a shell:

$ sudo docker run 48e3 ls
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
$ sudo docker run 48e3 python --version
Python 2.7.16
0

1 Answer 1

3

How the container executes depends on how it was built. In the builder's Dockerfile there may be CMD and ENTRYPOINT options.

For example, here is a container that never calls /bin/sh.

First, the program we want to run. I've written it in go just because it's easy to demonstrate.

$ cat small.go 
package main

import "fmt"
import "os"

func main() {
  fmt.Println("Hello")
  fmt.Print("You entered ")
  fmt.Println(os.Args[1:])
}

$ go build small.go

Now the instructions for building the container:

$ cat Dockerfile 
FROM scratch
ADD small /
ENTRYPOINT ["/small"]

$ docker build -t small .
Sending build context to Docker daemon  1.642MB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD small /
 ---> Using cache
 ---> 6171cecbf91b
Step 3/3 : ENTRYPOINT ["/small"]
 ---> Using cache
 ---> 14af8187a035
Successfully built 14af8187a035
Successfully tagged small:latest

And now running the container:

$ docker run --rm small some options passed
Hello
You entered [some options passed]

In fact this container only contains one file, the small program and nothing else! No shell, no libraries, nothing.

Now the docker image you're looking at will drop into python if you run it correctly and don't pass any parameters

$ docker run --rm -it python:2.7-slim
Python 2.7.16 (default, Mar 27 2019, 09:57:44) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

If we inspect the image, we can see...

$ docker inspect python:2.7-slim
...
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"python2\"]"
            ],

So we can see it was built with the CMD option in Dockerfile. This tells it the default command to run if no other command is passed to the docker run call.

So with this container, it will run python if not told to run anything else.

Which means we can do fun things like

$ echo 'print("hello")' | docker run --rm -i python:2.7-slim
hello
3
  • Could we remove the first line FROM scratch?
    – flow2k
    Commented Oct 10, 2019 at 19:00
  • @flow2k Please open a new question for this; it doesn't belong in comments. Commented Oct 11, 2019 at 19:42
  • Actually, I posted this question stackoverflow.com/questions/58330046/… a few days ago. Feel free to weigh in if you have additional input. Thanks.
    – flow2k
    Commented Oct 12, 2019 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .