docker入门(十三):Docker构建自己的ubuntu镜像

构建我的ubuntu

Docker hub 中 99%的镜像都是由下面这一层开始的

FROM scratch

然后构建我们自己的层。

(1)编写配置文件dockerfile

# 新建dockerfile
test@P340:/$ touch dockerfile
dockerfile

# 编写dockerfile
test@P340:/$ vim dockerfile
dockerfile

# 查看dockerfile内容,注意在dockerfile中注释需要单独一行,以#开头
test@P340:/$ cat dockerfile 

下面是dockerfile中的内容

# 拉取官方的ubuntu
FROM ubuntu
# 作者个人信息
MAINTAINER test<hello@ubuntu_world.com>
# 环境变量
ENV MYPATH /usr/local
# 设置工作目录,Dockerfile中的RUN、CMD、ENTRYPOINT、ADD、COPY等命令都会在这个目录中执行
WORKDIR $MYPATH
# 执行这几条命令,安装一些东西
RUN apt-get update -y 
RUN apt-get install -y vim
RUN apt-get install -y net-tools
#暴露80端口
EXPOSE 80
# 指定在容器启动时需要执行的命令
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash

(2)build镜像dockerfile

sudo docker build -f dockerfile -t test_ubuntu:0.1 .
  • -f 指定dockerfile文件
  • -t 执行tag
  • 注意最后有一个.表示在指定镜像构建过程中的上下文环境目录

看看是否构建镜像成功:

test@P340:/$ sudo docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
test_ubuntu      0.1       f45ecfe39407   4 minutes ago   173MB

(3)容器内测试

新建一个容器,并运行起来

test@P340:/$ sudo docker run -it --name my_ubuntu01 test_ubuntu:0.1 /bin/bash

在容器内查看信息,测试我们的net-tools是否安装成功(在docker hub上直接下载的ubuntu镜像构建的容器是最小化安装的,是没有网络工具这些东西的)

# 可以看到,进入容器之后就直接是工作目录
root@2205c0e152ec:/usr/local# pwd
/usr/local
root@2205c0e152ec:/usr/local# ls
bin  etc  games  include  lib  man  sbin  share  src

# 查看网络信息
root@2205c0e152ec:/usr/local# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 49  bytes 7398 (7.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(4)小知识点:查看images构建历史

test@P340:/$ sudo docker history test_ubuntu:0.1
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
f45ecfe39407   18 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B        
5adb91bf2eed   18 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
523e79b0a45f   18 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
1c1723fe0f2a   18 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
d55290504eec   18 minutes ago   /bin/sh -c apt-get install -y net-tools         1.52MB    
d122d34fff57   18 minutes ago   /bin/sh -c apt-get install -y vim               68.2MB    
b0ce56db2db8   18 minutes ago   /bin/sh -c apt-get update -y                    30.1MB    
db995d082256   18 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B        
ae0afd3c58b7   18 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B        
f61ab7e476a8   18 minutes ago   /bin/sh -c #(nop)  MAINTAINER test<hello@ubu…   0B        
1318b700e415   3 weeks ago      /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      3 weeks ago      /bin/sh -c #(nop) ADD file:524e8d93ad65f08a0…   72.8MB 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

留小星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值