I'm new-ish to docker
and am trying to understand it a little better by working with from-scratch examples.
I wanted to start as primitively as I could think of: copying a file from my context to the root of my docker image, then validating the file's existence in the container.
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 9.8 (stretch)
Release: 9.8
Codename: stretch
$ ls -R
.:
demo.txt Dockerfile
$
$ cat Dockerfile
FROM scratch
WORKDIR .
COPY demo.txt /foo
$
$ docker build -t demo:v1 -f ./Dockerfile .
Sending build context to Docker daemon 28.67kB
Step 1/3 : FROM scratch
--->
Step 2/3 : WORKDIR .
---> Using cache
---> 8eb9da711a99
Step 3/3 : COPY demo.txt /foo
---> c57e0e9a316b
Successfully built c57e0e9a316b
Successfully tagged demo:v1
$
$ docker run -it demo:v1 ls -l /foo
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"ls\": executable file not found in $PATH": unknown.
I understand the gist of the problem: FROM scratch
is just that -- without even ls
. So that's fixable by changing FROM scratch
to something more meaty:
$ cat Dockerfile
FROM alpine:3.7
WORKDIR .
COPY demo.txt /foo
$
$ docker build -t demo:v1 -f ./Dockerfile .
Sending build context to Docker daemon 28.67kB
Step 1/3 : FROM alpine:3.7
---> 6d1ef012b567
Step 2/3 : WORKDIR .
---> Using cache
---> 51c22fe2fd60
Step 3/3 : COPY demo.txt /foo
---> Using cache
---> e2c241241653
Successfully built e2c241241653
Successfully tagged demo:v1
$
$ docker run -it demo:v1 ls -l /foo
-rw-r--r-- 1 root root 6 Sep 8 23:42 /foo
This bring me to my question: I'm confused by this concept of installing/running a different Linux distribution (alpine) than what is on my host (debian). What happens when I run my docker container built from an image that included FROM alpine:3.7
in its Dockerfile
? Is a virtual machine that runs alpine Linux started on my physical PC that runs debian?
If I wanted to do something very primitive -- like ls
-- in my docker container, how does one pick the most minimal base image? I picked alpine randomly -- not particularly knowing what I was doing -- when would one need to pick a different distro, like ubuntu, etc.?