The Tips About Dockerfile

简介: Normally, we often write a `Dockerfile` in the current directory.- The `Dockerfile` is a configuration file that describes how to build the image. You can refer to the [official documentation](https://docs.docker.com/reference/dockerfile/) for more details.

This article is also posted on my blog, feel free to check the latest revision: The Tips About Dockerfile

Normally, we often write a Dockerfile in the current directory.

  • The Dockerfile is a configuration file that describes how to build the image. You can refer to the official documentation for more details.
  • If you list more than one CMD, only the last one takes effect. So if you have multiple commands to run, you better write them in a script file.
  • Docker is not the VMware, there is no systemd in the container. Its startup program is the container application process. The container exists for the main process. Once the main process exits, the container loses its meaning of existence and thus exits. So when you execute multiple commands and if they are blocking, you better write the previous commands in nohup and the last command in the blocking command. (never use the command such as CMD service nginx start, the CMD only will execute as CMD [ "sh", "-c", "service nginx start"], when the sh is executed, the container will exit, the correct way is run it directly CMD ["nginx", "-g", "daemon off;"])

Then, run the following command to build the image:

docker build -t my_image:1.0 .: the -t means tag, the . means the current directory(actually, it is the context of the dockerfile, but considering many people only use the same method, so here call it current). Besides, you can also build from .tar.gz file.

You can just refer to the official docs, but there is only one command that you should pay attention to: ENTRYPOINT.

If you want to tell the difference between CMD and ENTRYPOINT, you should first understand the shell pattern and the exec pattern.

exec pattern

The feature of exec pattern is that it will not pass the command through the shell. So the environment variables such as $HOME will not be passed.

CMD [ "echo", "$HOME" ]
... run docker run ...
... output: $HOME

But use the exec to run the shell you can get the correct result.

CMD [ "sh", "-c", "echo", "$HOME" ]
... run docker run ...
... output: /root

shell pattern

The shell pattern will execute the command via /bin/sh -c "task command", which means the no.1 process is not the task process but the bash process.

CMD top
... run docker run ...
PID1 /bin/sh -c top
PID7 top

CMD

There are three ways to use CMD.

  • CMD ["executable","param1","param2"] (exec pattern)
    • CMD ["param1","param2"] (provide the entrypoint parameters)
  • CMD command param1 param2 (shell pattern) == CMD ["sh", "-c", "command param1 param2"]

The both pattern of CMD command will be overwritten by the command in the end of the docker run command.
The overwrite command will also run in the same pattern.

# Example 1
CMD echo "hello"
# docker run my_image:1.0 top
# top

# Example 2
ENTRYPOINT ["/bin/echo", "Hello,"]
CMD ["world!"]
# docker run myimage "GPT3"
# Hello, GPT3

ENTRYPOINT

There are two ways to use ENTRYPOINT.

  • ENTRYPOINT ["executable","param1","param2"] (exec pattern)
  • ENTRYPOINT command param1 param2 (shell pattern)

In exec pattern, the ENTRYPOINT command will not be overwritten by the command in the end of the docker run command. Such as docker run my_image:1.0 -c. The -c will not overwrite the ENTRYPOINT [ "top", "-b" ], but it will be added to the ENTRYPOINT command as ENTRYPOINT [ "top", "-b", "-c" ].

In shell pattern, your custom command will be ignored by the ENTRYPOINT command. Such as docker run my_image:1.0 -c. The -c will be ignored by the ENTRYPOINT top, and the command will still be top.

You can also overwrite the ENTRYPOINT command by using the --entrypoint xxx follow the docker run command.

You can choose CMD or ENTRYPOINT according to your specific needs.

  • If you want to create an image with default behavior, and allow users to override the default behavior, you can use CMD.
  • If you want to create an image that always executes a specific command, and allows users to pass parameters, you can use ENTRYPOINT.
  • At the same time, CMD and ENTRYPOINT can also be used together, in which case the parameters specified in CMD will be used as the default parameters for the command specified by ENTRYPOINT.

conclusion

  • shellmode: The main process(pid 1) is the /bin/sh process, which can resolve the environment variables.
  • execmode: The main process is the command you specify, and the environment variables will not be resolved.
  • If ENTRYPOINT uses shellmode, the default CMD instruction will be ignored.
  • If ENTRYPOINT uses execmode, the content specified in default CMD instruction will be appended as parameters for the command specified by ENTRYPOINT. If ENTRYPOINT uses execmode, the default CMD instruction should also use exec mode.

目录
相关文章
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
PAI Model Gallery 支持云上一键部署 DeepSeek-V3、DeepSeek-R1 系列模型
DeepSeek 系列模型以其卓越性能在全球范围内备受瞩目,多次评测中表现优异,性能接近甚至超越国际顶尖闭源模型(如OpenAI的GPT-4、Claude-3.5-Sonnet等)。企业用户和开发者可使用 PAI 平台一键部署 DeepSeek 系列模型,实现 DeepSeek 系列模型与现有业务的高效融合。
|
3天前
|
人工智能 搜索推荐 Docker
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
DeepSeek R1 + LobeChat + Ollama:快速本地部署模型,创建个性化 AI 助手
1849 97
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
|
1月前
|
供应链 监控 安全
对话|企业如何构建更完善的容器供应链安全防护体系
阿里云与企业共筑容器供应链安全
171370 16
|
10天前
|
Linux iOS开发 MacOS
deepseek部署的详细步骤和方法,基于Ollama获取顶级推理能力!
DeepSeek基于Ollama部署教程,助你免费获取顶级推理能力。首先访问ollama.com下载并安装适用于macOS、Linux或Windows的Ollama版本。运行Ollama后,在官网搜索“deepseek”,选择适合你电脑配置的模型大小(如1.5b、7b等)。通过终端命令(如ollama run deepseek-r1:1.5b)启动模型,等待下载完成即可开始使用。退出模型时输入/bye。详细步骤如下图所示,轻松打造你的最强大脑。
8276 86
|
1月前
|
供应链 监控 安全
对话|企业如何构建更完善的容器供应链安全防护体系
随着云计算和DevOps的兴起,容器技术和自动化在软件开发中扮演着愈发重要的角色,但也带来了新的安全挑战。阿里云针对这些挑战,组织了一场关于云上安全的深度访谈,邀请了内部专家穆寰、匡大虎和黄竹刚,深入探讨了容器安全与软件供应链安全的关系,分析了当前的安全隐患及应对策略,并介绍了阿里云提供的安全解决方案,包括容器镜像服务ACR、容器服务ACK、网格服务ASM等,旨在帮助企业构建涵盖整个软件开发生命周期的安全防护体系。通过加强基础设施安全性、技术创新以及倡导协同安全理念,阿里云致力于与客户共同建设更加安全可靠的软件供应链环境。
150306 32
|
1天前
|
人工智能 自然语言处理 JavaScript
宜搭上新,DeepSeek 插件来了!
钉钉宜搭近日上线了DeepSeek插件,无需编写复杂代码,普通用户也能轻松调用强大的AI大模型能力。安装后,平台新增「AI生成」组件,支持创意内容生成、JS代码编译、工作汇报等场景,大幅提升工作效率。快来体验这一高效智能的办公方式吧!
840 5
|
2天前
|
API 开发工具 Python
阿里云PAI部署DeepSeek及调用
本文介绍如何在阿里云PAI EAS上部署DeepSeek模型,涵盖7B模型的部署、SDK和API调用。7B模型只需一张A10显卡,部署时间约10分钟。文章详细展示了模型信息查看、在线调试及通过OpenAI SDK和Python Requests进行调用的步骤,并附有测试结果和参考文档链接。
625 5
阿里云PAI部署DeepSeek及调用
|
10天前
|
人工智能 自然语言处理 Java
Spring AI,搭建个人AI助手
本期主要是实操性内容,聊聊AI大模型,并使用Spring AI搭建属于自己的AI助手、知识库。本期所需的演示源码笔者托管在Gitee上(https://gitee.com/catoncloud/spring-ai-demo),读者朋友可自行查阅。
933 41
Spring AI,搭建个人AI助手
|
3天前
|
机器学习/深度学习 人工智能 并行计算
一文了解火爆的DeepSeek R1 | AIGC
DeepSeek R1是由DeepSeek公司推出的一款基于强化学习的开源推理模型,无需依赖监督微调或人工标注数据。它在数学、代码和自然语言推理任务上表现出色,具备低成本、高效率和多语言支持等优势,广泛应用于教育辅导、金融分析等领域。DeepSeek R1通过长链推理、多语言支持和高效部署等功能,显著提升了复杂任务的推理准确性,并且其创新的群体相对策略优化(GRPO)算法进一步提高了训练效率和稳定性。此外,DeepSeek R1的成本低至OpenAI同类产品的3%左右,为用户提供了更高的性价比。
766 10
|
2月前
|
弹性计算 人工智能 安全
对话 | ECS如何构筑企业上云的第一道安全防线
随着中小企业加速上云,数据泄露、网络攻击等安全威胁日益严重。阿里云推出深度访谈栏目,汇聚产品技术专家,探讨云上安全问题及应对策略。首期节目聚焦ECS安全性,提出三道防线:数据安全、网络安全和身份认证与权限管理,确保用户在云端的数据主权和业务稳定。此外,阿里云还推出了“ECS 99套餐”,以高性价比提供全面的安全保障,帮助中小企业安全上云。
201994 15
对话 | ECS如何构筑企业上云的第一道安全防线