建立目录:
[root@iZwz99m0qp1swglw9om6zxZ docker]# pwd
/root/docker
[root@iZwz99m0qp1swglw9om6zxZ docker]# mkdir hurong-first-docker
[root@iZwz99m0qp1swglw9om6zxZ docker]# ls
hurong-first-docker
[root@iZwz99m0qp1swglw9om6zxZ docker]# cd hurong-first-docker/
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]#
建立一个c程序
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# vim hello.c
#include<stdio.h>
int main(){
printf("this is hurong's first docker imagen");
}
编译前需要安装(如果没安装的话)
(sudo) yum install gcc
(sudo) yum install glibc-static
编译完成后会多了一个hello文件
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# gcc -static hello.c -o hello
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# ls
hello hello.c
直接运行可以查看效果:
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# ./hello
this is hurong's first docker image
准备生成镜像文件:
在Dockerfile中写入:
FROM scratch
ADD hello /
CMD ["/hello"]
说明:
第1行表示从哪个镜像,由于是自己的第一个基础镜像,所以写scratch
第2行表示将编译的hello打包到镜像的根目录
第3行表示容器启动后执行根目录下的hello程序
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# vim Dockerfile
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# ls
Dockerfile hello hello.c
构建镜像:
docker build -t hurong .
-t后面是镜像名,点是当前目录,表示从当前目录构建
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# docker build -t hurong .
Sending build context to Docker daemon 847.9kB
Step 1/3 : FROM scratch
--->
Step 2/3 : ADD hello /
---> 4e8c5c2ab049
Step 3/3 : CMD ["/hello"]
---> Running in fef58c716e36
Removing intermediate container fef58c716e36
---> 5e574987d1c6
Successfully built 5e574987d1c6
Successfully tagged hurong:latest
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]#
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# ls
Dockerfile hello hello.c
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
hurong latest 5e574987d1c6 About a minute ago 844kB
hello-world latest e38bc07ac18e 2 weeks ago 1.85kB
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]#
运行试试:
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# docker run hurong
this is hurong's first docker image
查看docker镜像分层:
docker history IMAGE ID
[root@iZwz99m0qp1swglw9om6zxZ hurong-first-docker]# docker history 5e574987d1c6
IMAGE CREATED CREATED BY SIZE COMMENT
5e574987d1c6 3 minutes ago /bin/sh -c #(nop) CMD ["/hello"] 0B
4e8c5c2ab049 3 minutes ago /bin/sh -c #(nop) ADD file:ea6276e5b0554f41b… 844kB
上面可以看到这个镜像添加了一个文件,然后执行根目录下的hello程序
- 相关文章
- docker容器中开启php扩展,制成新镜
- docker容器中安装扩展后制成新镜像
- 如何在docker容器外执行php artisa
- docker容器如何执行composer insta
- docker容器内无法上网ping不通外网
- 将docker中的文件拷贝到docker外
- docker中nginx可以访问html无法访
- docker容器中安装amqp扩展
- docker安装及入门教程
- centos7上安装docker
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- centos7.4 64位下swoole安装及配置
- laravel页面静态化的方法
- navicate连接mycat报1184错误
- 单点登录sso原理及php实现方式及de
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:607篇
- 浏览数:935521