[root@centos17 ~]#docker run --name b1 -it busybox
WARNING: IPv4 forwarding is disabled. Networking will not work.
/ #
/ # mkdir -p /data/html
/ # echo "Busybox httpd server." > /data/html/index.html 注意:制作容器时让容器处理运行状态所以在另外一个终端执行:
[root@centos17 ~]#docker commit -p b1
sha256:db1dfc89367ed1899e85665d6c069e778d66a2cd590d969e761bed77b67cfa23
[root@centos17 ~]#docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
db1dfc89367e 7 seconds ago 1.16MB 制作好的容器
nginx alpine 36f3464a2197 10 days ago 18.6MB
busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB
redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB
centos latest 49f7960eb7e4 2 months ago 200MB 可使用docker tag命令来给镜像打标签;一个镜像可打多个标签
[[root@centos17 ~]#docker tag db1dfc89367e liuyutang/httpd:v0.1-1
[root@centos17 ~]#docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
liuyutang/httpd v0.1-1 db1dfc89367e 3 minutes ago 1.16MB
nginx alpine 36f3464a2197 10 days ago 18.6MB
busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB
redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB
centos latest 49f7960eb7e4 2 months ago 200MB
[root@centos17 ~]#docker tag liuyutang/httpd:v0.1-1 liuyutang/httpd:latest
[root@centos17 ~]#docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
liuyutang/httpd latest db1dfc89367e 5 minutes ago 1.16MB
liuyutang/httpd v0.1-1 db1dfc89367e 5 minutes ago 1.16MB
nginx alpine 36f3464a2197 10 days ago 18.6MB
busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB
redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB
centos latest 49f7960eb7e4 2 months ago 200MB 删除标签
[root@centos17 ~]#docker image rm liuyutang/httpd:latest
Untagged: liuyutang/httpd:latest 启动制作好的容器;可以看到我们刚才写的页面文件存在;容器制作成功
[root@centos17 ~]#docker run --name t1 -it liuyutang/httpd:v0.1-1
WARNING: IPv4 forwarding is disabled. Networking will not work.
/ # ls /data/
html
/ # ls /data/html/
index.html
/ # cat /data/html/index.html
Busybox httpd server.
/ # 示例2:使用Busybox做为基础镜像,实现容器启动时默认运行httpd,并且在制作镜像时一起打标签;
制作镜像:
[root@centos17 ~]#docker commit -a "liuyutang " -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 liuyutang/httpd:v0.2
sha256:f394f28026ff01f0bbdceb665b8e18a26c9a43a9c12cc57e4f895808f52e5f36
[root@centos17 ~]#docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
liuyutang/httpd v0.2 f394f28026ff 4 seconds ago 1.16MB
liuyutang/httpd latest db1dfc89367e 21 minutes ago 1.16MB
liuyutang/httpd v0.1-1 db1dfc89367e 21 minutes ago 1.16MB
nginx alpine 36f3464a2197 10 days ago 18.6MB
busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB
redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB
centos latest 49f7960eb7e4 2 months ago 200MB 运行容器
[root@centos17 ~]#docker run --name t2 liuyutang/httpd:v0.2 查看容器
[root@centos17 ~]#docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
044d7516f77b liuyutang/httpd:v0.2 "/bin/httpd -f -h /d…" 37 seconds ago Up 36 seconds t2 我们可使用docker inspect命令来查看容器信息
示例
[root@centos17 ~]#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: liuyutang
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@centos17 ~]#docker image push liuyutang/httpd
The push refers to repository [docker.io/liuyutang/httpd]
e1b0032bd40b: Pushed
8e9a7d50b12c: Mounted from library/busybox
latest: digest: sha256:2ba592789010f1349b8594c97015e10c49c7d75f1aeb36c7132b2525b1c64f82 size: 734
e1b0032bd40b: Layer already exists
8e9a7d50b12c: Layer already exists
v0.1-1: digest: sha256:2ba592789010f1349b8594c97015e10c49c7d75f1aeb36c7132b2525b1c64f82 size: 734
c0a38dc06799: Pushed
8e9a7d50b12c: Layer already exists
v0.2: digest: sha256:77fbb7f07a896404b232e879ef6b061d69a233d7388f37a39ba0ab4ea14ad722 size: 734
镜像的导入和导出
将镜像文件导出为tar文件:
docker save
Save one or more images to a tar archive (streamed to STDOUT by default)
docker save [OPTIONS] IMAGE [IMAGE...] 导出示例
[root@centos17 ~]#docker save -o myimages.gz liuyutang/httpd:v0.1-1 liuyutang/httpd:v0.2
[root@centos17 ~]#ls
192.168.30.27 elasticsearch-5.5.1.rpm kibana-5.5.1-x86_64.rpm myimages.gz
anaconda-ks.cfg filebeat-5.5.1-x86_64.rpm logstash-5.5.1.rpm 从tar文件导入镜像 :
docker load
Load an image from a tar archive or STDIN
docker load [OPTIONS]
--input, -i Read from tar archive file, instead of STDIN
--quiet, -qfalseSuppress the load output 导入示例
[root@centos37 ~]#docker load -i myimages.gz
8e9a7d50b12c: Loading layer 1.378MB/1.378MB
c0a38dc06799: Loading layer 5.12kB/5.12kB
Loaded image: liuyutang/httpd:v0.2
e1b0032bd40b: Loading layer 5.12kB/5.12kB
Loaded image: liuyutang/httpd:v0.1-1
[root@centos37 ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
liuyutang/httpd v0.2 f394f28026ff 2 hours ago 1.16MB
liuyutang/httpd v0.1-1 db1dfc89367e 2 hours ago 1.16MB 这种方式适用于测试环境