Dcokerfile involves directives that are most commonly used in Dockerfile. The FROM directive, which specifies the base image, is required in a Dockerfile and must be the first directive. For example, if I want to build an application based on an ubuntu 14.04 image, the first line I’ll write is FROM Ubuntu :14.04 ADD to ADD a local file to the image, For example, if I wanted to ADD a Python requirements. TXT to initialize the environment, I would write the ADD requirements. TXT /home/root COPY command. The format and nature of the ADD instruction and COPY are basically the same. However, some functionality has been added to COPY. For example, if ADD is a URL path, ADD will automatically download, etc. At present, these advanced functions of ADD are obscure and not recommended. COPY app.py to /usr/local/bin. COPY app.py to /usr/local/bin/run. If I want to install PYTHon3 from yum, I will write: beware of executing interactive commands. Note the yum -y parameter RUN yum install -y python3 CMD command. This command is the entry configuration to start the service when running the container, for example, I think based on supervisord start the container, I will be written the following CMD [“/usr/local/bin/supervisord “, “- c”, “/ path/to/app/supervisord. Conf”] ENTRYPOINT instructions, Start the container process, which is often used in conjunction with CMD. For example, when executing commands under Linux, the default is to use the shell’s parent process to execute the current process. The ENTRYPOINT [“/bin/bash”] WORKDIR command specifies the default working directory for your code, which is the default path when you enter the container. The default path to log in to the system is /home/username. For example, if I want to set the environment variable ENV_APP_PORT, I will read the environment variable when I start my process. ENV ENV_APP_PORT 8899 LABEL directive to LABEL the container that I’m starting. For example, the container that I’m starting is a user group, so I can LABEL it like this, Example: LABEL group=”zhangyue.user” LABEL version=”1.0″ the above is the most commonly used in Dockerfile some instructions, in the development process of the basic involved in most can use the above instructions to complete the work. Just like.gitignore, if you don’t want some files to be packaged into the image, you can set it in the root directory. Dockerignore
git
.git .gitignore
python
.pyc pycache/ /pycache/ //pycache/ ///pycache/ .py[cod] /.py[cod] //.py[cod] ///*.py[cod]
project
script test tpl readme.txt requirements *.log
Docker
docker-compose.yml .docker Dockerfile
Vim swap files
.swp /.swp //.swp ///.swp
Python mode for VIM
.ropeproject /.ropeproject //.ropeproject ///.ropeproject
PyCharm
.idea
Virtual environment
.env
Installer logs
pip-log.txt pip-delete-this-directory.txt
CI
.codeclimate.yml. Travis. Yml. Dockerignore is used to prevent files that are not relevant to the application from being loaded into the image, since any extra files will make the image larger. Therefore, we should be good at using the.dockerignore file to mask unnecessary temporary files. When building docker images using build cache, you need to understand that there is a concept of layering, what is layering, how to determine how many layers my mirror image has? I’ll tell you a simple way to do it, how many instructions you use in your Dockerfile is roughly how many layers you add, and why do you add layers, because the image that you import FROM has its own layers, So the total number of layers is the number of layers imported FROM the image plus the number of new layers is the total number of layers of the image. Reducing the layering of the image helps reduce the size of the image build, and reducing the size of the image helps with image distribution and speeds up startup. So we want to minimize mirror layering. I’m going to share some tips for reducing the layering of images. In docker1.10 and later, RUN,COPY, and ADD create new layers. The other commands only create intermediate temporary images and do not directly affect the final image size. Merge multiple lines of arguments. For example, when installing a package, don’t write a RUN for each package, RUN apt-get update && apt-get install -y BZR CVS Git Mercurial Subversion merge Incorporated (nasdaq: PCLN – news) com. Example. Is – beta = com. Example. Is – production = “” com. Example. Version =” 0.0.1 – beta” Com.example. relex-date =”2015-02-12″ RUN apt-get update && apt-get install -y aufs-tools automake Build -essential curl DKG -sig libcap-dev libsqlite3-dev mercurial reprepro ruby1.9.1 ruby1.9.1-dev s3cmd=1.1.* && rm -rf /var/lib/apt/lists/* Dockerfile is good at using PIPES to avoid invalid environment construction. For example, when executing a command, Docker determines whether the command is successfully executed based on the return value of the command. If the return value is 0, it is considered successful. Otherwise, it is considered a failure, and the pipe symbol can easily mask the correct return value of an instruction, causing subsequent commands to continue without interruption even if the previous command failed. RUN wget – O – some. Site | wc -l > / number how to avoid this situation? Add set-o pipefail as an example: RUN the set -o pipefail && wget – o – some. Site | wc -l default shell > / number execution environment is/bin/sh, / bin/bash, / bin/sh there is some difference, To specify shell execution commands, do the following: RUN [“/bin/bash “, “- c”, “set -o pipefail && wget – o – some. Site | wc -l > / number”] reasonable select ADD or COPY the ADD is similar to the COPY instructions, The COPY command only copies files to the image layer. The ADD command also has some additional functions, such as decompress files. ADD rootfs.tar.xz /, run the ADD example.com/big.tar.xz /usr/src/things/ download command, Using curl or wget, you can download a file in one layer and unzip the file to delete unnecessary tar files. The ADD command requires two layers to perform this logic. If the image has multiple levels, COPY can be divided into multiple copies to avoid one COPY, because the cache will be invalid if the text changes. If you COPY an unmodified file, there will be a build cache. If you COPY a large number of files, Specify user and group permissions. For security reasons, if you do not specify user and group permissions, the default user root may be used. If a container can be started by a non-root user, you are advised to specify user root, for example: RUN groupadd -r postgres && useradd –no-log-init -r -g postgres postgres –no-log-init This bug occurs when go packages tar and generates a log file to /var/log/faillog. If this parameter is not set, log files will grow. Debian/Ubuntu does not currently support this command. To avoid sudo directives, consider using goSU directives instead of adapting your company’s specifications. Every company has internal conventions, such as: How to name the image created by Dockerfile, how to name the image, how to specify the version number whether to allow the latest tag image to be pushed to the repository whether to delete the image which environment variables and tags can not be used arbitrarily in the middle layer image, for example, Internal service discovery or docker-compose will be based on container labeling whether there will be conflicts, etc., which need to be standardized in naming. Each company may have differences in the above specifications. I put forward some points for you to expand your ideas, so I will not list the scenarios one by one. Docker push, Docker pull and external tar files docker save, Docker load docker load docker load load docker load load docker load load load load load load load load load load load load load load load load load load load load load load load load load load load load load load load load load load The usage of each command run can be checked by docker < corresponding command > -h. Since there are many commands, I will not introduce its functions one by one. I will show you the help information of one command by adding my wechat