首页 > 其他分享 >docker构建镜像——Dockerfile

docker构建镜像——Dockerfile

时间:2024-04-03 13:56:05浏览次数:35  
标签:centos root repo yum3 0.0 镜像 docker Dockerfile

COPY

格式

COPY [--chown=<用户>:<组>]<源>...<目的>
COPY [--chown=<用户>:<组>]["<源>"..."<目的>"]

案例

[root@hmm ~]# mkdir  yum3-centos
[root@hmm ~]# ls
anaconda-ks.cfg  docker-hello  hello.c          sizetest  yum3-centos
Dockerfile       hello         hello-world.tar  test1
[root@hmm ~]# cd yum3-centos/
[root@hmm yum3-centos]# ls
[root@hmm yum3-centos]# touch Dockerfile
[root@hmm yum3-centos]# touch nginx.repo
[root@hmm yum3-centos]# pwd
/root/yum3-centos
[root@hmm yum3-centos]# vim Dockerfile
-bash: vim: command not found
[root@hmm yum3-centos]# vi Dockerfile
[root@hmm yum3-centos]# vi nginx.repo
[root@hmm yum3-centos]# docker images
REPOSITORY                                           TAG        IMAGE ID       CREATED         SIZE
registry                                             latest     9363667f8aec   2 weeks ago     25.4MB
centos-with-nginx                                    latest     fe0f7fc1d27c   2 weeks ago     248MB
imglayers-test                                       latest     3d2d641327e8   2 weeks ago     196MB
my-hello                                             latest     e8c9e00260a8   3 weeks ago     861kB
registry.cn-hangzhou.aliyuncs.com/hanmiao_03/hmmmm   24.03.26   e8c9e00260a8   3 weeks ago     861kB
127.0.0.1:5000/ubuntu                                latest     ca2b0f26964c   4 weeks ago     77.9MB
ubuntu                                               latest     ca2b0f26964c   4 weeks ago     77.9MB
httpd                                                latest     ac45b24b92cc   2 months ago    167MB
hm/ubuntu                                            hhh        f9a80a55f492   10 months ago   63.2MB
ubuntu                                               18.04      f9a80a55f492   10 months ago   63.2MB
hello-world                                          latest     d2c94e258dcb   11 months ago   13.3kB
127.0.0.1:5000/centos                                v1         5d0da3dc9764   2 years ago     231MB
centos                                               latest     5d0da3dc9764   2 years ago     231MB
ubuntu                                               14.04      13b66b487594   3 years ago     197MB
[root@hmm yum3-centos]# docker run centos-with-nginx
[root@hmm yum3-centos]# docker build -t 'yun3-copy1' .
[+] Building 0.2s (7/7) FINISHED                            docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 99B                                   0.0s
 => [internal] load metadata for docker.io/library/centos:latest      0.0s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [internal] load build context                                     0.0s
 => => transferring context: 140B                                     0.0s
 => [1/2] FROM docker.io/library/centos:latest                        0.0s
 => [2/2] COPY ./nginx.repo /etc/yum.repos.d/                         0.0s
 => exporting to image                                                0.0s
 => => exporting layers                                               0.0s
 => => writing image sha256:592cacbb3f593d986c578fdb9e29ea0e806b057b  0.0s
 => => naming to docker.io/library/yun3-copy1                         0.0s
