docker[3]-dockerrun

Docker hub:https://hub.docker.com/

第一个docker命令

1
> docker run hello-world

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

将文件快照加载到container中,执行指定命令,如 > run hello-wold

重写指定命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ docker run busybox echo hi here
hi here

$ docker run busybox ls
bin
dev
etc
home
proc
root
sys
tmp
usr
var
可以看出打印出的文件目录是运行的linux虚拟机的文件目录

查看运行中的container

1
2
3
4
> docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07615c2e850e busybox "ping baidu.com" 19 seconds ago Up 18 seconds confident_feynman

查看所有的container,包含以前运行过的。

1
2
3
4
5
6
7
8
9
10
11
> docker ps --all

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07615c2e850e busybox "ping baidu.com" About a minute ago Up About a minute confident_feynman
a043e6324f01 busybox "ls" 14 hours ago Exited (0) 14 hours ago quirky_hawking
a49540a381c4 busybox "echo hi here" 14 hours ago Exited (0) 14 hours ago silly_wozniak
5e8d8cab229d hello-world "/hello" 14 hours ago Exited (0) 14 hours ago confident_poitras
32e394eb48be elasticsearch:6.5.0 "/usr/local/bin/dock…" 2 weeks ago Exited (255) 40 hours ago 0.0.0.0:9200->9200/tcp, 9300/tcp sharp_sanderson
26e7cfd18013 elasticsearch:6.5.0 "/usr/local/bin/dock…" 2 weeks ago Exited (137) 2 weeks ago upbeat_hawking
3aef31dc5086 elasticsearch:6.5.0 "/usr/local/bin/dock…" 2 weeks ago Exited (137) 2 weeks ago trusting_bhabha
813f5b2d17c5 hello-world "/hello" 2 weeks ago Exited (0) 2 weeks ago

docker run

docker run 命令本身运行了两条指令: docker create 以及dokcer start:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ docker create hello-world
1a7fc1e876787a20d190187ba0ba6e83eda5f76b0401e9d511d2982f9d8533a6

//-a的作用是将输出打印到终端中。
$ docker start -a 1a7fc1e876787a20d190187ba0ba6e83eda5f76b0401e9d511d2982f9d8533a6

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

删除废弃container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
1a7fc1e876787a20d190187ba0ba6e83eda5f76b0401e9d511d2982f9d8533a6
a043e6324f01a418a8c115b4b1cdddab0a10afff9bc9c5ee06a720219a4f1b1c
a49540a381c4d7da4dfd1efb598a27c2db3574115b82b1b6d33c4f8390b5241e
5e8d8cab229df7f2adcef62f344581fd7e9094fd93e26618d0999545cdc6d66e
32e394eb48be7d5cd7b530a78559ab686f7cf9af88c2c7ae1fdc68e551ca5b36
26e7cfd18013369a5bc8fed0b5612ad2703929f18c01cb50809274dde055ae66
3aef31dc5086b99029e3ecabc2f6ba0cb096000cf9e535d685d08e78de9db692
813f5b2d17c5368e5c1804450da9aa054fe7072d89c64db0a77f347dd6b848ed
0d47fb9e443b4e4630722e41ddcec47fe9d8c8f5488f26f68963ad110acc0601
b0a0dac8a3ed04083591e959d339db4ba8a74cb311f793cbaa98e931e68bd333
bd1c71df5bc48a49308bba863adf66d7c7400fe93936718afbac9c10255f2804
585f744217f723b9c355720abed52bcf87d5bc9c6159c265b88b965922741106
7a3fef3b2896bc383f28cb5f1374763b94c7503827fc5705b35442e842035ce5
ad43464f85fcb2378435bdebbd2cd5c3232af2439bb934f9b0ccc1751d27e297
04a5b4a90ba958d8d3b38d369adb81facd04914535fb77da220792cf011a4963
9102238917d07e0e44a6f052fc6919f3b7b71f55f170918d9c3d5646491277f0
39e50fa4f86b5a260dd472cbfae7a875410093a95bb349e5551156f88b71eb3d
49e7c150cac80467f1eb66ceb2ea53fc06e855f3d7f93f1f41b781b956c071ba
5f5d3dd19fba4734a9b3508f4b0fe9bce5fc3c993c08a29fe2f98e01c622f651
fd9d9e75471a281b1388e9376492b0017055a613b833d40598a95b83d5c4c599
f8564237f6ffc9b95f06bab040aedd465f915f8cc58abdf63fcf8346476cc3ca
f48f5791f55949dcbd3c4c010d3336877c9887f0a5a2a0973d1d06ede75f8bfe
3aee61c2515fbcb95e8bbc631450037ed7c31df12084cb18fe6a7fcc44d9ba2f
460336feb3c41a8449edf6938813055f4f1a54bca5ac0d4792ebf56c5b150e43
0175f73a66980a7976484b6165914dc2c92b3038de212ed3c8ada518b3747315
5bb9c44ff6e00236e1efff308c15ab2c325dca83dadcfa5436929fe53bca3c9b

