通过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]# lsDockerfile soeasy2 soeasy.c soeasy.sh[root@localhost docker_test]# vim Dockerfile [root@localhost docker_test]# docker build -t bigni/test1 . #-t表示tag 最后的点号,表示在当前目录寻找DockerfileSending build context to Docker daemon 865.8kBStep 1/3 : FROM scratch ---> Step 2/3 : ADD soeasy2 / ---> 3ec8199c2855Step 3/3 : CMD ["/soeasy2"] ---> Running in 7fcaf3239425Removing intermediate container 7fcaf3239425 ---> f5620b92331cSuccessfully built f5620b92331cSuccessfully tagged bigni/test1:latest[root@localhost docker_test]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEbigni/test1 latest f5620b92331c 12 seconds ago 861kBubuntu 14.04 2c5e00d77a67 7 weeks ago 188MBcentos latest 9f38484d220f 3 months ago 202MBhello-world latest fce289e99eb9 6 months ago 1.84kB[root@localhost docker_test]# docker run f5620b92331c #运行imagedocker so easy[root@localhost docker_test]# cat Dockerfile FROM scratchADD soeasy2 /CMD ["/soeasy2"][root@localhost docker_test]# cat soeasy.c #includeint main(){ printf("docker so easy\n");}[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/bashecho "docker so easy !"[root@localhost docker_test]# vim Dockerfile [root@localhost docker_test]# cat Dockerfile FROM scratchADD soeasy.sh /CMD ["/soeasy.sh"][root@localhost docker_test]# docker build -t bigni/test2 .Sending build context to Docker daemon 865.8kBStep 1/3 : FROM scratch ---> Step 2/3 : ADD soeasy.sh / ---> 93d27c3e1b5bStep 3/3 : CMD ["/soeasy.sh"] ---> Running in 4af6bd362099Removing intermediate container 4af6bd362099 ---> e2b5b08cc31cSuccessfully built e2b5b08cc31cSuccessfully tagged bigni/test2:latest[root@localhost docker_test]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEbigni/test2 latest e2b5b08cc31c About a minute ago 36Bbigni/test1 latest f5620b92331c 18 minutes ago 861kBubuntu 14.04 2c5e00d77a67 7 weeks ago 188MBcentos latest 9f38484d220f 3 months ago 202MBhello-world latest fce289e99eb9 6 months ago 1.84kB[root@localhost docker_test]# docker run e2b5b08cc31c #运行imagestandard_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 #改成centosADD soeasy.sh /CMD ["/soeasy.sh"][root@localhost docker_test]# docker build -t bigni/test3 .Sending build context to Docker daemon 865.8kBStep 1/3 : FROM centos ---> 9f38484d220fStep 2/3 : ADD soeasy.sh / ---> 9ae6ad56171aStep 3/3 : CMD ["/soeasy.sh"] ---> Running in b53205a3eb75Removing intermediate container b53205a3eb75 ---> cfbfd0a29d1cSuccessfully built cfbfd0a29d1cSuccessfully tagged bigni/test3:latest[root@localhost docker_test]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEbigni/test3 latest cfbfd0a29d1c 13 seconds ago 202MBbigni/test2 latest e2b5b08cc31c 5 minutes ago 36Bbigni/test1 latest f5620b92331c 22 minutes ago 861kBubuntu 14.04 2c5e00d77a67 7 weeks ago 188MBcentos latest 9f38484d220f 3 months ago 202MBhello-world latest fce289e99eb9 6 months ago 1.84kB[root@localhost docker_test]# docker run cfbfd0a29d1cdocker so easy ![root@localhost docker_test]#