[root@hmm yum3-centos]# docker images
REPOSITORY                                           TAG        IMAGE ID       CREATED         SIZE
yun3-copy1                                           latest     592cacbb3f59   5 seconds ago   231MB
registry                                             latest     9363667f8aec   2 weeks ago     25.4MB
centos-with-nginx                                    latest     fe0f7fc1d27c   2 weeks ago     248MB
imglayers-test                                       latest     3d2d641327e8   2 weeks ago     196MB
my-hello                                             latest     e8c9e00260a8   3 weeks ago     861kB
registry.cn-hangzhou.aliyuncs.com/hanmiao_03/hmmmm   24.03.26   e8c9e00260a8   3 weeks ago     861kB
127.0.0.1:5000/ubuntu                                latest     ca2b0f26964c   4 weeks ago     77.9MB
ubuntu                                               latest     ca2b0f26964c   4 weeks ago     77.9MB
httpd                                                latest     ac45b24b92cc   2 months ago    167MB
hm/ubuntu                                            hhh        f9a80a55f492   10 months ago   63.2MB
ubuntu                                               18.04      f9a80a55f492   10 months ago   63.2MB
hello-world                                          latest     d2c94e258dcb   11 months ago   13.3kB
127.0.0.1:5000/centos                                v1         5d0da3dc9764   2 years ago     231MB
centos                                               latest     5d0da3dc9764   2 years ago     231MB
ubuntu                                               14.04      13b66b487594   3 years ago     197MB
[root@hmm yum3-centos]# docker run -ti yun3-copy1
[root@5e60d54ffd46 /]# cd /etc/yum.repos.d/
[root@5e60d54ffd46 yum.repos.d]# ls
CentOS-Linux-AppStream.repo	     CentOS-Linux-HighAvailability.repo
CentOS-Linux-BaseOS.repo	     CentOS-Linux-Media.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Plus.repo
CentOS-Linux-Debuginfo.repo	     CentOS-Linux-PowerTools.repo
CentOS-Linux-Devel.repo		     CentOS-Linux-Sources.repo
CentOS-Linux-Extras.repo	     nginx.repo
CentOS-Linux-FastTrack.repo
[root@5e60d54ffd46 yum.repos.d]# cat nginx.repo 
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/$releasever/$basearch
gpgcheck=0
enabled=1

源路径必须位于构建上下文中

使用copy的时候超出当前构建范围,不管是使用相对路径还是使用绝对路径,构建镜像的时候都会报错

  • 使用相对路径构建
[root@hmm yum3-centos]# docker build -t  yum3-copy2 .
[+] Building 0.0s (7/7) FINISHED                            docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 135B                                  0.0s
 => [internal] load metadata for docker.io/library/centos:latest      0.0s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [1/3] FROM docker.io/library/centos:latest                        0.0s
 => [internal] load build context                                     0.0s
 => => transferring context: 31B                                      0.0s
 => CACHED [2/3] COPY ./nginx.repo /etc/yum.repos.d/                  0.0s
 => ERROR [3/3] COPY ../../home/hmm/hello.txt /root                   0.0s
------
 > [3/3] COPY ../../home/hmm/hello.txt /root:
------
Dockerfile:3
--------------------
   1 |     FROM centos
   2 |     COPY ./nginx.repo /etc/yum.repos.d/
   3 | >>> COPY ../../home/hmm/hello.txt /root    #报错
   4 |     CMD /bin/bash
   5 |     
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 77c8e775-3806-4851-a513-b1983d25edd4::03l8kntfbdcyvyt4tw33xeuv5: "/home/hmm/hello.txt": not found
[root@hmm yum3-centos]

#不管源文件是用的绝对路径还是相对路径,只要超出了当前路径那么就会失败,报错
  • 使用绝对路径
[+] Building 0.0s (7/7) FINISHED                            docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 139B                                  0.0s
 => [internal] load metadata for docker.io/library/centos:latest      0.0s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [1/3] FROM docker.io/library/centos:latest                        0.0s
 => [internal] load build context                                     0.0s
 => => transferring context: 31B                                      0.0s
 => CACHED [2/3] COPY ./nginx.repo /etc/yum.repos.d/                  0.0s
 => ERROR [3/3] COPY /root/yum3-centos/nginx.repo /root               0.0s
------
 > [3/3] COPY /root/yum3-centos/nginx.repo /root:
------
Dockerfile:3
--------------------
   1 |     FROM centos
   2 |     COPY ./nginx.repo /etc/yum.repos.d/
   3 | >>> COPY /root/yum3-centos/nginx.repo /root #报错
   4 |     CMD /bin/bash
   5 |
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 77c8e775-3806-4851-a513-b1983d25edd4::pu9wv1zzwb7w9tuck529visuq: "/root/yum3-centos/nginx.repo": not found
[root@hmm yum3-centos]#

如果源是目录,则复制目录的整个内容,包括文件系统元数据。

