【Docker】docker日常工作总结(Dockerfile语法、编写规则)

导读:本篇文章讲解 【Docker】docker日常工作总结(Dockerfile语法、编写规则),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

今日状态:保持学习
打卡学习 星期一 星期二 星期三 星期四 星期五 星期六 星期日
成功 成功

一、案例:创建自己的镜像

在这里插入图片描述

🌙1、新建fun.py文件

from cowpy import cow
print(cow.milk_random_cow("hello,dev07"))                                            

🌙2、准备安装包requirement.txt文件

cowpy==1.1.0                 

🌙3、制作镜像文件Dockerfile

FROM python:3.8-alpine

LABEL maintaier="kobe<118@qq.com>"
LABEL description="Dockerfile demo"

WORKDIR /myapp

COPY . .

RUN pip install -r requirements.txt

CMD python fun.py

🌙4、创建镜像(kobeya/myapp)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker build -t kobeya/myapp .
[sudo] zhilong 的密码: 
Sending build context to Docker daemon  5.632kB
Step 1/7 : FROM python:3.8-alpine
 ---> 926e859df334
Step 2/7 : LABEL maintaier="kobe<118@qq.com>"
 ---> Using cache
 ---> d77df09a3a66
Step 3/7 : LABEL description="Dockerfile demo"
 ---> Using cache
 ---> 0ffecf31e2c8
Step 4/7 : WORKDIR /myapp
 ---> Using cache
 ---> 82c7391ed055
Step 5/7 : COPY . .
 ---> a9c9ec281098
Step 6/7 : RUN pip install -r requirements.txt
 ---> Running in 383bcce3aa82
