Use of Dockerfile
Introduction to Dockerfile
Dockerfile is a text file that contains instructions (Instruction), and each instruction builds a layer, so the content of each instruction describes how the layer should be constructed.
Basic knowledge
- Each command is an uppercase reserved keyword
- The execution sequence is executed from top to bottom. Each command is a layer
- # Indicates a comment
Use Dockerfile common instructions
command | effect | Example |
---|---|---|
FROM | The basic mirror tells who the mirror's mother is | FROM ubuntu |
MAINTAINER | Who wrote the name + email address of the mirror | |
RUN | What do you want the mirror to do | |
COPY | Copy files to the container | |
ADD | If the source path is http path ADD command can goAutomatic download Then copy to the specified path, if the copied file is tar.gz ADD willAutomatic decompression And delete the compressed package | |
WORKDIR | The working directory of the container is cd | |
VOLUME | Where to store data | |
EXPOSE | Expose the mirrored port | |
CMD | The command specified when the container is started can only take effect at the end and will be replaced | |
ENTRYPOINT | The command specified when the container is started, the command can be appended and it will be appended | |
ONBUILD | But build an inherited Dockerfile | |
ENV | Set environment variables when building the image |
command | effect | Example |
---|---|---|
FROM imageName | Which image must start the customized Dockerfile based on | FROM tomcat 或者 FROM tomcat:8.5.32 |
WORKDIR The directory to switch | Switching the workspace can replace the cd command | WORKDIR /usr/local/tomcat can switch the current directory to the specified directory |
RUN bash command | Execute a bash command | RUN echo "Hello World"> /usr/local/1.txt etc. |
COPY file/container path of the context path | Copy files to the container | COPY index.js /usr/local/docker/test |
ADD the file of the context path. Container path | Copy files to the container | If the source path is http path, the ADD command can be downloaded and copied to the specified path. If the copied file is tar.gz ADD will automatically decompress and delete |
CMD command | Start the program in the mirror | CMD [“nginx”, “-g”, “daemon off;”] |
EXPOSE port [port 2, port 3,...] | Expose the port mirrored in the Docker container | EXPOSE [80,8080] or EXPOSE 80 |
. . . and many more |
Build image
docker build -f dockerfile名称 -t 镜像名称 .
# -f 指定dockerfile文件名
# -t 构建的镜像名称
# . 上下文路径
Precautions
The difference between CMD and ENTRYPOINT
RUN command executes multiple commands
#正确用法
RUN buildDeps='gcc libc6-dev make' \
&& apt-get update \
&& apt-get install -y $buildDeps \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-3.2.5.tar.gz" \
&& mkdir -p /usr/src/redis \
&& tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \
&& make -C /usr/src/redis \
&& make -C /usr/src/redis install \
&& rm -rf /var/lib/apt/lists/*
#错误用法
FROM debian:jessie
RUN apt-get update
RUN apt-get install -y gcc libc6-dev make
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-3.2.5.tar.gz"
RUN mkdir -p /usr/src/redis
RUN tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1
RUN make -C /usr/src/redis
RUN make -C /usr/src/redis install
Each instruction in the Dockerfile represents a layer, the same is true for RUN, and the above writing method creates a 7-layer image. This is completely meaningless, not only will increase the build time, but also increase the chance of error!
Note: Union FS has a maximum number of layers. For example, AUFS used to have a maximum of 42 layers, but now it cannot exceed 127 layers.
The correct way of writing is to concatenate the Dockerfile instructions with the \ symbol to wrap and use &&, so that the previous seven layers are converted into one layer.
Attached, the contextual explanation of the Docker custom image
If you pay attention, you will see docker build
the last command a .
. .
It represents the current directory, but Dockerfile
in the current directory, so many beginners think that this is the path specified in Dockerfile
the path, so understanding is actually inaccurate. If the command corresponding to the above format, you may find that this is in the specified context path . So what is context?
- Like this command
docker build -t imageName .
When building a Dockerfile, the user will specify the path to build the image context. After the docker build - t name .
command knows this path, all content under the path specified by the user will be packaged and uploaded to the Docker engine. In this way, after the Docker engine receives the context package, it will get all the files needed to build the image.
If Dockerfile
so write in:
COPY ./package.json /app/
This is not to copy execution docker build
directory where Dockerfile of command package.json
, nor is it to copy Dockerfile
the directory under package.json
, but replication context (context) under the directory package.json
.
Publish your own mirror
Publish to DockerHub
register account
# 先登录自己的账户
docker login -u yufire
# 输入密码
Password:
# 提示你没有把将未加密地存储在/root/.docker/config.json中。
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
# 登陆成功
Login Succeeded
docker push 镜像名:版本号
Publish to Alibaba Cloud Image Service
- Log in to Alibaba Cloud
- Find the container image service
- Create a namespace to prevent conflicts Only 3 accounts can be created
- Create warehouse
Try to bring the version number of the mirror you created
docker login -u username
docker logout
End to spread flowers
Author: yufire © [email protected]