docker基础学习之二:docker commit及Dockerfile构建镜像,私有镜像仓库的镜像上传及下载

一、Ubuntu系统安装docker
因为申请的是华为公有云免费的Ubuntu系统,先安装docker

1、编辑/etc/apt/sources.list文件, 在文件最前面添加(操作前请做好相应备份):
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

2、更新源
sudo apt update
sudo apt upgrade

3、安装Docker
sudo apt install docker.io

4、启动Docker服务
sudo systemctl start docker

5、查看docker 版本
docker version

二、docker commit构建镜像
docker commit命令:可将一个运行中的容器保存为镜像,其运行过程可总结如下:
运行一个容器
修改容器内容
将修改完内容的容器保存为镜像

1、先用docker pull **命令去互联网拉取两个镜像nginx、httpd。

2、查看本地镜像。
root@herrychen:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 602e111c06b6 46 hours ago 127MB
httpd latest b2c2ab6dcf2e 2 days ago 166MB

3、创建名为nginx1的容器,看到欢迎来到nginx,表示容器跑起来了。
root@herrychen:~# docker run --name nginx1 -d -p 80:80 nginx
0c2e2dbfd6285dc3def78636af5d3c49e786cfdb392555e4399e5c946b1639c1
root@herrychen:~# curl 127.0.0.1 80

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

curl: (7) Couldn't connect to server

4、进入容器内部修改主页,
root@herrychen:~# docker exec -it 0c2e2 bash
root@0c2e2dbfd628:/# cd /usr/share/nginx/html/
root@0c2e2dbfd628:/usr/share/nginx/html# ls
50x.html index.html
root@0c2e2dbfd628:/usr/share/nginx/html# echo “update the nginx index” > index.html
root@0c2e2dbfd628:/usr/share/nginx/html# exit
exit
root@herrychen:~# curl 127.0.0.1:80
update the nginx index

5、docker commit命令将修改完内容的容器保存为新镜像
在这里插入图片描述

6、查看镜像,确实有nginx: v1.2 和nginx两个镜像了。
root@herrychen:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1.2 329de503b8d0 2 minutes ago 127MB
53fff9476e57 6 minutes ago 127MB
nginx latest 602e111c06b6 47 hours ago 127MB
httpd latest b2c2ab6dcf2e 2 days ago 166MB

docker rmi命令,删除之前误操作的无用镜像
root@herrychen:~# docker rmi 53fff9476e57
Deleted: sha256:53fff9476e572001e79eff6e83b68a8949b675903c09a9f08f49cdae44381c6c
如果镜像被容器调用,就无法删除了,先要删除相关的容器(docker stop**, docker rm **),再删除镜像docker rmi **

7、比较这两个镜像可以很好的理解分层镜像的概念。
在这里插入图片描述

三、Dockerfile构建镜像

1、Dockerfile概念
Dockerfile是文件指令集,描述如何自动创建Docker镜像
Dockerfile是包含若干指令的文本文件,可以通过这些指令创建出docker image
Dockerfile文件中的指令执行后,会创建一个个新的镜像层
Dockerfile文件中的注释以"#"开始
Dockerfile一般由4部分组成:
基础镜像信息
维护者信息
镜像操作指令
容器启动指令
bulid context 为镜像构建提供所需的文件或目录。

2、Dockerfile常用指令

在这里插入图片描述

3、先编辑一个配置文件
root@herrychen:~# mkdir dockerfile
root@herrychen:~# pwd
/root

root@herrychen:~# cd dockerfile
root@herrychen:~/dockerfile# ls
root@herrychen:~/dockerfile# vim dockerfile01
FROM httpd
MAINTAINER herrychen
RUN echo “dockerfile test” > /usr/local/apache2/htdocs/index.html

4、docker build命令用于根据给定的Dockerfile和上下文以构建Docker镜像。
Build方式需要写一个配置文件,然后利用当前是已存在的image,按照配置文件进行调整生成新的image。
-t,镜像的名字及tag
-f,Dockerfile的完整路径

root@herrychen:~/dockerfile# docker build -t httpd:v11 -f dockerfile01 /root/dockerfile
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM httpd
—> b2c2ab6dcf2e
Step 2/3 : MAINTAINER herrychen
—> Using cache
—> b47014fc2621
Step 3/3 : RUN echo “dockerfile test” > /usr/local/apache2/htdocs/index.html
—> Using cache
—> 5472bd905bde
Successfully built 5472bd905bde
Successfully tagged httpd:v11

root@herrychen:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd v11 5472bd905bde 8 minutes ago 166MB
nginx v1.2 329de503b8d0 About an hour ago 127MB
nginx latest 602e111c06b6 2 days ago 127MB
httpd latest b2c2ab6dcf2e 2 days ago 166MB

