Analysis of Docker container technology principle (custom create image)

scratch mirror

The scratch image is a virtual image, because it does not exist at all, but it does all the basics tend to be the original template image.
  • We can check how the Dockerfile of Base Image is written from the official website
  • Click on the docker official website
  • Search for the linux version you want to use (here in centos)
  • Select the version number of the query 7.4.1708
  • The webpage is as shown below
    Insert picture description here
The yum source uses Alibaba Cloud
 [root@es2 ~]# yum -y install docker
[root@es2 ~]# systemctl start docker
 [root@es2 ~]# docker version

Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-108.git4ef4b30.el7.centos.x86_64
Go version: go1.10.3
Git commit: 4ef4b30/1.13.1
Built: Tue Jan 21 17:16:25 2020
OS/Arch: linux/amd64

Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-108.git4ef4b30.el7.centos.x86_64
Go version: go1.10.3
Git commit: 4ef4b30/1.13.1
Built: Tue Jan 21 17:16:25 2020
OS/Arch: linux/amd64
Experimental: false

 [root@es2 ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

View mirrored scratch
[root@es2 ~]# docker search scratch

INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/scratch an explicitly empty image, especially for … 621 [OK]
docker.io docker.io/scratchy/nativescript-cli This repo provides nativescript CLI includ… 6 [OK]
docker.io docker.io/astefanutti/scratch-node Scratch Node.js Docker Images 4
docker.io docker.io/grammarly/scratch Empty image used for data volumes and othe… 2
docker.io docker.io/darkmagus/scratch3 Dockerized version of Scratch 3 editor fro… 1
docker.io docker.io/kihamo/scratch-ca-certs Scratch image with certs 1 [OK]
docker.io docker.io/microbox/scratch 1
docker.io docker.io/aosqe/scratch 0
docker.io docker.io/area51/scratch-base A minimal docker image containing the esse… 0
docker.io docker.io/connecthq/scratch-ssl A scratch image that has only one file in … 0
docker.io docker.io/crashvb/scratch This docker image is an abstraction of tia… 0 [OK]
docker.io docker.io/daixijun1990/scratch scratch base image 0 [OK]
docker.io docker.io/dtschan42/scratch2-singularity Scratch 2 cont… 0 [OK]
docker.io docker.io/ghmlee/scratch 0 [OK]
docker.io docker.io/gridx/scratch 0
docker.io docker.io/kelog/scratch scratch 0
docker.io docker.io/manifoldco/scratch-certificates A Docker scratch image with ca-certificate… 0
docker.io docker.io/mcloone/scratch-with-ca 0
docker.io docker.io/mikenavarro/scratch-go 0
docker.io docker.io/omniprojects/scratch 0
docker.io docker.io/onedr0p/scratch 0
docker.io docker.io/pvtmert/scratch from scratch 0 [OK]
docker.io docker.io/qubell/scratch 0 [OK]
docker.io docker.io/resin/scratch A materialised FROM scratch image 0
docker.io docker.io/sublimino/scratch 0

Downloading the mirrored scratch will report an error
[root@es2 ~]# docker pull scratch

Using default tag: latest
Error response from daemon: ‘scratch’ is a reserved name

Make scratch mirror
  • Since the base image is created based on scratch, a file centos-7-docker.tar.xz is added
  • To implement the basic image, scratch and centos-7-docker.tar.xz are required
  • scratch is an empty image, which can be obtained by the following methods (cannot be downloaded)
Method 1: Use /dev/null to create an empty file, the command is as follows
  • Create tar file
]# tar cf scratch.tar --files-from /dev/null
]# ls

scratch.tar
uses docker import to import this empty file to the mirror, the command is as follows

]# docker import scratch.tar scratch
Method 2: One command to get it ("-"is the standard output after the pipe)
]# tar -cv --files-from /dev/null  | docker import - scratch

sha256: 19fade5b1bd2a08546a120091194ac28425e02c2a494a7533bc7b1264b8c5180

View the image file (the size is 0B, the image is a virtual image)
[root@es1 ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
scratch latest 19fade5b1bd2 5 seconds ago 0 B

Make centos-7-docker.tar.xz
  • centos-7-docker is actually all the files in the root directory of the system
  • Since the system is not installed, we do not have any commands to use, so we can only use the most basic way of adding files to create this source.
  • The yum software is installed to the specified root directory-insallroot. The most basic software packages required for the system that can log in and operate are:
command effect Installation package
yum Install the software to use yum
bash User login operating environment bash
ls / pwd Common basic system commands coreutils
1. Create a folder, install the software
[root@es2 ~]# mkdir vroot
[root@es2 ~]# yum -y install --installroot=/root/vroot  bash yum coreutils
[root@es2 ~]# cd  vroot/
[root@es2 vroot]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

or

[root@es2 ~]# chroot /root/vroot
bash-4.2# 
bash-4.2# ls -a /etc/skel/      //查看解释器
.  ..  .bash_logout  .bash_profile  .bashrc
bash-4.2# cp -a /etc/skel/.[!.]*   /root/
bash-4.2# ls -la /root

Total usage 12
dr-xr-x— 2 root root 62 February 17 19:24.
Dr-xr-xr-x 17 root root 224 February 17 18:41…
-rw-r--r-- 1 root root 18 April 11 2018 .bash_logout
-rw-r--r-- 1 root root 193 April 11 2018 .bash_profile
-rw-r--r-- 1 root root 231 April 11 2018 .bashrc

bash-4.2#  exit
[root@es2 ~]# chroot /root/vroot
[root@es2 /]# 
Configure YUM source
[root@es2 /]# cd /etc/yum.repos.d/
[root@es2 yum.repos.d]# ls

CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo
CentOS-Debuginfo.repo CentOS-Sources.repo

[root@es2 yum.repos.d]# rm -rf *
[root@es2 yum.repos.d]# exit
exit
[root@es2 ~]# ls /etc/yum.repos.d/
elasticsearch.repo  local.repo          
[root@es2 ~]# cp /etc/yum.repos.d/local.repo  /root/vroot/etc/yum.repos.d/
Packaging (to be packaged with the virtual directory as the root)
[root@es2 ~]# cd /root
[root@es2 ~]# ls
scratch.tar  vroot
[root@es2 ~]# tar -acf centos-7-docker.tar.xz -C vroot ./
[root@es2 ~]# ls
centos-7-docker.tar.xz  scratch.tar  vroot
[root@es2 ~]# tar -atvf centos-7-docker.tar.xz | head

dr-xr-xr-x root/root 0 2020-02-17 18:41 ./
drwxr-xr-x root/root 0 2020-02-17 18:41 ./var/
drwxr-xr-x root/root 0 2020-02-17 18:41 ./var/lib/
drwxr-xr-x root/root 0 2020-02-17 18:43 ./var/lib/rpm/
-rw-r–r-- root/root 0 2020-02-17 18:39 ./var/lib/rpm/.dbenv.lock
-rw-r–r-- root/root 5226496 2020-02-17 18:42 ./var/lib/rpm/Packages
-rw-r–r-- root/root 8192 2020-02-17 18:42 ./var/lib/rpm/Name
-rw-r–r-- root/root 512000 2020-02-17 18:42 ./var/lib/rpm/Basenames
-rw-r–r-- root/root 8192 2020-02-17 18:42 ./var/lib/rpm/Group
-rw-r–r-- root/root 49152 2020-02-17 18:42 ./var/lib/rpm/Requirename

[root@es2 ~]# mkdir aabb
[root@es2 ~]# mv centos-7-docker.tar.xz aabb/
[root@es2 ~]# ls
aabb  scratch.tar  vroot
[root@es2 ~]# cd aabb/
[root@es2 aabb]# ls

centos-7-docker.tar.xz

Find the initial Docker file on the official website
[root@es2 aabb]# vim Dockerfile  
FROM scratch
ADD centos-7-docker.tar.xz /

LABEL name="CentOS Base Image" \
    vendor="CentOS" \
    license="GPLv2" \
    build-date="20170911"

CMD ["/bin/bash"]
Create a custom image
[root@es2 aabb]# docker build -t centos:latest .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@es2 aabb]# systemctl start docker
[root@es2 aabb]# docker build -t centos:latest .

Sending build context to Docker daemon 51.91 MB
Step 1/4 : FROM scratch
—>
Step 2/4 : ADD centos-7-docker.tar.xz /
—> Using cache
—> d60c3b58ba23
Step 3/4 : LABEL name “CentOS Base Image” vendor “CentOS” license “GPLv2” build-date “20170911”
—> Using cache
—> 013e6a237897
Step 4/4 : CMD /bin/bash
—> Using cache
—> 180a71e4838f
Successfully built 180a71e4838f

View mirror
[root@es2 aabb]# docker history centos

IMAGE CREATED CREATED BY SIZE COMMENT
180a71e4838f 48 minutes ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
013e6a237897 48 minutes ago /bin/sh -c #(nop) LABEL name=CentOS Base … 0 B
d60c3b58ba23 48 minutes ago /bin/sh -c #(nop) ADD file:3383b6d576d74ca… 306 MB

Run custom made centos image
[root@es2 aabb]# docker run -it centos
[root@152eca359652 /]# 
[root@152eca359652 /]# ifconfig
bash: ifconfig: command not found
[root@152eca359652 /]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
local_repo                                               | 3.6 kB     00:00     
(1/2): local_repo/group_gz                                 | 166 kB   00:00     
(2/2): local_repo/primary_db                               | 5.9 MB   00:00     
repo id                           repo name                               status
local_repo                        CentOS-7 - Base                         9911
repolist: 9911
[root@152eca359652 /]# yum -y install net-tools
[root@152eca359652 /]# yum -y install psmisc
[root@152eca359652 /]# pstree -p
bash(1)---pstree(148)

[root@152eca359652 /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
Save the image file
[root@152eca359652 /]# exit
exit
[root@es2 aabb]# ls
centos-7-docker.tar.xz  Dockerfile
[root@es2 aabb]# docker save centos -o centos.tar
[root@es2 aabb]# docker images

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104357957