Dockerfile instructions and actual combat
1. Introduction to Dockerfile
Dockerfile is a file used to build a docker image and is a command script.
Build steps:
- Write a dockerfile file
- docker build builds into a mirror
- docker run run the image
- Docker push release image (DockerHub, Alibaba Cloud image warehouse)
We can check how the
official image is written. The official image is a basic package, and many functions are not available. We usually build our own image.
2. Dockerfile build process
Basic knowledge:
- Each reserved keyword (command) must be in uppercase letters
- Execution is executed sequentially from top to bottom
- # Indicates a comment
- Every instruction will create a new mirror and submit
- Dockerfile is development-oriented. If we want to publish projects and mirror images in the future, we need to write a dockerfile file, which is relatively simple.
- Docker image has gradually become the standard for enterprise delivery, and it must be mastered
- Steps: development, deployment, operation and maintenance, all indispensable
- DockerFile: build file, define a step, source code
- Dock and images: The image generated by DockerFile is the last product to be released and run
- Docker container: The container is the server provided by the image running
3. DockerFile instruction
FROM # 基础镜像,一切从这里开始构建
MAINTAINER # 镜像的作者 邮箱+姓名
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:tomcat镜像,这个tomcat压缩包,添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOST # 保留端口配置
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD 的指令,触发指令。
COPY # 类似ADD ,将我们的文件拷贝到镜像中
ENV # 构建的时候设置环境变量
Illustration:
4. Actual test, build your own centos
The official centos is a streamlined version, lacking good commands, such as vim, ifconfig, etc. Therefore, we create a centos with vim and ifconfig.
1. Create a dockerfile file, the file name is mydockerfile-centos
FROM centos
MAINTAINER ybg<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
2. Build your own centos image
command:
docker build -f mydockerfile-centos -t ybgcentos:0.1 .
Screenshot:
3. Test run
command:
docker run -it ybgcentos:0.1 /bin/bash
Screenshot: As
you can see, vim and ifconfig tests are available, and the build of your own image is successful.