docker之制作镜像

基于容器制作镜像

  • 在docker中拉busybox的镜像
[root@localhost ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
bdbbaa22dec6: Pull complete 
Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
  • 创建私有镜像
[root@salt1 ~]# docker run -name qq -it busybox
unknown shorthand flag: 'n' in -name
See 'docker run --help'.
[root@salt1 ~]# docker run --name qq -it busybox
/ # mkdir /data
/ # echo 'hello qiang' > /data/index.html
/ # cat /data/index.html 
hello qiang

// 在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行

  • 给予新创建镜像名字和标识
[root@salt1 ~]# docker commit -p qq
sha256:55757b22dd70e0b44f39a52cd15f62d5553e1d22fc4c1723f2d2020bc9849ab0
[root@salt1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
                            55757b22dd70        8 hours ago         1.22MB
[root@salt1 ~]# docker tag 55757b22dd70 junqiang1000/qq:v0.1
[root@salt1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
junqiang1000/qq     v0.1                55757b22dd70        8 hours ago         1.22MB

// 仓库名为qq,所以我们要在Docker Hub上创建一个名为qq的仓库,然后再将我们做好的镜像push上去
docker之制作镜像_第1张图片

  • 在linux中登陆docker账号
[root@salt1 ~]# 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: junqiang1000
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@salt1 ~]# docker push junqiang1000/qq:v0.1
The push refers to repository [docker.io/junqiang1000/qq]
4c579b0d0b74: Pushed 
a6d503001157: Mounted from library/busybox 
v0.1: digest: sha256:b98ca90d751226e1b1a530bf9803a8eec9a8dff0688dcae3e3664329e2b29ac2 size: 734
  • 使用新镜像创建容器
[root@salt1 ~]# docker run --name app -it junqiang1000/qq:v0.1
/ # ls
bin   dev   home  root  tmp   var
data  etc   proc  sys   usr
/ # ls /data/
index.html
/ # cat /data/index.html 
hello qiang
  • 重新生成镜像并上传
[root@salt1 ~]# docker commit -a 'sean <[email protected]>' -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p qq junqiang1000/qq:v0.2
sha256:904e0fff574a87932ad64e4692b3c58ae376bb0f9a6f3fcb236854a4c208bc3c
[root@salt1 ~]# docker push junqiang1000/qq
The push refers to repository [docker.io/junqiang1000/qq]
e2999df6e0cc: Layer already exists 
a6d503001157: Layer already exists 
v0.1: digest: sha256:649c8aaeb57425ee89d3b10cee2e716108217d23704fc0b0a9212db83b9fc4d5 size: 734
  • 利用新镜像生成容器
[root@salt1 ~]# docker run --name app1 -d junqiang1000/qq:v0.2
6b1ba8a25b9cd81f65c68ae55fda80028328ea794eb0fabd886daea608ac814c
  • 查看ip端口
[root@salt1 ~]# docker inspect app1
[
    {
         ......
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
       ......
    }
]
  • 查看网页
[root@salt1 ~]# curl 172.17.0.3
hello qiang

你可能感兴趣的:(Linux课程)