[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 5182e96772bf 6 days ago 200 MB
启动一个容器
[root@master ~]# docker run -itd 5182e96772bf /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
4faf98f1c6619212c3205b0a7830617ff69d457c0882ed0ca4e71430fc549406
这里提示ipv4的路由功能没有开启,所以,我们需要开启ipv4的路由转发功能。
-p:生成过程中停止容器的运行
[root@master ~]# docker commit -m "nwe" -a "test" cbeb511d2b9a apache:test
sha256:96aaf92c7b04b6d91fcaea0d15ad7715d575dac82971458320d54d449eedc174
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache test 96aaf92c7b04 12 seconds ago 319 MB
docker.io/centos latest 5182e96772bf 6 days ago 200 MB
可以看到现在本地有两个镜像了,其中一个就是我们修改后,重新创建的。我们测试用这个镜像创建并启动容器,访问容器的apache服务。
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ea867891f2b 96aaf92c7b04 "/bin/bash" 8 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp upbeat_spence
cbeb511d2b9a 5182e96772bf "/bin/bash" 39 minutes ago Up 39 minutes inspiring_davinci
[root@master ~]# docker exec -it 5ea867891f2b /bin/bash
[root@5ea867891f2b /]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64
[root@5ea867891f2b /]# /usr/sbin/apachectl -D FOREGROUND
Passing arguments to httpd using apachectl is no longer supported.
You can only start/stop/restart httpd using this script.
If you want to pass extra arguments to httpd, edit the
/etc/sysconfig/httpd config file.
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[root@master ~]# vim Dockerfile
FROM centos
#基于的基础镜像centos,若本地没有,则去仓库下载
MAINTAINER test
#维护该镜像的用户信息,随意填写
RUN yum install httpd -y
#镜像操作安装apache软件包
EXPOSE 80
#开启80端口
ADD index.html /var/www/html/index.html
#复制网页首页文件
ADD run.sh /run.sh
#将启动脚本添加到镜像中
RUN chmod 777 /run.sh
#修改脚本执行权限
CMD ["/run.sh"]
#启动容器时执行脚本
[root@master ~]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND
[root@master ~]# vim index.html
this is test
[root@master ~]# docker build -t httpd:centos .
#在Dockerfile文件目录下,运行命令,注意后面的点,一定要有
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos ef950765e1bb 2 minutes ago 319 MB
daoke test 2850c25d855b 24 minutes ago 215 MB
docker.io/centos latest 5182e96772bf 6 days ago 200 MB
#可以看到第一个就是我们刚才创建的镜像
将新创建的镜像加载到容器中运行
[root@master ~]# docker run -d -p 1200:80 httpd:centos
519bf067d1ad0e06aeb030530d3bcb517be1424ed41c0635d6aca28784c80143
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
519bf067d1ad httpd:centos "/run.sh" 24 seconds ago Up 23 seconds 0.0.0.0:1200->80/tcp pedantic_elion
可以看到镜像已经在容器中运行,访问虚拟机IP以及端口即http://192.168.58.159:1200,成功访问首页。
[root@master ~]# 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: lightblueyx
Password:
Login Succeeded
[root@master ~]# docker tag httpd:centos lightblueyx/httpd:centos
[root@master ~]# docker push lightblueyx/httpd:centos
The push refers to a repository [docker.io/lightblueyx/httpd]
cdf567d92322: Pushed
619278fd492d: Pushed
7e054e58afc8: Pushed
abf29fcf5046: Pushed
1d31b5806ba4: Pushed
centos: digest: sha256:6ce997f142e7757853296a314ea4de4309785867988ddc06dae57d5bacc428e6 size: 1362
[root@master ~]# docker tag daoke:test 192.168.58.159:5000/daoke
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos ef950765e1bb 46 minutes ago 319 MB
lightblueyx/httpd centos ef950765e1bb 46 minutes ago 319 MB
192.168.58.159:5000/daoke latest 2850c25d855b About an hour ago 215 MB
daoke test 2850c25d855b About an hour ago 215 MB
docker.io/centos latest 5182e96772bf 6 days ago 200 MB
docker.io/registry latest b2b03e9146e1 5 weeks ago 33.3 MB
使用docker push上传标记的镜像。
[root@master ~]# docker push 192.168.58.159:5000/daoke
The push refers to a repository [192.168.58.159:5000/daoke]
3a1d67a7fe13: Pushed
latest: digest: sha256:7eaeca4042df243bcec277baf4867b04ad6e0b1f112b8324e05655f216dc919f size:
在本地可以查看到上传的镜像。
[root@master ~]# cd /data/
[root@master data]# ls
registry