Docker镜像创建、镜像push

admin
2023-04-08 / 0 评论 / 82 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年04月08日,已超过355天没有更新,若内容或图片失效,请留言反馈。

镜像创建、镜像push

是什么

Dockerfile是用于构建docker镜像的文件

Dockerfile里包括了构建镜像所需的“指令”

Dockerfile有其特定的语法规则

基本结构

例如:在一台ubuntu 21.04上运行下面这个hello.py的Python程序

不用daocker步骤:

1、编写hello.py文件。

print("hello docker")

2、准备Python环境

apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev

3、运行hello.py

$ python3 hello.py
hello docker

使用Dockerfile构建镜像后就这样

FROM ubuntu:20.04
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
ADD hello.py /
CMD ["python3", "/hello.py"]

说明:

FROM:导入一个基础镜像

RUN:要运行的linx命令

ADD:添加文件到docker镜像的指定目录

CMD:使用该镜像创建容器的时候,要执行的命令。

构建镜像

将写好的hello.py文件和Dockerfile文件放在一起进入该目录。

构建镜像

docker image build -t 容器名称:版本 构建镜像存放路径

例如:

docker image build -t hello .

image-20230408200328380

注意:如果不写版本号默认是latest。t:表示标签

例如,构建上面的py镜像:

docker image build -t i2cn/hello:1.0 .

image-20230408200139826

构建镜像名称是hell,版本默认是latest,存放位置当前文件夹下。

image-20230408200430346

注意:因为是根据一个image创建的,image的id是相同。

镜像提交到DockerHub

需要符合DockerHub命名规范。

根据现有image修改tag生成一个新image tag:

docker image tag 容器名称 新容器名称/名称:版本
docker image tag i2cn/hello:1.0 i2cn/hello:1.1.0

image-20230408200704669

登录DockerHhub:

docker login

会提示输入用户名密码。

推送自定义镜像到DockerHub

docker image push 镜像名称:tag

注意:如果是要推送到自己的DockerHub一定要取一个自己账号开头的镜像名称。

例如推送上面自己构建的镜像:

docker image push i2cn/hello:1.0

image-20230408201133110

打开Docker hub自己仓库,就可以看到push的镜像了。

image-20230408201300712

测试拉去push的镜像

删除本地镜像:

docker image rm i2cn/hello:1.0

image-20230408201516507

从新拉取推送到dockerhub的镜像

docker image pull i2cn/hello:1.0

image-20230408201711909

执行镜像:

docker container run -it i2cn/hello:1.0

image-20230408202006537

可以看到输出:hello docker

测试成功。

commit创建镜像

将现有容器的改变,commit成一个新的镜像。

例如:启动一个nginx容器,修改nginx的欢迎页面信息,然后重新commit成一个新的镜像,如果要使用改变后的nginx容器,则可以直接执行commiit后的镜像即可。

执行nginx镜像,运行nginx容器,查看nginx首页。

docker container run -d -p 80:80 nginx

image-20230408203627476

image-20230408203651232

进入nginx修改首页信息

docker exec -it 8872c sh

image-20230408204345857

进入nginx容器首页文件路径

cd /usr/share/nginx/html

修改首页信息

echo "<h1>hello word </h1>" > index.html

用echo命令,将“

hello word

”写入index.html

查看首页信息

image-20230408204536234

重点:

使用commit命令,将修改后的nginx容器,commit成一个镜像。

停止nginx容器运行

docker container stop 8872c

commit容器成一个新镜像:

image-20230408205003992

docker container commit 8872 i2cn/nginx:1.0.0

image-20230408205020339

查看通过commit新生成的镜像:

image-20230408205109631

创建容器,commit创建镜像

1、创建ubuntu容器

docker container run -it ubuntu:21.04 sh

2、在ubuntu容器里面安装Python环境

apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev

3、创建hello.py文件,打印hello docker

echo "print('hello docker')" > /hello.py

4、推出ubuntu容器,通过commit打包ubuntu容器。

docker container commit ubuntu i2cn/pydemo:1.0

5、通过打包后的pydemo:1.0镜像运行容器。

docker container run i2cn/pydemo:1.0 python3 /hello.py 

这种方法和离线导入导出不怎么使用。

查看镜像分层

docker image history 镜像名称

例如查看nginx分层信息

docker image history nginx

scratch特殊镜像

scratch镜像是一个为空的基础镜像。

1

评论 (0)

取消