The reason why Docker is so popular nowadays is mainly because of its lightweight, rapid deployment and resource utilization. However, the quality of a Docker image mainly depends on the quality of the Dockerfile. The same function image, but different Dockerfile builds the image size is different, this is because Docker is accumulated layer by layer read-only layer, and each layer is each instruction in the Dockerfile, so the size of the Docker image depends entirely on the size of the intermediate layer generated by each instruction in the Dockerfile. Let's take a small example to explain the formation of dockerimage in detail. We have a Dockerfile: FROM Ubuntu:14.04 ADD run.sh / VOLUME /data CMD [“./run.sh”] The main things this simple Dockerfile does are: based on the Ubuntu:14.04 system, put run.sh in the root directory, set the volume mount point, and then run the script run.sh when the image starts. The following figure shows the resulting docker image: Obviously, from the figure we can see that the four instructions form four layers respectively. Assuming that Ubuntu:14.04 is 150MB and run.sh is 1MB, then the size of the FROM Ubuntu:14.04 layer is 150MB, the size of the ADD run.sh / layer is 1MB, and the size of the VOLUME /data layer and CMD ["./run.sh"] is 0 because no files or other data are added and no data is generated in the system. So the size of the entire image is 151MB. After knowing the principle of docker image generation, let's talk about the optimization and compression of docker images. One thing that needs to be explained is that the number of layers does not determine the size of the image in some cases. Only when the Dockerfile appears:
The image can be compressed and optimized when RUN yum uninstall *** is used. Because the above two sentences install a tool and then uninstall it. Under normal circumstances, we feel that the size of the installation and uninstallation is 0, but this is not the case in the docker image. RUN yum uninstall *** can only make the previous layer invisible, but the size of the previous layer will not change. Therefore, if we want to achieve the effect of 0, we need to compress these two layers into one layer, which is written like this:
This will achieve the effect of compressing the image. Therefore, there are two main points for compressing images: 1. Select a smaller original image, that is, the image after FROM should be as small as possible. 2. Merge the layers in Dockerfile according to the actual situation. The specific situation is as mentioned above. It should be noted that the effect will not be achieved by merging layers randomly. Additional knowledge: How to build anaconda+jupyter into a docker image Recently, due to business needs, we need to build a jupyter image to provide services. Because docker is lightweight, it can be easily migrated. Here is a brief introduction to what I did and the pitfalls I encountered: First, let's install anaconda. There are python2 and 3 versions. The versions are different but the build process is the same. There are two ways. The first one is that you can build the image through Dockerfile, but there is no interaction when running the Anaconda2-5.0.1-Linux-x86_64.sh script, so I used the docker commit method to execute it. But it turns out that you can also build it through Dockerfile. You only need to run the Anaconda2-5.0.1-Linux-x86_64.sh script on your local computer first, and ADD the generated folder, which is anaconda2, to the corresponding location in the image, and modify the environment variables to add PATH. Let's take python2 as an example: 1. Download and run the script Anaconda2-5.0.1-Linux-x86_64.sh from the anaconda official website. When downloading, pay attention to whether your system is 32-bit or 64-bit. 2. scp the script to the base image and install the decompression command bzip2
3. Run the script (enter yes all the way)
4. Update anaconda
5. Install Jupyter
6. Create a login password root@localhost ~]# ipython Python 3.5.2 (default, Aug 4 2017, 02:13:48) Type 'copyright', 'credits' or 'license' for more information IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: from notebook.auth import passwd In [2]: passwd() Enter password: Verify password: Out[2]: 'sha1:5311cd8b9da9:70dd3321fccb5b5d77e66080a5d3d943ab9752b4' 7. Generate configuration files
Note: You may encounter encoding errors at this step: UnicodeEncodeError:'ascii'codec can't encode characters in position... The solution is: in the anaconda2 folder, in lib>python2.7>site.py, change: if 0: # Enable to support locale aware default string encodings. import locale loc = locale.getdefaultlocale() if loc[1]: encoding = loc[1] #Change the 0 after if in the above code segment to 1, save, and restart anaconda. 8. Modify the configuration file:
Add the following content: c.NotebookApp.ip='*' c.NotebookApp.password = u'sha1:5311cd8b9da9:70dd3321fccb5b5d77e66080a5d3d943ab9752b4' #Note that the key here is the one just generated c.NotebookApp.open_browser = False c.NotebookApp.port =8888 #Specify a port at random, you can also use the default 8888 9. Save the image
10. Start images to provide services:
Note: There is a big pitfall in centos7. When you turn off the firewall, systemctl cannot be used and an error message is given: Failed to get D-Bus connection: Operation not permitted So you have to use init to start, and in the Dockerfile you can use CMD to start the runtime. 11. Enter the docker image
12. Turn off the firewall
13. Start Jupyter
14. Enter the server IP + mapped port number in the browser to access it. Done~ The above Docker image compression and optimization operation is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Realizing tree-shaped secondary tables based on angular
>>: Key points for writing content of HTML web page META tags
1. Environmental Preparation The IP address of ea...
How to write join If you use left join, is the ta...
Table of contents CentOS rpm installation and con...
Table of contents 1. What is Promise 2. Basic usa...
Preface: When we are making web pages, we often n...
This article shares the specific code of Vue2.0 t...
Table of contents 1. Basic Concepts 2. Developmen...
1. What is the cardinality? Cardinality refers to...
Table of contents 1. Technical Overview 2. Techni...
DCL (Data Control Language): Data control languag...
1. Dashed box when cancel button is pressed <br...
Table of contents 1. MySQL 8.0.18 installation 2....
The equal-width layout described in this article ...
In the MySQL database, null is a common situation...
The picture is used as the background and the lin...