In your shell, just run the command:
➜ ~ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Maybe you will see a few extra lines if your Docker install is brand new.
hello-world
image.Hello from Docker!
Let’s run a container
➜ ~ docker run --rm -i -t debian:9 root@51ca967672e8:/#
Debian
system version 9.--rm
tells Docker that remove the container when it exits automatically.-i
tells Docker keep stdin open and connect us to the container’s stdin.-t
tells Docker than we want a pseudo-TTY.Try to echo Hello
and play fun with toilet
command.
root@66fbe4fe71e1:/# echo Hello
Hello
root@66fbe4fe71e1:/# toilet
bash: toilet: command not found
Of course, we need to install it.
We need toilet
, so let’s install it:
root@66fbe4fe71e1:/# apt update && apt install -y toilet
Get:1 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
...
One minute later, toilet
is instaled.
The toilet
takes a string as parameter. -f
option to special font.
root@66fbe4fe71e1:/# toilet Docker -f mono9
▄▄▄▄ █
█ ▀▄ ▄▄▄ ▄▄▄ █ ▄ ▄▄▄ ▄ ▄▄
█ █ █▀ ▀█ █▀ ▀ █ ▄▀ █▀ █ █▀ ▀
█ █ █ █ █ █▀█ █▀▀▀▀ █
█▄▄▄▀ ▀█▄█▀ ▀█▄▄▀ █ ▀▄ ▀█▄▄▀ █
It’s OK.
exit
or ^D
to exit our container.toilet
command (if we never installed it on host).stopped
state.This is a debian:9
‘s image.
(Tao) ➜ debian tree
.
├── 0783bfd2d1fc35d2dd7c457d9c7195ef2512acabe6ffa78fd035cece65292b0e
│ ├── json
│ ├── layer.tar
│ └── VERSION
├── 3bbb526d26083e7a65a7a112ed72e1ec58e81384412f2d3fcdbbd87d49fd588d.json
├── manifest.json
└── repositories
1 directory, 6 files
+----------------------------------+
| read-write layer for container |
| |
| |
+----------------------------------+
+----------------------------------+
|read only layer for image |
|+-------------------------------+ |
|| 6bbb34566773 0 B | |
|| | |
|+-------------------------------+ |
| |
|+-------------------------------+ |
|| 3bbb526d2608 101 M | |
|| | |
|+-------------------------------+ |
+----------------------------------+
debian:9
docker image.docker run
start a container from a given image.
(Tao) ➜ ~ docker pull debian:9
9: Pulling from library/debian
Digest: sha256:07fe888a6090482fc6e930c1282d1edf67998a39a09a0b339242fbfa2b602fff
Status: Image is up to date for debian:9
(Tao) ➜ ~ docker run --rm -it --name debian debian:9
root@17bae8832e6f:/#
(Tao) ➜ ~ docker commit debian local/debian:9
sha256:86fb2e51de2c8501c51d10f4839154464cba66afe69490da0723a0e0fecb2a35
debian
, centos
taobeier/vim
, taobeier/docker
registry.corp.youdao.com/infraop/openjdk
docker run
.docker commit
to commit our changes.docker tag
to set images namespaces and name.As explained before:
docker diff
to show difference between the image and its copy.Dockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image.Dockerfile
is a text document.docker build
command build an image from Dockerfile
.A Dockerfile
need to be a empty directory.
(Tao) ➜ ~ mkdir new-image
Dockerfile
inside this directory.(Tao) ➜ ~ cd new-image
(Tao) ➜ new-image vim Dockerfile
FROM debian:9
RUN apt update
RUN apt install -y toilet
Dockerfile
must start with a FROM
instruction.FROM
instruction specifies the Base Image from which you are building.RUN
instruction will execute any commands in a new layer on top of the current image and commit the results.(Tao) ➜ new-image docker build -t local/toilet .
-t
indicates the tag to apply to the image..
indicates the build location of the build context.(Tao) ➜ new-image docker build -t local/toilet .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM debian:9
---> f2aae6ff5d89
Step 2/3 : RUN apt update
---> Running in 677466dc02b1
...
Removing intermediate container 677466dc02b1
---> 49f7109e9c21
Step 3/3 : RUN apt install -y toilet
---> Running in 3d9dc635fd20
Setting up toilet (0.3-1.1) ...
...
Removing intermediate container 3d9dc635fd20
---> d8d2a8171e93
Successfully built d8d2a8171e93
Successfully tagged local/toilet:latest
If you run this build again, what happens?
(Tao) ➜ new-image docker build -t local/toilet .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM debian:9
---> f2aae6ff5d89
Step 2/3 : RUN apt update
---> Using cache
---> 49f7109e9c21
Step 3/3 : RUN apt install -y toilet
---> Using cache
---> d8d2a8171e93
Successfully built d8d2a8171e93
Successfully tagged local/toilet:latest
(Tao) ➜ new-image docker history local/toilet:latest
IMAGE CREATED CREATED BY SIZE COMMENT
d8d2a8171e93 25 minutes ago /bin/sh -c apt install -y toilet 4.36MB
49f7109e9c21 25 minutes ago /bin/sh -c apt update 16.2MB
f2aae6ff5d89 4 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 4 days ago /bin/sh -c #(nop) ADD file:58d5c21fcabcf1eec… 101MB
RUN <command>
(shell form, the command is run in a shell, which by default is /bin/sh -c
on Linux or cmd /S /C
on Windows)RUN ["executable", "param1", "param2"]
(exec form)# escape=`
FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\
CMD
and ENTRYPOINT
The CMD
instruction has three forms:
CMD ["executable","param1","param2"]
(exec form, this is the preferred form)CMD ["param1","param2"]
(as default parameters to ENTRYPOINT)CMD command param1 param2
(shell form)
There can only be one CMD
instruction in a Dockerfile.
ENTRYPOINT
has two forms:
ENTRYPOINT ["executable", "param1", "param2"]
(exec form, preferred)ENTRYPOINT command param1 param2
(shell form)
The ENTRYPOINT
same as CMD
. Only the last one will take effect.
Each stage is a separate image, and can copy files from previous stages. Each stage is numbered, starting at 0.
FROM debian AS builder
LABEL maintainer="Jintao Zhang <zhangjintao9020@gmail.com>"
...
RUN ./configure \
--with-compiledby="Jintao Zhang <zhangjintao9020@gmail.com>" \
&& make \
&& make install
FROM debian
COPY --from=builder /usr/local/bin/ /usr/local/bin
...
ENTRYPOINT [ "executable" ]
CMD [ "--help" ]
—
—
—
(Tao) ➜ ~ docker version
Client:
Version: 17.06.0-ce
API version: 1.30
Go version: go1.8.3
Git commit: 02c1d87
Built: Fri Jun 23 21:15:15 2017
OS/Arch: linux/amd64
Server:
Version: dev
API version: 1.39 (minimum version 1.12)
Go version: go1.10.3
Git commit: e8cc5a0b3
Built: Tue Sep 11 02:09:53 2018
OS/Arch: linux/amd64
Experimental: false
List all tools about docker.
(Tao) ➜ ~ ls /usr/bin |grep docker
docker # docker clidocker-containerd
docker-containerd-ctr # containerd clidocker-containerd-shim
dockerd
docker-init # init injectsdocker-proxy
docker-runc
+-------------------------+
| dockerd |
+-------------------------+
|
+-------------------------+
| containerd |
+-------------------------+
| |
+---------------+ +---------------+
|containerd-shim| |containerd-shim|
+---------------+ +---------------+
| |
+---------------+ +---------------+
| runc | | runc |
+---------------+ +---------------+