[root@hmm yum3-centos]# vi Dockerfile 
[root@hmm yum3-centos]# docker build -t yum3-copy3 .
[+] Building 0.3s (8/8) FINISHED                            docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 130B                                  0.0s
 => [internal] load metadata for docker.io/library/centos:latest      0.0s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [1/3] FROM docker.io/library/centos:latest                        0.0s
 => [internal] load build context                                     0.1s
 => => transferring context: 962.10kB                                 0.1s
 => CACHED [2/3] COPY ./nginx.repo /etc/yum.repos.d/                  0.0s
 => [3/3] COPY ./lunisolar-main/  /home/                              0.0s
 => exporting to image                                                0.1s
 => => exporting layers                                               0.1s
 => => writing image sha256:84ae402461bb0b18255a4fbe8dfd918f35e26072  0.0s
 => => naming to docker.io/library/yum3-copy3                         0.0s
[root@hmm yum3-centos]# docker run yum3-copy3
[root@hmm yum3-centos]# docker run -ti yum3-copy3
[root@153b880fd808 /]# ls 
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@153b880fd808 /]# cd /home/
[root@153b880fd808 home]# ls
CHANGELOG.md  babel.config.js	   package.json      tsconfig.json
LICENSE       changelog-config.js  pnpm-lock.yaml    typings
README.md     docs		   rollup.config.js
__tests__     jest.config.ts	   src
[root@153b880fd808 home]#

多个源

#如果直接指定多个源,或者源中使用了通配符,则目的路径必须是目录,并且必须以斜杠(/)结尾
[root@hmm yum3-centos]# ls
Dockerfile  lunisolar-main  nginx.repo
[root@hmm yum3-centos]# vim Dockerfile 
[root@hmm yum3-centos]# cat Dockerfile 
FROM centos
COPY ./luni* nginx.repo /home/
CMD /bin/bash
[root@hmm yum3-centos]# docker build -t "yun3-copy5" .
[+] Building 0.1s (7/7) FINISHED                            docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 94B                                   0.0s
 => [internal] load metadata for docker.io/library/centos:latest      0.0s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [internal] load build context                                     0.0s
 => => transferring context: 10.89kB                                  0.0s
 => CACHED [1/2] FROM docker.io/library/centos:latest                 0.0s
 => [2/2] COPY ./luni* nginx.repo /home/                              0.0s
 => exporting to image                                                0.0s
 => => exporting layers                                               0.0s
 => => writing image sha256:d4277a7b443afe5bc46923ee7beb05eef0364295  0.0s
 => => naming to docker.io/library/yun3-copy5                         0.0s
[root@hmm yum3-centos]# docker run -ti yun3-copy5
[root@34cfdea289ae /]# cd /home/
[root@34cfdea289ae home]# ls
CHANGELOG.md  babel.config.js	   nginx.repo	     src
LICENSE       changelog-config.js  package.json      tsconfig.json
README.md     docs		   pnpm-lock.yaml    typings
__tests__     jest.config.ts	   rollup.config.js
[root@34cfdea289ae home]# 

  • 如果目的路径不以斜杠结尾,则它将被视为常规文件,源内容将被写入目录路径(简单来说就是会重命名)

  • 如果目的路径不存在,则会与其路径中所有缺少的目录一起被创建。(简单来说就是,会自动创建路径)

ADD

实例

[root@hmm yum3-centos]# docker build -t 'yun3-add' .
[+] Building 0.2s (10/10) FINISHED              docker:default
 => [internal] load build definition from Dockerfile      0.0s
 => => transferring dockerfile: 189B                      0.0s
 => [internal] load metadata for docker.io/library/cento  0.0s
 => [internal] load .dockerignore                         0.0s
 => => transferring context: 2B                           0.0s
 => CACHED [1/4] FROM docker.io/library/centos:latest     0.0s
 => [internal] load build context                         0.0s
 => => transferring context: 413.14kB                     0.0s
 => http://mirrors.aliyun.com/repo/Centos-8.repo          0.1s
 => [2/4] ADD nginx.repo /etc/yum.repos.d/                0.0s
 => [3/4] ADD http://mirrors.aliyun.com/repo/Centos-8.re  0.0s
 => [4/4] ADD lunisolar-main.zip /home/yun3/              0.0s
 => exporting to image                                    0.0s
 => => exporting layers                                   0.0s
 => => writing image sha256:eb713753ec15edfd95fe4feba80d  0.0s
 => => naming to docker.io/library/yun3-add               0.0s