5、使用镜像httpd:v11来创建容器成功。
root@herrychen:~/dockerfile# docker run -d -p 8081:80 httpd:v11
c5c6187b5e60ed3056b4a15ef3a34b412a8ae2b1aeb03afefc23b0515315f166
看到主页的内容为之前配置文件里编辑的内容了。
root@herrychen:~/dockerfile# curl 127.0.0.1:8081
dockerfile test

四、创建私有镜像仓库
registry是存放容器镜像的仓库,用户可以进行镜像下载和访问,分为公有和私有两类registry
公有镜像仓库
docker hub是Docker公司为公众提供的托管registry
私有镜像仓库
企业可以用docker registry构建私有的registry,其本身是一个开源项目,可以用于搭建私有registry。

1、私有镜像仓库其实也是个容器。需要挂载本地目录。
root@herrychen:~# mkdir myregistry
root@herrychen:~# docker run -d -p 1000:5000 -v /root/myregistry:/var/lib/registry registry
-v 表示把本地的/root/myregistry挂载到容器里存放镜像的目录/var/lib/registry,创建的镜像是 registry镜像

Unable to find image ‘registry:latest’ locally
latest: Pulling from library/registry
486039affc0a: Pull complete
ba51a3b098e6: Pull complete
8bb4c43d6c8e: Pull complete
6f5f453e5f2d: Pull complete
42bc10b72f42: Pull complete
Digest: sha256:7d081088e4bfd632a88e3f3bcd9e007ef44a796fddfe3261407a3f9f04abe1e7
Status: Downloaded newer image for registry:latest
acbc44e0b486bae39d73073a6787b0f8fc770154cbf9ba40accd6e25a25a848e

root@herrychen:~# curl 127.0.0.1:1000/v2/_catalog
{“repositories”:[“michael/httpd”]}

root@herrychen:~# ls
dockerfile myregistry
root@herrychen:~# cd myregistry
root@herrychen:~/myregistry# cd docker/registry/v2/repositories/michael/httpd/_layers/sha256/
直接输入镜像层验证码所在的位置
root@herrychen:~/myregistry/docker/registry/v2/repositories/michael/httpd/_layers/sha256# ls
1e75357b4a1f2a749b504ff067eabbcbed16b183a8443e9601179c0ec861adb2 8219e18ac429a73656e9123013a8a236281a3b952c49a0934d6969a31957dcba
3ae1b816f5e1ebc28f7044e1431eb6516bca574739cbf4025816c01a150e4b78 889bcd3261f7791a58f805f9a9804b1012311e05852dc79e77f971255034513f
4f6febfae8db62875b2841597bf8c07e6da508b9aee3c11ab999a3a44bf4f52b a5aa59ad8b5e8f77a232f42938287b07f2bb3487a55bcb4cabce5b53cd674f44
54fec2fa59d0a0de9cd2dec9850b36c43de451f1fd1c0a5bf8f1cf26a61a5da4

看到了镜像的7个层,说明上传到私有镜像仓库myregistry成功了。

root@herrychen:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd v11 1e75357b4a1f 17 minutes ago 166MB
127.0.0.1:1000/michael/httpd v11 1e75357b4a1f 17 minutes ago 166MB
nginx latest 602e111c06b6 2 days ago 127MB
httpd latest b2c2ab6dcf2e 2 days ago 166MB
registry latest 708bc6af7e5e 3 months ago 25.8MB

2、先删除V11这两个镜像,便于之后的操作
root@herrychen:~# docker rmi 127.0.0.1:1000/michael/httpd:v11
root@herrychen:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 602e111c06b6 2 days ago 127MB
registry latest 708bc6af7e5e 3 months ago 25.8MB

3、从私有镜像仓库拖下来v11的镜像
root@herrychen:~# docker pull 127.0.0.1:1000/michael/httpd:v11
v11: Pulling from michael/httpd
54fec2fa59d0: Already exists
8219e18ac429: Pull complete
3ae1b816f5e1: Pull complete
a5aa59ad8b5e: Pull complete
4f6febfae8db: Pull complete
889bcd3261f7: Pull complete
Digest: sha256:f04c974d612e68109ed13521afdbfe76f62ae786f9c06cfc4262b5e9b8fb3c92
Status: Downloaded newer image for 127.0.0.1:1000/michael/httpd:v11
127.0.0.1:1000/michael/httpd:v11

4、查看镜像已经拖下来了。
root@herrychen:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:1000/michael/httpd v11 1e75357b4a1f 29 minutes ago 166MB
nginx latest 602e111c06b6 2 days ago 127MB
registry latest 708bc6af7e5e 3 months ago 25.8MB

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值