Deleted Networks:
composer_default
frontend_default

Deleted Images:
deleted: sha256:976d4268d7fdf90cc93ec61f85f078927d36bdc67db90b499b89ccf1a055ef0a
deleted: sha256:8a8ed4af849b7d02679d9b9e4fab2fc9127c767bb06663941cbd670a290e0164
deleted: sha256:016d15cc49ebebae72ead1a301ff58d286452b4c41b6e55bf773c178dbca8e80
deleted: sha256:d0ec57f38348b804f222f15580a90ccbd74b62edb7603c31ca93426b17273177
deleted: sha256:d38bf24881b8a552e4860a522b855da7477c21d1afda8da134f58ae2385b5cb1
deleted: sha256:a072b7745bf6bcaed809584e6c435a0b73c0bf7bcdc079cee7b1a0e3646bf99b
deleted: sha256:9dd54c53fb6a246adcb826eb42321be2f1bf37d61eb96180da2ba05b68d218d6
deleted: sha256:1dfabb24d4d4a7f7b3ba2af8d5f58e4f16805c0e67e1337a6240d13402487b22
deleted: sha256:a241c1a1c4a34e202c30a39f4bcba36ea2572b7707ce393d8d495c60f940218d
deleted: sha256:1611321909424f073c866daf57fe4d6fc3e5f49e34aeb2b2e0c37bc0c9f31333

Total reclaimed space: 462.9MB

停止container

docker stop 让程序自动停止,10秒钟后不停止即会调用docker kill
docker kill 立即停止

1
2
> docker stop <id>
> docker kill <id>

打印日志

1
2
3
4
5
6
7
8
$ docker create busybox echo hi there
5795ad1cd0e25ff9601eef2fdb45a28ba7fc3f765a73b6b5513a7fcdd1a9b767

$ docker start 5795ad1cd0e25ff9601eef2fdb45a28ba7fc3f765a73b6b5513a7fcdd1a9b767
5795ad1cd0e25ff9601eef2fdb45a28ba7fc3f765a73b6b5513a7fcdd1a9b767

$ docker logs 5795ad1cd0e25ff9601eef2fdb45a28ba7fc3f765a73b6b5513a7fcdd1a9b767
hi there

执行命令在container中

it代表能够与键盘交互、i代表输入输出。 t代表好的格式化展示。 exec命令将redis-cli加入到container中

1
2
3
4
5
6
$ docker run redis
$ docker exec -it 5f6f2e746bf4 redis-cli
127.0.0.1:6379> set myvalue 5
OK
127.0.0.1:6379> get myvalue
"5"

sh在container中执行任意指令

1
2
3
4
5
6
7
$ docker exec -it 5f6f2e746bf4 sh
# ls
# cd /
# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
# redis-cli
127.0.0.1:6379>

构建自己的docker image

新建 Dockerfile文件