[root@hmm yum3-centos]# docker run -ti yun3-add
[root@ec222ba8717e /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@ec222ba8717e /]# cd root/
[root@ec222ba8717e ~]# ls
Centos-8.repo	 anaconda-post.log
anaconda-ks.cfg  original-ks.cfg
[root@ec222ba8717e ~]# cd /home/
[root@ec222ba8717e home]# ls
yun3
[root@ec222ba8717e home]# cd yun3/
[root@ec222ba8717e yun3]# ls
lunisolar-main.zip
[root@ec222ba8717e yun3]# cd /etc/yum.repos.d/
[root@ec222ba8717e yum.repos.d]# ls
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
nginx.repo

标签:centos,root,repo,yum3,0.0,镜像,docker,Dockerfile
From: https://www.cnblogs.com/humlogs/p/18111153

相关文章

  • 为Python应用选择最好的Docker镜像 转载
    在使用Python的早些年,为了解决Python包的隔离与管理virtualenvwrapper就成为我的工具箱中重要的一员。后来,随着Python3的普及,virtualenvwrapper逐渐被venv所替换。毕竟venv是Python3的标配,优点是显而易见的。而这几年,应用场景的的复杂性越来与高,无论是开发还是部......
  • docker------docker入门
    ......
  • 【Docker】专题六:Docker Registry 详解
    以下内容均来自个人笔记并重新梳理,如有错误欢迎指正!如果对您有帮助,烦请点赞、关注、转发!欢迎扫码关注个人公众号!目录一、基本介绍二、Registry创建方法三、Registry常用API请求四、Registry镜像清理一、基本介绍笔者在【Docker】专题一:Docker基本架构 中介绍......
  • [转]docker compose 配置文件 .yml 全面指南
    原文地址:dockercompose配置文件.yml全面指南-知乎Compose是一个用于定义和运行多容器Docker的工具。借助Compose,您可以使用YAML文件来配置应用程序的服务。使用Compose基本上分为三步:使用Dockerfile定义您的应用程序的环境使用docker-compose.yml定义组成......
  • Docker服务以及容器如何设置设备开机自动启动
    如果碰到断电重启,在没有设置自动启动的情况下,我们所有应用(比如redis)都需要重新手动启动一遍,这样就非常麻烦,不能及时处理,所以本文来讲解一下如果设置自动启动,话不多说,直接开始。一、Docker服务设置自动启动使用dockerrun命令创建容器时没有添加参数--restart=always,导致的后果......
  • Docker:探索容器化技术,重塑云计算时代应用交付与管理
    一,引言    在云计算时代,随着开发者逐步将应用迁移至云端以减轻硬件管理负担,软件配置与环境一致性问题日益凸显。Docker的横空出世,恰好为软件开发者带来了全新的解决方案,它革新了软件的打包、分发和管理方式,让开发人员能够运用先进的自动化运维工具和理念,实现在不同环......
  • 系统学习Docker:1_Docker简介以及2_安装Docker
    01-Docker简介什么是Docker在不同的机器和操作系统中安装运行环境和依赖库是一件很烦人的事情,容器就是为了解决这一问题而出现的技术。容器是一种虚拟化技术,将应用程序及其依赖项(环境、系统工具等)打包到一个独立的可移植环境中。这个运行环境就是容器。Docker是一个能把......
  • Container容器:未来的最终解:Docker(入门导览)
    容器容器:可以无视机器、系统限制的时刻使用任何的软件或程序的虚拟机-容器解释:[什么是容器?|IBM备注:Docker本身并不是容器,它是创建容器的工具,是应用容器引擎优势:docker虚拟机内存轻量占用大设备几乎支持所有电子设备主要PC主机镜像复用可以打包到官方仓库,云端下载需要点......
  • 将asp.net core 程序打包成docker镜像,并创建容器
    一、编写DockerfileFROMmcr.microsoft.com/dotnet/aspnet:7.0//指定基础镜像asp.netcore7COPYdist/appWORKDIR/appEXPOSE80/tcpENTRYPOINT["dotnet","App.dll"]二、将asp.netcore程序打包成镜像dockerbuild-t镜像名-fDockerfile三、创建容器1......
  • docker下mysql连接数修改后不生效问题的解决
    使用容器的方式使用mysql,在创建mysql容器的时候,映射了配置文件的路径(我映射的路径是:/home/env/mysql/config/),当修改了路径中配置文件中的最大连接数后,重启容器后,发现参数是不生效的情况,你遇到过吗?先说结果:是权限的问题,777权限会被mysql认为是安全的问题,忽略这种文件。具体如下:......