int main() { printf("hello world!\n"); return 0; }
# 不加-static编译,docker run会报错,为什么?后续再查... ... # standard_init_linux.go:190: exec user process caused "no such file or directory" [vagrant@localhost ~]$ gcc -static helloworld.c -o helloworld
[vagrant@localhost ~]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE yujiang/helloworld latest b898a6498b21 21 seconds ago 857kB
查看分层 [vagrant@localhost ~]$ docker history yujiang/helloworld IMAGE CREATED CREATED BY SIZE COMMENT b898a6498b21 About a minute ago /bin/sh -c #(nop) CMD ["/helloworld"] 0B 2949199fbdb8 About a minute ago /bin/sh -c #(nop) ADD file:5e0b91d4866514aa0… 857kB
[vagrant@localhost ~]$ ll -h total 848K -rw-rw-r--. 1 vagrant vagrant 50 Dec 13 16:41 Dockerfile -rwxrwxr-x. 1 vagrant vagrant 837K Dec 13 16:47 helloworld -rw-rw-r--. 1 vagrant vagrant 79 Dec 13 16:11 helloworld.c
[vagrant@localhost ~]$ docker run yujiang/helloworld hello world!
[vagrant@localhost ~]$ docker run -it centos [root@b2985a1be234 /]# yum install vim -y [root@b2985a1be234 /]# exit [vagrant@localhost ~]$ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b2985a1be234 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago nervous_haslett
2、使用docker commit创建image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[vagrant@localhost ~]$ docker commit b2985a1be234 yujiang/centos-vim sha256:d0a8856e664eb754f9854c0c27a92c7d01623d2087a77269fbd12ba5021e6e13 [vagrant@localhost ~]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE yujiang/centos-vim latest d0a8856e664e 59 seconds ago 327MB centos latest 1e1148e4cc2c 7 days ago 202MB
[vagrant@localhost ~]$ docker history 1e1148e4cc2c IMAGE CREATED CREATED BY SIZE COMMENT 1e1148e4cc2c 7 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 7 days ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 7 days ago /bin/sh -c #(nop) ADD file:6f877549795f4798a… 202MB [vagrant@localhost ~]$ docker history d0a8856e664e IMAGE CREATED CREATED BY SIZE COMMENT d0a8856e664e About a minute ago /bin/bash 126MB 1e1148e4cc2c 7 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 7 days ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 7 days ago /bin/sh -c #(nop) ADD file:6f877549795f4798a… 202MB
这样发布image是不安全的,因为其他人不知道你对镜像做了哪些修改。不提倡。
使用docker build制作image
1、创建centos-vim目录
1
[vagrant@localhost ~]$ mkdir centos-vim && cd centos-vim
2、编写Dockerfile
1 2 3
[vagrant@localhost centos-vim]$ vim Dockerfile FROM centos RUN yum install vim -y
[vagrant@localhost centos-vim]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE yujiang/centos-vim latest e00635baf672 6 minutes ago 327MB centos latest 1e1148e4cc2c 7 days ago 202MB
[vagrant@localhost ~]$ docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: lnsyyj Password: Login Succeeded
[vagrant@localhost ~]$ docker push lnsyyj/helloworld:latest The push refers to repository [docker.io/lnsyyj/helloworld] a9094ec14918: Pushed latest: digest: sha256:dd740db962a1e3a8fb74461505f539248b7c88de80b133db612c22e80d7b2d17 size: 527