在Mac系统上写一个简易的Dockerfile并测试运行
编写Dockfile
在家目录下创建一个用于写dockfile的目录并进入
huxiaojing@huxiaojingdeMacBook-Pro ~ % mkdir Docker
huxiaojing@huxiaojingdeMacBook-Pro ~ % cd Docker/
huxiaojing@huxiaojingdeMacBook-Pro ~ % mkdir dockerfile
huxiaojing@huxiaojingdeMacBook-Pro ~ % cd dockerfile/
然后使用vim编写一个mydockerfile-centos文件
huxiaojing@huxiaojingdeMacBook-Pro dockerfile % vim mydockerfile-centos
具体内容如下:
FROM centos # 基础镜像
MAINTAINER huxiaojing<614790023@qq.com> # 作者信息
ENV MYPATH /usr/local # 配置环境变量为本地目录
WORKDIR $MYPATH # 将工作目录设置为环境变量所在的目录
FROM centos
RUN yum -y install vim # 安装vim指令
RUN yum -y install net-tools # 安装ifconfig命令
EXPOSE 80 # 暴露端口号80
CMD echo $MYPATH # 输出环境变量的路径
CMD echo "------end------" # 输出字符
CMD /bin/bash # 启动之后进入的命令行
构建镜像
采用如下命令构建镜像:
docker build -f 文件路径 -t 镜像名:[tag] .
具体过程如下:
huxiaojing@huxiaojingdeMacBook-Pro dockerfile % docker build -f mydockerfile-centos -t mycentos:1.0 .
[+] Building 26.0s (8/8) FINISHED
=> [internal] load build definition from mydockerfile-centos 0.0s
=> => transferring dockerfile: 263B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [1/4] FROM docker.io/library/centos 0.0s
=> CACHED [2/4] WORKDIR /usr/local 0.0s
=> [3/4] RUN yum -y install vim 21.3s
=> [4/4] RUN yum -y install net-tools 4.4s
=> exporting to image 0.2s
=> => exporting layers 0.2s
=> => writing image sha256:527a949f87ec4599cecffbc42f0b2324d68aa052d5148 0.0s
=> => naming to docker.io/library/mycentos:1.0 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
在Docker中查看是否生成镜像
huxiaojing@huxiaojingdeMacBook-Pro dockerfile % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0 527a949f87ec 22 seconds ago 320MB
测试运行
huxiaojing@huxiaojingdeMacBook-Pro dockerfile % docker run -it 527a949f87ec #以交互的方式运行,后面加上镜像id
[root@fe152eb00c80 local]# pwd
/usr/local # 进入容器后默认工作目录 与dockerfile所写一致
[root@fe152eb00c80 local]# ifconfig # 使用该命令可以查看网络配置信息 与dockerfile所写一致
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 10 bytes 876 (876.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
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
[root@fe152eb00c80 local]# vim test # 使用vim命令并没有报错
综上,在DockerHub上所写的的阉割版centos上加上了一些vim和ifconfig操作后,能够成功运行。
账号注册
进入https://hub.docker.com/
注册自己的dockerhub账号
上传到dockerhub上
huxiaojing@huxiaojingdeMacBook-Pro ~ % docker login -u 账号名 -p 密码
huxiaojing@huxiaojingdeMacBook-Pro ~ % docker images # 查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0 527a949f87ec About an hour ago 320MB
tomcat02 1.0 aaf8062844a2 15 hours ago 663MB
redis latest fb10d3158372 12 days ago 99.7MB
tomcat latest d9e19311a36b 2 weeks ago 658MB
nginx latest ab2a5aa39300 2 weeks ago 126MB
mysql/mysql-server latest 98f3b9f1c498 4 weeks ago 396MB
debian latest 7bb198a3a665 4 weeks ago 108MB
docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
centos latest a0477e85b8ae 6 months ago 249MB
huxiaojing@huxiaojingdeMacBook-Pro ~ % docker tag 527a949f87ec 账号名/mycentos:1.0 # 将本地创建的镜像打上标签
huxiaojing@huxiaojingdeMacBook-Pro ~ % docker push 13952742648/mycentos:1.0 #push到远程dockerhub上
The push refers to repository [docker.io/13952742648/mycentos]
825aa067cbe0: Pushed
6e57ba84c47b: Pushed
5f70bf18a086: Pushed
16764cdd1bc4: Pushed
1.0: digest: sha256:b23574e25bc1683adfaa3161eb3eb8ad5d3378a10a1d1d407015f0d2940fae6f size: 1158
查看远程仓库是否已推送成功
至此,简易dockfile文件做成、测试完且成功发布到dockerhub上。