docker个人笔记1:制作适用的镜像

本文为一些镜像的制作记录

golang运行容器

适用于 golang 程序运行的环境。
总结:
以基础镜像为基础(如 busybox),将必要的 C 库,解析器拷贝到容器中。

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

最终测试:
可行:
docker run -itd -p 80:80 --rm --name webgin latelee/webgin

docker run -itd -p 80:80 --rm --name gin latelee/busybox:go sh

docker exec -it gin sh

mkdir -p /lib/x86_64-linux-gnu /lib64


docker cp -a /lib/x86_64-linux-gnu/libpthread.so.0 gin:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libpthread-2.23.so gin:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libc-2.23.so gin:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libc.so.6 gin:/lib/x86_64-linux-gnu

docker cp -a /lib64/ld-linux-x86-64.so.2 gin:/lib64/
docker cp -a /lib/x86_64-linux-gnu/ld-2.23.so gin:/lib/x86_64-linux-gnu/


排查:
/ # ls gin_web
gin_web
/ # ./gin_web
sh: ./gin_web: not found

拷贝ld文件
docker cp -a /lib64/ld-linux-x86-64.so.2 gin:/lib64/
docker cp -a /lib/x86_64-linux-gnu/ld-2.23.so gin:/lib/x86_64-linux-gnu/


/ # ./gin_web
./gin_web: error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file or directory
拷贝 pthread
docker cp -a /lib/x86_64-linux-gnu/libpthread.so.0 gin:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libpthread-2.23.so gin:/lib/x86_64-linux-gnu

/ # ./gin_web
./gin_web: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

docker cp -a /lib/x86_64-linux-gnu/libc-2.23.so gin:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libc.so.6 gin:/lib/x86_64-linux-gnu


成功。


/ # ./webgin
sh: ./webgin: not found

docker cp /lib/ld-2.25.so arm:/lib/
docker cp /lib/ld-linux-armhf.so.3 arm:/lib/

/ # /webgin
/webgin: error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file or directory
docker cp /usr/lib/libpthread-2.25.so arm:/lib
docker cp /usr/lib/libpthread.so.0 arm:/lib

/ # /webgin
/webgin: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
docker cp /usr/lib/libc.so.6 arm:/lib/
docker cp /usr/lib/libc-2.25.so arm:/lib/

docker commit arm latelee/armbusybox:go1

docker run -itd --rm --name arm1 latelee/armbusybox:go1

docker cp webgin arm1:/

docker exec -it arm1 /webgin

docker build -t latelee/armwebgin -f Dockerfile.arm .

64位arm

1
2
3
docker run -itd --name arm --rm arm64v8/ubuntu:16.04 

docker run -itd --name arm --rm latelee/mvs:arm64 sh

c/c++运行容器

适用于 c/c++ 程序运行的环境。
总结:
以基础镜像为基础(如 busybox),将必要的 C 库,解析器拷贝到容器中。
注:是否可将 c/c++、golang 合并为一个镜像,是否有必要?

多平台合并

镜像似乎要存在于dockerhub上。

目的:制作多平台镜像,但下载镜像相同。docker根据执行命令时的架构判断选择。

步骤1:
export DOCKER_CLI_EXPERIMENTAL=enabled

2:
制作不同镜像

3、上传
docker push latelee/led-light-mapper-x86:v1.1
docker push latelee/led-light-mapper-arm:v1.1

4、
docker manifest annotate latelee/led-light-mapper:v1.1 latelee/led-light-mapper-x86:v1.1 –os linux –arch amd64
docker manifest annotate latelee/led-light-mapper:v1.1 latelee/led-light-mapper-arm:v1.1 –os linux –arch arm

docker manifest create latelee/led-light-mapper:v1.1 latelee/led-light-mapper-x86:v1.1 latelee/led-light-mapper-arm:v1.1

1
2
3
4
5
6
7
8
9
10
11
12
13
创建:
# docker manifest create latelee/webgin latelee/amd64webgin latelee/armwebgin
Created manifest list docker.io/latelee/webgin:latest

配置:
docker manifest annotate latelee/webgin latelee/amd64webgin --os linux --arch x86_64
docker manifest annotate latelee/webgin latelee/armwebgin --os linux --arch armv7l

查看:
docker manifest inspect latelee/webgin

推送:
docker manifest push latelee/webgin

arch字段:arm amd64 ppc64le arm64??

注:如果错误创建了manifest(如名称不同的镜像,但内容一致,打标签不同,认为是2个,实际是一个),再更新似乎更新不了。方法:找另一台新的机器,重新步骤。
注2:不需要下载到本地,但需要dockerhub上存在。

注3:因周知之原因,dockerhub网络不太稳定,可能需要多次尝试。

如不存在,无法下载,不存在arm版本:

1
2
3
4
5
docker pull latelee/led-light-mapper:v1.1
v1.1: Pulling from latelee/led-light-mapper
v1.1: Pulling from latelee/led-light-mapper
v1.1: Pulling from latelee/led-light-mapper
no matching manifest for linux/arm in the manifest list entries
1
2
3
4
5
6
7
8
9
https://hub.docker.com/r/latelee/led-light-mapper/tags

busybox合并为一个?

latelee/armbusybox
latelee/arm64busybox
latelee/amd64busybox

-> latelee/busybox

镜像制作记录

为保证环境一致,使用ubuntu16.04 64bit宿主机(编译测试在该系统)。CI系统可选该版本系统。

latelee/busybox:rt64 镜像

名称:64位运行环境,x86平台。
功能:
基础库,可运行简单golang、c/c++程序。如有其它库如ssl依赖,再添加

制作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
运行:
docker run -itd --name busybox latelee/busybox
创建目录:
docker exec -it busybox mkdir -p /lib/x86_64-linux-gnu /lib64
拷贝运行库、链接器:
docker cp -a /lib/x86_64-linux-gnu/libpthread.so.0 busybox:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libpthread-2.23.so busybox:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libc-2.23.so busybox:/lib/x86_64-linux-gnu
docker cp -a /lib/x86_64-linux-gnu/libc.so.6 busybox:/lib/x86_64-linux-gnu
docker cp -a /lib64/ld-linux-x86-64.so.2 busybox:/lib64/
docker cp -a /lib/x86_64-linux-gnu/ld-2.23.so busybox:/lib/x86_64-linux-gnu/
docker cp -a /usr/lib/x86_64-linux-gnu/libstdc++.so.6 busybox:/lib/x86_64-linux-gnu/
docker cp -a /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 busybox:/lib/x86_64-linux-gnu/
docker cp -a /lib/x86_64-linux-gnu/libm.so.6 busybox:/lib/x86_64-linux-gnu/
docker cp -a /lib/x86_64-linux-gnu/libm-2.23.so busybox:/lib/x86_64-linux-gnu/
docker cp -a /lib/x86_64-linux-gnu/libgcc_s.so.1 busybox:/lib/x86_64-linux-gnu/

保存
docker commit busybox latelee/busybox:rt64
docker push latelee/busybox:rt64

docker commit busybox registry.cn-hangzhou.aliyuncs.com/latelee/busybox:rt64
docker push registry.cn-hangzhou.aliyuncs.com/latelee/busybox:rt64