Docker Learning Note 4.1-building images with dockerfile files

Source: Internet
Author: User
Tags port number docker ps docker run
dockerfile File


Dockerfile uses a DSL -based directive to build a Docker image, and then uses the Docker build command to build a new image based on the Dockerfile directive.



About the DSL language, Bo Master carefully consulted, interested can refer to Guo Xiaogang teacher translated "field dedicated language combat" http://book.51cto.com/art/201310/412336.htm

Dockerfile instructions


Create a Dockerfile file


Dockerfile volume instruction

We must create a directory to hold the Dockerfile, which is called the build context. Docker uploads all the directories and files in that context to the Docker daemon as it builds the image, making it easy for Docker to directly access all the code, files, and data you want to store in the image.



Create the following code:


#mkdir static_web
#cd static_web
#vim Dockerfile


The blogger's Dockerfile file is as follows:
Each dockerfile is composed of a series of instructions and parameters. The instruction must be in uppercase and must be followed by a parameter. The dockerfile instructions are executed from the top down order.



Dockerfile instruction details the first instruction for each dockerfile is from, which is used to specify a base image. In the Dockerfile file above, we then specified the maintainer directive, which indicated the author and author mailbox Run instructions for Docker, and in the previous example we updated the APT library and installed the Nginx service and then created the Nginx index.html. After each run instruction is executed, a new mirror layer is submitted, expose instruction, which tells the application in the container to access the specified port when run. For security, Docker does not actively open the port, and then opens the port at run time. Expose instructions can also be linked to multiple containers, followed by re-introduction. The dockerfile execution process runs a container from the underlying image and executes an instruction to commit a new mirror layer to build a new container based on the mirror layer just now to execute the next instruction until all instructions have been executed



From here you can see the concept of "copy-on-write" that we use in Docker. The advantage is lightweight, on the other hand, even if one of the instructions in the middle of the error, we can easily be based on the previous well-run image for debugging, and debugging is not necessary to run the previous command has been run. building a new image based on Dockerfile



Build the image code as follows:


Docker build-t= "Zhangyang/static_web".


The-t option specifies the warehouse name and the mirror name for creating a new image. For example, my warehouse name is my name. Zhangyang Mirror Name is Static_web
The last. Tell Docker to look for the Dockerfile file from the current directory, or you can specify a git address.



The execution process is as follows:



As you can see, the ID of the image is eventually returned b92c35890d8a
And after each step is completed, an ID is returned, that is, Docker submits the execution results for each step. dockerfile command failed to run



Bloggers here put the nginx of my dockerfile file as Ngnx, then build. The error is as follows:

As we can see, the error is in Step4, and a new mirror layer is submitted after the STEP3 is finished e8ec593f1475
We can use the command


Docker Run-i-T E8ec593f1475/bin/bash


Enter the container for debugging, resolve the problem, and then exit the container to modify the corresponding error location of the Dockerfile file. Dockerfile and Build cache



Docker treats the mirrored layer submitted after each step as a cache.



However, there are times when we have to make sure that the previous cache is overwritten. For example, if you have already cached the previous third step, that is, apt-get update, but we must ensure that the software that is installed next is the latest version, you must ignore the caching feature. You can use the –no-cache flag of the Docker build.


Docker build--no-cache-t= "Zhangyang/static_web".
building a cache-based dockerfile template


We can use the ENV directive in the Dockerfile file to set an environment variable, which is used to indicate the last update time of the template.
Examples of dockerfile template files are as follows:


From Ubuntu14.04
maintainer Zhangyang "867037982@qq.com"
ENV refreshed_at 2014-07-01
RUN apt-get-qq Update


The blogger created an environment variable refreshed_at in the above file.
That is, each time a build is refreshed, the cache can be reset by modifying the date value in the env instruction, and the package cache will be updated with the update value. View New Image






Docker History Zhangyang/static_web


This command will show the mirroring build process

This command details each layer of the mirroring build and the port settings when its Docker instructions start the container from the new mirror



The-p flag specifies which ports the container opens at runtime to host hosts
The-p (UPPERCASE) flag opens all ports specified in the Dockerfile file to expose



To start a container from a new image:


Docker run-d-P--name static_web_2 zhangyang/static_web/nginx-g "daemon off;"


The above command runs a new container called static_web_2, and option-D tells Docker to run in the background in a separate (detached) manner, which is well suited for long-running processes like nginx. Finally we also specify the command to be run in the container: nginx-g "daemon off;". This will run Nginx as our server in the foreground.



The-p option opens the 80 port to the host, and Docker can have two modes to open the port: a 49153-65535 port on the host is then mapped to port 80 of the container to specify a specific port number to map to port 80



We can use the following command to view the port allocation situation:


Docker Ps-l


You can also use the Docker port ID port number to view the specific port number on the mapped host.



The effect is shown in the following figure:


Docker run-d-P 127.0.0.1:80:80--name static_web_2 zhangyang/static_web \nginx-g "daemon off;"


You can also directly specify a port on the host to map to the container. But obviously this can run a docker that's not going to give us the full advantage of Docker.
If you do not want to specify the host IP address, you can write this:


Docker run-d-P 127.0.0.1::80--name static_web_2 zhangyang/static_web \nginx-g "daemon off;"


The command randomly assigns a port on the 127.0.0.1 map to port 80 on the container.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.