1
2
3
4
5
6
#使用存在的docker image作为基本,类似于一个初始的操作系统。
FROM alpine
#下载redis到container的文件fs系统中
RUN apk add --update redis
#告诉 image如何做 开启作为container的时候
CMD ["redis-server"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ docker build .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM alpine
---> 11cd0b38bc3c
Step 2/3 : RUN apk add --update redis
---> Running in d92500442b84
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/1) Installing redis (4.0.11-r0)
Executing redis-4.0.11-r0.pre-install
Executing redis-4.0.11-r0.post-install
Step 3/3 : CMD ["redis-server"]
---> Running in 0f7a29a04b04
Removing intermediate container 0f7a29a04b04
---> 5a14153acdc5
Successfully built 5a14153acdc5

$ docker run 5a14153acdc5

当构建好,最后就会有一个image id,可以通过docker run 5a14153acdc5运行。

docker build cache

docker build 时,拥有缓存机制。例如,如果在之前的基础上增加下面的命令:
命令行显示出:

1
2
3
4
5
6
7
8
9
10
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM alpine
---> 11cd0b38bc3c
Step 2/3 : RUN apk add --update redis
---> Using cache
---> 50e12ffd3c2e
Step 3/3 : CMD ["redis-server"]
---> Using cache
---> 44385ec8305b
Successfully built 44385ec8305b

这是由于最后一次执行docker build时 前两条命令时完全一样的,就会使用到缓存机制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
下面的两个dockerfile文件在缓存机制上也是不一样的。
FROM alpine

RUN apk add --update redis
RUN apk add --update gcc

CMD ["redis-server"]



FROM alpine

RUN apk add --update gcc
RUN apk add --update redis




CMD ["redis-server"]

自己的image打包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ docker build -t zhuimengshaonian/redis-test:latest .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM alpine
---> 11cd0b38bc3c
Step 2/4 : RUN apk add --update redis
---> Using cache
---> 50e12ffd3c2e
Step 3/4 : RUN apk add --update gcc
---> Using cache
---> 8abfafcb6590
Step 4/4 : CMD ["redis-server"]
---> Using cache
---> 44385ec8305b
Successfully built 44385ec8305b
Successfully tagged zhuimengshaonian/redis-test:latest

##运行

1
docker run zhuimengshaonian/redis-test

简单的docker web应用程序

package.json:

1
2
3
4
5
6
7
8
{
"dependencies": {
"express": "*"
},
"scripts": {
"start": "node index.js"
}
}

index.js:

1
2
3
4
5
6
7
8
9
10
11
const express = require('express');

const app = express();

app.get('/', (req, res) => {
res.send('How are you doing');
});

app.listen(8080, () => {
console.log('Listening on port 8080');
});

Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Specify a base image 预置docker包node中的image
FROM node:alpine
# 当前工作路径 目的是将应用程序文件与其他文件分离分离
WORKDIR /usr/app

# Install some depenendencies
# 添加当前目录中的文件到container中,并执行npm install命令
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command 开启container时候的start命令
CMD ["npm", "start"]

打包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ docker  build -t zhuimengshaonian/webtest:latest .
Sending build context to Docker daemon 4.096kB
Step 1/6 : FROM node:alpine
---> 4e50ad7c0e0b
Step 2/6 : WORKDIR /usr/app
---> Running in 2cdc013a3181
Removing intermediate container 2cdc013a3181
---> 2d21ad008a15
Step 3/6 : COPY ./package.json ./
---> 3f2b0cf62e30
Step 4/6 : RUN npm install
---> Running in e9d76dc22840
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN app No description
npm WARN app No repository field.
npm WARN app No license field.

added 48 packages from 36 contributors and audited 121 packages in 6.418s
found 0 vulnerabilities

Removing intermediate container e9d76dc22840
---> ff47e69c4824
Step 5/6 : COPY ./ ./
---> 14e12e344e93
Step 6/6 : CMD ["npm", "start"]
---> Running in 31b6e275bb7c
Removing intermediate container 31b6e275bb7c
---> e4c1d9cc8d2e
Successfully built e4c1d9cc8d2e
Successfully tagged zhuimengshaonian/webtest:latest

执行

端口转发,实际的8080端口映射docker中的8080端口。

1
2
3
4
5
6
$ docker run -p 8080:8080  zhuimengshaonian/webtest

> @ start /usr/app
> node index.js

Listening on port 8080