构建docker基本镜像
准备:
创建一个目录oldboy-hello
编写Dockerfile
内容如下,只有三行
FROM scratch
ADD hello /
CMD ["/hello"]
当前目录下的文件
[root@linux-node1 oldboy-hello]# ls
Dockerfile hello
构建docker镜像
[root@linux-node1 oldboy-hello]# docker build -t oldboyedu/hello .
Sending build context to Docker daemon 4.608kB
Step 1/3 : FROM scratch
--->
Step 2/3 : ADD hello /
---> 0018e580ccaf
Removing intermediate container 64b74f2071e1
Step 3/3 : CMD /hello
---> Running in 6438314dc49d
---> 4a047322f97d
Removing intermediate container 6438314dc49d
Successfully built 4a047322f97d
Successfully tagged oldboyedu/hello:latest
启动容器
[root@linux-node1 oldboy-hello]# docker run --rm -it oldboyedu/hello
Welcome oldboy education!
补充
hello的是使用C语言编译之后的二进制程序
[root@linux-node1 opt]# cat hello.c
//#include <unistd.h>
#include <sys/syscall.h>
#ifndef DOCKER_IMAGE
#define DOCKER_IMAGE "hello-world"
#endif
#ifndef DOCKER_GREETING
#define DOCKER_GREETING "Welcome oldboy education!"
#endif
const char message[] =
DOCKER_GREETING "\n";
void _start() {
//write(1, message, sizeof(message) - 1);
syscall(SYS_write, 1, message, sizeof(message) - 1);
//_exit(0);
syscall(SYS_exit, 0);
}
编译命令gcc -o hello -static -nostartfiles hello.c
测试执行
[root@linux-node1 oldboy-hello]# ./hello
Welcome oldboy education!
如果编译出错
[root@linux-node1 ~]# gcc -o hello -static -nostartfiles hello.c
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
安装glibc-static
yum install glibc-static gcc -y
评论已关闭