Collecting cowpy==1.1.0
  Downloading cowpy-1.1.0.tar.gz (14 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Building wheels for collected packages: cowpy
  Building wheel for cowpy (setup.py): started
  Building wheel for cowpy (setup.py): finished with status 'done'
  Created wheel for cowpy: filename=cowpy-1.1.0-py3-none-any.whl size=10656 sha256=7d634e53dbc90a9945272136b55393e8b6fec0ef3069146c0f076be796be1cfc
  Stored in directory: /root/.cache/pip/wheels/26/23/e0/6d551b2cf42dd0e359edf54fab392e64a3bfb0d197759ef03e
Successfully built cowpy
Installing collected packages: cowpy
Successfully installed cowpy-1.1.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 383bcce3aa82
 ---> 8d9ee514385b
Step 7/7 : CMD python fun.py
 ---> Running in e0f5044cd5bc
Removing intermediate container e0f5044cd5bc
 ---> 6a75149dde6d
Successfully built 6a75149dde6d
Successfully tagged kobeya/myapp:latest

5、🌙查看镜像

在这里插入图片描述

🌙6、运行镜像

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker run --rm kobeya/myapp:latest
 _____________ 
< hello,dev07 >
 ------------- 
  o
   o    (__)
        $$\
       ('') \---------
          \           \
           |          |\
           ||---(  )_|| *
           ||    UU  ||
           ==        ==

二、Dockerfile语法

🌙1、注释:

可以使用#指定注释信息,不会当作构建指令

🌙2、往往文件名写成Dockerfile

Dockerfile的作用:可以使用Dockerfile文件自定义镜像

🌙3、一个Dockerfile文件中,至少需要一个FROM,并且在首行

当前FROM有三种类型

1、FROM 镜像名(首先会在本地查看有没有这个镜像,如果没有去docker hub中找latest),最好不要指定最新版本的,因为有版本差异
2、FROM 镜像名:tag(建议使用)
3、FROM 镜像名@digest

🌙4、使用FROM 指定base 镜像

FROM python:3.8-alpine

🌙5、LABEL:指定元素信息和描述,

LABEL key value
或者
LABEL key1=value1 key2=value2(建议使用)

LABEL maintaier="kobe<118@qq.com>"
LABEL description="Dockerfile demo"

🌙6、WORKDIR:WORKDIR 指定进入到容器的特定目录中,相当于linux命令中的cd

🔥运行容器会将Dockerfile中的文件复制到WORKDIR指定的目录

格式:WORKDIR 容器中的目录

WORKDIR /myapp

🔥注意1:

WORKDIR /myapp
WORKDIR aa/ 

相对路径: 如果有2个WORKDIR相当于进入/myapp/aa/

🔥注意2:

WORKDIR /myapp
WORKDIR /aa/ 

绝对路径:WORKDIR /aa/ 如果有2个WORKDIR相当于进入/aa/

🌙7、COPY:用于将宿主机中(安装docker的机器)的资源复制到容器中,进行容器共享

🔥COPY:用于将宿主机中(安装docker的机器)的资源复制到容器中,进行容器共享

格式:COPY 宿主机文件或者目录 容器中的文件或者目录

COPY . .

. :将Dockerfile同级目录下的文件复制粘贴到容器中的myapp目录中

zhilong@zhilong-virtual-machine:~/桌面$ sudo docker run -it kobeya/myapp:latest /bin/sh
/myapp # ls
Dockerfile        fun.py            requirements.txt

注意1:

COPY fun.py /myapp/:将fun.py文件复制到myapp目录下

注意2:

COPY fun.py /aaa :复制fun.py文件到容器中,并将fun.py复制粘贴并且命名为aaa文件

🔥如果需要复制文件到容器的某个目录中,那么最好在路径后面添加/;不然结果为将fun.py复制粘贴并且命名为aaa文件

🌙8、使用docker build来构建镜像 Makefile

sudo docker build -t kobe/myapp:v1 .

1、-t:指定构建镜像的名称和tag号,如果不指定tag,那么会自动使用latest
2、-f:指定Dockerfile文件的路径,如果命名为Dockerfile并且在当前路径下,那么不指定-f参数
3、. :指定构建时的上下文数据
4、格式:docker build -t 镜像名:tag -f Dockerfile文件 .

🌙9、ADD:ADD与COPY命令功能相似,用于将宿主机或者远程的资源复制到容器中

可以将本地的压缩文件解压之后,复制到容器中
也可以将远程资源路径下载之后(不会解压),复制到容器中

ADD src.tar ./

🌙10、RUN :安装运行

🔥是在myapp目录下运行的

如果RUN运行的不止一条命令,不建议写多个RUN,可以拼接到一个RUN中,并且要分行展示

例如

RUN pip install -r requirements.txt && \
        cat fun.py && \
        ls /home

🌙11、ENV:指定环境变量

FROM python:3.8-alpine

LABEL maintaier="kobe<118@qq.com>"
LABEL description="Dockerfile demo"

WORKDIR /myapp

COPY . .

ENV myuser=zhikong \
        myage=18

RUN pip install -r requirements.txt && \
        cat fun.py && \
        ls /home


CMD python fun.py

构建,创建镜像

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker build -t kobe/myapp:v1 .
Sending build context to Docker daemon  5.632kB
Step 1/8 : FROM python:3.8-alpine
 ---> 926e859df334
Step 2/8 : LABEL maintaier="kobe<118@qq.com>"
 ---> Using cache
 ---> d77df09a3a66
Step 3/8 : LABEL description="Dockerfile demo"
 ---> Using cache
 ---> 0ffecf31e2c8
Step 4/8 : WORKDIR /myapp
 ---> Using cache
 ---> 82c7391ed055
Step 5/8 : COPY . .
 ---> 6d9bb3cb0b8d
Step 6/8 : ENV myuser=zhikong 	myage=18
 ---> Running in 643df7e12cf0
Removing intermediate container 643df7e12cf0
 ---> 99b33e3abaad
Step 7/8 : RUN pip install -r requirements.txt && 	cat fun.py && 	ls /home
 ---> Running in 476df4d1e83f
Collecting cowpy==1.1.0
  Downloading cowpy-1.1.0.tar.gz (14 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Building wheels for collected packages: cowpy
  Building wheel for cowpy (setup.py): started
  Building wheel for cowpy (setup.py): finished with status 'done'
  Created wheel for cowpy: filename=cowpy-1.1.0-py3-none-any.whl size=10656 sha256=f19df94a3979606a4a78661667a031949e185b6eb90d10bc883452f7e133f971
  Stored in directory: /root/.cache/pip/wheels/26/23/e0/6d551b2cf42dd0e359edf54fab392e64a3bfb0d197759ef03e
Successfully built cowpy
Installing collected packages: cowpy
Successfully installed cowpy-1.1.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
from cowpy import cow
print(cow.milk_random_cow("hello,dev07"))
Removing intermediate container 476df4d1e83f
 ---> 4f53a90478a3
Step 8/8 : CMD python fun.py
 ---> Running in eeeca0281985
Removing intermediate container eeeca0281985
 ---> c005edf44c7c
Successfully built c005edf44c7c
Successfully tagged kobe/myapp:v1

运行容器:

sudo docker run -it kobe/myapp:v1 env

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker run -it kobe/myapp:v1 env
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=85b6000b4128
TERM=xterm
LANG=C.UTF-8
GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568
PYTHON_VERSION=3.8.13
PYTHON_PIP_VERSION=22.0.4
PYTHON_SETUPTOOLS_VERSION=57.5.0
PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/6ce3639da143c5d79b44f94b04080abf2531fd6e/public/get-pip.py
PYTHON_GET_PIP_SHA256=ba3ab8267d91fd41c58dbce08f76db99f747f716d85ce1865813842bb035524d
myuser=zhikong
myage=18
HOME=/root

🔥注意:在命令中用-e指定的全局变量优先级高于Dockerfile指定的全局变量

🌙12、EXPOSE:将我们暴露出来的端口添加到Dockerfile文件中

格式:EXPOSE 8005
运行容器只能指定-p 8005:80
sudo docker run -it -p 8005:80 kobe/myapp:v1 /bin/sh

FROM python:3.8-alpine

LABEL maintaier="kobe<118@qq.com>"
LABEL description="Dockerfile demo"

WORKDIR /myapp

COPY . .

ENV myuser=zhikong \
        myage=18

EXPOSE 8005

RUN pip install -r requirements.txt && \
        cat fun.py && \
        ls /home


CMD python fun.py

🌙13、VOLUME:指定数据卷

VOLUME /myapp

FROM python:3.8-alpine

LABEL maintaier="kobe<118@qq.com>"
LABEL description="Dockerfile demo"

WORKDIR /myapp

COPY . .

ENV myuser=zhikong \
        myage=18

EXPOSE 8005
VOLUME /myapp

RUN pip install -r requirements.txt && \
        cat fun.py && \
        ls /home


CMD python fun.py

生成自定义镜像文件

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker build -t kobe/myapp:v1 .
Sending build context to Docker daemon  5.632kB
Step 1/10 : FROM python:3.8-alpine
 ---> 926e859df334
Step 2/10 : LABEL maintaier="kobe<118@qq.com>"
 ---> Using cache
 ---> d77df09a3a66
Step 3/10 : LABEL description="Dockerfile demo"
 ---> Using cache
 ---> 0ffecf31e2c8
Step 4/10 : WORKDIR /myapp
 ---> Using cache
 ---> 82c7391ed055
Step 5/10 : COPY . .
...
...
...

运行容器

运行命令中指定-v myapp_v6:/myapp
宿主机中的myapp_v6目录与容器中的myapp目录相映射

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker run -it -p 8005:80 -v myapp_v6:/myapp kobe/myapp:v1 /bin/sh
/myapp # ls
Dockerfile        fun.py            requirements.txt
/myapp # 

到宿主机中查看映射的数据卷:

sudo docker volume ls

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker/docker_888$ sudo docker volume ls
DRIVER    VOLUME NAME
local     4f47bec7346a7eb6c9aa649a106d04659510f03e8b4703c458fa9f2a42789433
local     5faa504dd145732e6d4e4df20029c68251ecc1caecd1ab852053d1ec36b3aec9
local     8c9f9fd267a96b952fe55c3003dfa465b4f03be8f03e4fad5ec8351c14571ea8
local     97fff49444e3bb0e0d645acb4e58dd4d858adf0602c0e1a148611b8fa2c41489
local     781caa242336789da568505678c8993526b06dcaae8838c44825daf8a715c54d
local     937eb5f474823192d093ee01a42fdf44e2ef0b4945583dc3b85f0c807d6dccbb
local     19386203fc92fe20cf84aa4bc25c59714af8ce0341192360a1fee3d8fcd90c69
local     c70b1dddf7f1fc2081beb035466b2086a1f595eb8cd5bc3feddda83e3d74a741
local     ce7599f23cc77c08cc3138a4acc6837211a3c3c011cf522fc9db29c6bc8ed6f9
local     deb359cde637d5df17b745130ce7a0cbacc01337df9413523eb24f72e8753c30
local     e579bcfef5dabdc55f7618e60fe0df72fd922e2f94759db2e988d0e70d00f76c
local     myapp_v
local     myapp_v1
local     myapp_v6
local     nnn
local     v1

查看数据卷的详细信息:

sudo docker inspect myapp_v6

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker/docker_888$ sudo docker inspect myapp_v6
[
    {
        "CreatedAt": "2022-06-03T23:29:54+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/myapp_v6/_data",
        "Name": "myapp_v6",
        "Options": null,
        "Scope": "local"
    }
]

🌙🔥14、解决cat: /var/lib/docker/volumes/myapp_v6/_data: 权限不够的问题

sudo docker run -it –privileged –pid=host debian nsenter -t 1 -m -u -n -i sh
cd /var/lib/docker/volumes/数据卷名称/_data

hilong@zhilong-virtual-machine:~/Desktop/shell_text/docker/docker_888$ cat /var/lib/docker/volumes/myapp_v6/_data
cat: /var/lib/docker/volumes/myapp_v6/_data: 权限不够
zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker/docker_888$ docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh
Unable to find image 'debian:latest' locally
latest: Pulling from library/debian
e756f3fdd6a3: Already exists 
Digest: sha256:3f1d6c17773a45c97bd8f158d665c9709d7b29ed7917ac934086ad96f92e4510
Status: Downloaded newer image for debian:latest
# ls
bin  boot  cdrom  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media	mnt  opt  proc	root  run  sbin  snap  srv  swapfile  sys  tmp	usr  var
# cd /var/lib/docker/volumes/myapp_v6/_data
# ls
Dockerfile  fun.py  requirements.txt
# exit

🌙15、CMD:是运行容器的时候默认执行的命令

CMD有两种格式

a、shell格式(不推荐格式)
b、exec格式(exec格式):CMD [“python”,“fun.py”]

CMD ["python","fun.py"]

Dockerfile文件中出现多个CMD,只会执行最后一个CMD,前面的都会被忽略

CMD ["python","fun.py"]  不执行
CMD ["ls","/"]    执行

在运行一个容器的时候,如果在镜像名称后指定其他命令,会默认把Dockerfile中的CMD命令覆盖

例如下面的命令会执行ls /

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker rmi -f kobe/myapp:v1 && sudo docker build -t kobe/myapp:v1 . && sudo docker run kobe/myapp:v1 ls /

🌙16、ENTRYPOINT

Dockerfile文件中出现多个ENTRYPOINT,只会执行最后一个ENTRYPOINT,前面的都会被忽略
在运行一个容器的时候,如果在镜像名称后指定其他命令,不会将Dockerfile中的ENTRYPOINT的命令覆盖

1、ENTRYPOINT可以和CMD一起使用

2、CMD中指定的参数会作为NETRYPOINT的默认参数;相当于cat /etc/passwd,/etc/hosts,例如

ENTRYPOINT ["cat","/etc/passwd"]
CMD ["/etc/hosts"]

3、如果在运行一个容器的时候,如果在镜像名称后指定其他命令,不会将Dockerfile中的ENTRYPOINT的命令覆盖,会将CMD的命令覆盖,并且将指定的其他命令作为ENTRYPOINT的参数

🌙17、上传镜像到docker hub

a、首选有docker hub账号
b、保证镜像名称前缀和docker hub的用户名保持一致,如果不一致需要修改镜像名称

sudo docker tag kobe/myapp:v1 zhilongya/myapp:v1

c、登录docker hub

sudo docker login

d、输入用户名和密码
f、上传镜像到docker hub

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker push zhilongya/myapp:v1
The push refers to repository [docker.io/zhilongya/myapp]
b1789c140df9: Pushed 
41de4fa5ab4b: Pushed 
4a1f4cf2b877: Pushed 
4531d365dde0: Mounted from library/python 
9e567d1bb30a: Mounted from library/python 
56bb29f4ee33: Mounted from library/python 
09c126bb3acd: Mounted from library/python 
24302eb7d908: Mounted from library/python 
v1: digest: sha256:ca0ccb61b6a0c424b62c4e5e5d6c131b612dc7196ca7b4b25e843fde9c029c8b size: 1994

在这里插入图片描述

🌙18、docker hub中下载镜像

公共的镜像能下载

sudo docker pull zhilongya/myapp

🌙19、除17外方法,还可以将镜像压缩之后,共享

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ sudo docker save -o myapp.tar zhilongya/myapp:v1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker1$ ls
docker-entrypoint.sh  Dockerfile  fun.py  myapp.tar  requirements.txt

拿的镜像的伙伴可以执行,下载

sudo docker load -i myapp.tar 

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/74035.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!