Docker Learning Notes (iii)-Dockerfile create a local mirror

Source: Internet
Author: User
Tags gpg local time mkdir unpack
Dockerfile Create a local mirror

This blog post will describe how to create a local mirror in Docker. The content includes writing dockerfile files, creating mirrors from the written dockerfile, mirroring the created process, and running the mirrors created.

Dockerfile is used to create custom image, containing instructions, each instruction corresponds to a command under Linux, and the Docker program translates these instructions into real Linux commands. With Dockerfile, when we need to customize to make other changes, directly modify the Dockerfile, rebuild the mirror file, save the trouble of knocking commands.

Official Dockerfile reference Dockerfile References Step1 to write dockerfile files

1. Create a new path Mydockerbuild,ubuntu using MkDir, Windows uses MD

root@mingchen-hp:~# mkdir Mydockerbuild

2. Switch to the new directory

root@mingchen-hp:~# CD mydockerbuild/

3. Create and edit new files named Dockerfile, Linux,mac use such as Nano or VI, VIM, etc. for editing, Windows use NOTEAPD.

root@mingchen-hp:~/mydockerbuild# Nano Dockerfile

4. Add the From declaration as follows:

From Docker/whalesay:latest

The FROM keyword tells Docker to create the underlying mirror image of the mirror. The underlying image here is the current version of the Whalesay Mirror, which already contains the Cossay program, which we will begin with.

5. Adding the Run declaration will add the fortunes program to the Mirror:

RUN apt-get-y update && apt-get install-y Fortunes

Whalesay mirroring is based on Ubuntu, so we use Apt-get to install the program fortunes

6. Add CMD declaration, this statement tells the mirror when the environment is set up, the final Run command. The following command runs FORTUNE-A

cmd/usr/games/fortune-a | Cowsay

7. Check the input command, dockerfile should be like the following

From Docker/whalesay:latest
RUN apt-get-y update && apt-get install-y fortunes
Cmd/usr/games/fortune- A | Cowsay

Note that for a friend who first used the Nano editor, save-Ctrl+o, Exit-ctrl+x, enter the file name you want to save, and then return to confirm that you will exit the Nano editor. With Gedit, Vim, VI and other editors can also, sudo access. STEP2 Create a mirror from a dockerfile file

When you are under the Mydockerbuild path, use the Docker build command:

$ docker build-t Docker-whale.

Embarrassed: Originally thought will pass the build smoothly, the result encountered the bug, has had each kind of attempt, has browsed the most can Google to the question related post, but just does not understand that most other people can solve, but I cannot solve, certainly also saw someone reply cannot solve, A user of GitHub spits thinking "heartbroken, I ' ve been fighting with this porblem off and on for weeks." “

If it goes well in this step, then congratulations.
If you encounter a problem in this step, the error, such as me, also very normal. I ran into a problem, and I've been doing it for about a week, and I've had two major problems. After solving the first problem, we found that there was a second problem. First, the above dockerfile, the command execution is actually entered into the container, can be seen in the output of the terminal, a series of lowercase letters and numbers is the ID of the container,---> Running in 21aac36e6265 so the actual problem is that the container can not connect to the outside of the net, ping outside the network, I put this issue has been published to the Docker Community forums, look forward to the guidance of the Great God, there is a user reply, and did not solve, and then I resolved, The solution has just been released to the bottom of the question, my link is, communication between containers and wider world, for English is not very OK students, I will be in the next blog to write this issue alone. The second command above has apt-get install, which requires the container to be able to connect to the extranet, so this will be problematic; the second, the error hash sum mismatch, the solution to this problem is more, because the cause of this error is also many, such as apt source settings, apt Cache removal, apt version of the different causes of bugs, Proxy server proxy settings, server and local time timestamp different, etc. i will write it in the next blog. It's embarrassing that these two mistakes are very tiring. Given the question of space, I'm not going to elaborate on this blog, the subject of this article is dockerfile create a local mirror

Normally, the output of the terminal should be as follows:

mingchen@mingchen-hp:~/mydockerbuild$ sudo docker build-t Docler-whale. Sending build context to Docker daemon 2.048 MB step 1:from docker/whalesay:latest---> 6b362a9f73eb step 2:env DE Bian_frontend noninteractive---> Running in f251aa390321---> Daa3f0eb1eff removing Intermediate container F251AA  390321 step 3:run sed-i "s|http://archive.ubuntu.com|http://mirrors.163.com|g"/etc/apt/sources.list && RM-RF 
/var/lib/apt/lists/* && apt-get-y update && apt-get install-y Fortunes---> Running in 84874f888414 IGN http://mirrors.163.com trusty inrelease get:1 http://mirrors.163.com trusty-updates inrelease [65.9 KB] get:2 http:// mirrors.163.com trusty-security inrelease [65.9 KB] get:3 http://mirrors.163.com trusty release.gpg [933 B] Get:4 http://m irrors.163.com trusty-updates/main Sources [MB KB] get:5 http://mirrors.163.com trusty-updates/restricted Sources [ 5921 B] get:6 http://mirrors.163.com trusty-updates/universe Sources [216 KB] GEt:7 http://mirrors.163.com trusty-updates/main amd64 Packages [1172 KB] Get:8 http://mirrors.163.com Restricted AMD64 Packages [20.4 kb] get:9 http://mirrors.163.com trusty-updates/universe amd64 Packages [507 KB] Get:10 HT tp://mirrors.163.com trusty release [58.5 KB] get:11 http://mirrors.163.com trusty-security/main Sources [157 KB] Get:12 H ttp://mirrors.163.com trusty-security/restricted Sources [4621 B] get:13 http://mirrors.163.com Universe Sources [55.9 KB] get:14 http://mirrors.163.com trusty-security/main amd64 Packages [711 KB] Get:15 Http://mirror s.163.com trusty-security/restricted AMD64 Packages [17.0 KB] get:16 http://mirrors.163.com trusty-security/universe AMD64 Packages [193 KB] get:17 http://mirrors.163.com trusty/main Sources [1335 KB] get:18 http://mirrors.163.com trusty/r estricted Sources [5335 B] get:19 http://mirrors.163.com trusty/universe Sources [7926 KB] get:20 http://mirrors.163.com t Rusty/main AMD64 Packages [1743 KB] get:21 Http://mirrors.163.com trusty/restricted AMD64 Packages [16.0 KB] get:22 http://mirrors.163.com trusty/universe-AMD64
Packages [7589 KB] fetched 22.3 MB in 1min 35s (234 kb/s) Reading Package lists ...
Reading Package Lists ...
Building Dependency Tree ...
Reading state information ... The following extra packages would be Installed:fortune-mod fortunes-min Librecode0 suggested Packages:x11-utils BSDM Ainutils the following NEW packages'll be Installed:fortune-mod fortunes fortunes-min librecode0 0 upgraded, 4 newly
installed, 0 to-remove and not upgraded.
Need to get 1961 KB of archives.
After this operation, 4817 KB of additional disk space would be used. Get:1 http://mirrors.163.com/ubuntu/trusty/main librecode0 amd64 3.6-21 [771 KB] Get:2 http://mirrors.163.com/ubuntu/ Trusty/universe fortune-mod AMD64 1:1.99.1-7 [39.5 KB] Get:3 Http://mirrors.163.com/ubuntu/trusty/universe Fortunes-min all 1:1.99.1-7 [61.8 KB] Get:4 Http://mirrors.163.com/ubuntu/trusty/universe Fortunes All 1:1.99.1-7 [1089 KB] fetched 1961 KB in 13s (148 kb/s) Selecting previously unselected package librecode0:amd64. (Reading database ...)
13116 files and directories currently installed.)
Preparing to unpack .../librecode0_3.6-21_amd64.deb ...
Unpacking librecode0:amd64 (3.6-21) ...
Selecting previously unselected package fortune-mod.
Preparing to unpack .../fortune-mod_1%3a1.99.1-7_amd64.deb ...
Unpacking Fortune-mod (1:1.99.1-7) ...
Selecting previously unselected package fortunes-min ... preparing to unpack ...
Unpacking fortunes-min (1:1.99.1-7) ...
Selecting previously unselected package fortunes.
Preparing to unpack .../fortunes_1%3a1.99.1-7_all.deb ...
Unpacking fortunes (1:1.99.1-7) ...
Setting up Librecode0:amd64 (3.6-21) ...
Setting up Fortune-mod (1:1.99.1-7) ...
Setting up Fortunes-min (1:1.99.1-7) ...
Setting up Fortunes (1:1.99.1-7) ... Processing triggers for Libc-bin (2.19-0ubuntu6.6) ...---> 746e8b0e3ebb removing intermediate conTainer 84874f888414 Step 4:cmd/usr/games/fortune-a | Cowsay---> Running in bcf0fb436960---> 4506ff33e5ba removing intermediate container bcf0fb436960 bu Ilt 4506FF33E5BA

See the last sentence successfully built, should feel the world is still beautiful. Official website of the document, although the basic will not God horse problem, but really just reference. -_-|| STEP3 interprets the build process of mirrors Docker the code that is necessary to make sure that it is built, that is, the most basic one that can prop up a building process. The terminal will output the following
Sending build context to Docker daemon 2.048 KB Docker check that the underlying mirror we are using is local, if any, use it directly, and if not, pulls it from Docker
Hub The mirrors used here are already downloaded from the previous article.
Step 1:from docker/whalesay:latest--->
6b362a9f73eb
This string of characters is the ID of this layer of the current container, and each time the command is executed, the IDs of the different layers are not the same. Analogy of the lasagna, we understand that the base image of the container is after each step of the modification is a layer of overlay, different layers of different IDs.

Next, set the container's running environment and execute the Run command:

Step 2:env debian_frontend noninteractive---> Running in
f251aa390321---> Daa3f0eb1eff removing Intermediate container
f251aa390321 Step 3:run Sed-i
"S|http://archive.ubuntu.com|http://mirrors.163.com|g"
/etc/apt/sources.list && rm-rf/var/lib/apt/lists/* && apt-get-y
Update && apt-get install-y Fortunes---> Running in 84874f888414
IGN http://mirrors.163.com Trusty Inrelease get:1
http://mirrors.163.com trusty-updates inrelease [65.9 KB] Get:2
http://mirrors.163.com trusty-security inrelease [65.9 KB] Get:3
http://mirrors.163.com trusty RELEASE.GPG [933 B]
...... (The middle is too long, omitted.) See the full STEP2 above)
Setting up
Fortunes-min (1:1.99.1-7) ... Setting up Fortunes (1:1.99.1-7) ...
Processing triggers for Libc-bin (2.19-0ubuntu6.6) ...--->
746e8b0e3ebb Removing intermediate container 84874f888414

Finally, a new container (mirrored running instance) created by Dockerfile is generated:

bcf0fb436960  ---> 4506ff33e5ba removing Intermediate container bcf0fb436960 successfully built
4506ff33e5ba
Step4 Run the newly generated dockerfileView all mirrors through the sudo docker images:

Run the mirrored docker-whale that you just generated:

Bravo! If such a Docker logo and text are displayed, then the local mirror created by Dockerfile is successful. Running multiple run commands will get the same copy and different text. As follows:

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.