docker--build base image

    通过dockerfile build一个base image,在上面运行一个c程序

首先

1、创建一个目录。

2、然后创建一个c写的小程序,并且gcc编译好。

3、创建一个Dockerfile


FROM scratch #scrath表示运行在内核之上
ADD soeasy2 /   #增加文件
CMD ["/soeasy2"]#运行程序

4、build image

5、运行image

[root@localhost docker_test]# ls
Dockerfile  soeasy2  soeasy.c  soeasy.sh
[root@localhost docker_test]# vim Dockerfile 
[root@localhost docker_test]# docker build -t bigni/test1 .   #-t表示tag  最后的点号,表示在当前目录寻找Dockerfile
Sending build context to Docker daemon  865.8kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD soeasy2 /
 ---> 3ec8199c2855
Step 3/3 : CMD ["/soeasy2"]
 ---> Running in 7fcaf3239425
Removing intermediate container 7fcaf3239425
 ---> f5620b92331c
Successfully built f5620b92331c
Successfully tagged bigni/test1:latest
[root@localhost docker_test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
bigni/test1         latest              f5620b92331c        12 seconds ago      861kB
ubuntu              14.04               2c5e00d77a67        7 weeks ago         188MB
centos              latest              9f38484d220f        3 months ago        202MB
hello-world         latest              fce289e99eb9        6 months ago        1.84kB
[root@localhost docker_test]# docker run f5620b92331c #运行image
docker so easy
[root@localhost docker_test]# cat Dockerfile 
FROM scratch
ADD soeasy2 /
CMD ["/soeasy2"]
[root@localhost docker_test]# cat soeasy.c 
#include<stdio.h>

int main()
{
 printf("docker so easy
");
}
[root@localhost docker_test]#

把程序改成shell脚本,然后还是在linux kernel上构建base image,结果程序运行不起来,改成在centos image上构建,则运行成功。

[root@localhost docker_test]# vim soeasy.sh 
[root@localhost docker_test]# cat soeasy.sh 
#!/bin/bash
echo "docker so easy !"
[root@localhost docker_test]# vim Dockerfile 
[root@localhost docker_test]# cat Dockerfile 
FROM scratch
ADD soeasy.sh /
CMD ["/soeasy.sh"]
[root@localhost docker_test]# docker build -t bigni/test2 .
Sending build context to Docker daemon  865.8kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD soeasy.sh /
 ---> 93d27c3e1b5b
Step 3/3 : CMD ["/soeasy.sh"]
 ---> Running in 4af6bd362099
Removing intermediate container 4af6bd362099
 ---> e2b5b08cc31c
Successfully built e2b5b08cc31c
Successfully tagged bigni/test2:latest
[root@localhost docker_test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
bigni/test2         latest              e2b5b08cc31c        About a minute ago   36B
bigni/test1         latest              f5620b92331c        18 minutes ago       861kB
ubuntu              14.04               2c5e00d77a67        7 weeks ago          188MB
centos              latest              9f38484d220f        3 months ago         202MB
hello-world         latest              fce289e99eb9        6 months ago         1.84kB
[root@localhost docker_test]# docker run e2b5b08cc31c #运行image
standard_init_linux.go:211: exec user process caused "no such file or directory"
[root@localhost docker_test]# vim Dockerfile 
[root@localhost docker_test]# cat Dockerfile 
FROM centos #改成centos
ADD soeasy.sh /
CMD ["/soeasy.sh"]
[root@localhost docker_test]# docker build -t bigni/test3 .
Sending build context to Docker daemon  865.8kB
Step 1/3 : FROM centos
 ---> 9f38484d220f
Step 2/3 : ADD soeasy.sh /
 ---> 9ae6ad56171a
Step 3/3 : CMD ["/soeasy.sh"]
 ---> Running in b53205a3eb75
Removing intermediate container b53205a3eb75
 ---> cfbfd0a29d1c
Successfully built cfbfd0a29d1c
Successfully tagged bigni/test3:latest
[root@localhost docker_test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
bigni/test3         latest              cfbfd0a29d1c        13 seconds ago      202MB
bigni/test2         latest              e2b5b08cc31c        5 minutes ago       36B
bigni/test1         latest              f5620b92331c        22 minutes ago      861kB
ubuntu              14.04               2c5e00d77a67        7 weeks ago         188MB
centos              latest              9f38484d220f        3 months ago        202MB
hello-world         latest              fce289e99eb9        6 months ago        1.84kB
[root@localhost docker_test]# docker run cfbfd0a29d1c
docker so easy !
[root@localhost docker_test]#
原文地址:https://www.cnblogs.com/laonicc/p/11143106.html