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\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/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]#

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM