This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together

I’m sorry, but I guess the first container image in the universe was built by Solomon Hykes of DotCloud. Hats off!

As a colleague asked, how to build your own container image? One is to write a Dockerfile based on a container image. The other is based on a container image. First run a container, perform some column operations in the container, then exit the container, commit a new container image based on the container

My colleague’s curiosity impressed me, so let’s write an article about building the first basic container image. The method described in this article does not belong to the above two methods, so with the scary weird title, let’s start building the first basic container image in your own small universe

1. Define base container image (target)

  • You can run a shell: /bin/bash
  • You can do ls naming to see what files are in the root directory
  • The experimental environment is CentOS7.9
[root@master1 /]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@master1 /]# uname -r4.4.248-1. El7. Elrepo x86_64Copy the code

Create rootfs

1. Create a temporary rootfs root directory/TMP/myImage

[root@master1 /]# mkdir /tmp/myimage
[root@master1 /]# mkdir /tmp/myimage/bin
[root@master1 /]# mkdir /tmp/myimage/lib64
Copy the code

2. Determine the dynamic library on which the bash command depends

[root@master1 /]# ldd /bin/bash | egrep -o '/lib.*\.[0-9]'
/lib64/libtinfo.so.5
/lib64/libdl.so.2
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
Copy the code

3. Determine the dynamic library on which the ls command depends

[root@master1 /]# ldd /bin/ls | egrep -o '/lib.*\.[0-9]'
/lib64/libselinux.so.1
/lib64/libcap.so.2
/lib64/libacl.so.1
/lib64/libc.so.6
/lib64/libpcre.so.1
/lib64/libdl.so.2
/lib64/ld-linux-x86-64.so.2
/lib64/libattr.so.1
/lib64/libpthread.so.0
Copy the code

3, copy the above file to/TMP /myimage/lib64 and test it

[root@master1 /]# cp /bin/bash /tmp/myimage/bin/
[root@master1 /]# cp /bin/ls /tmp/myimage/bin/
[root@master1 /]# NEW_ROOT=/tmp/myimage
[root@master1 /]# NEW_LIBS="$(ldd /bin/ls | egrep -o '/lib.*\.[0-9]')"
[root@master1 /]# for i in $NEW_LIBS; do cp -v "$i" "${NEW_ROOT}${i}"; done
[root@master1 /]# NEW_LIBS="$(ldd /bin/bash | egrep -o '/lib.*\.[0-9]')"
[root@master1 /]# for i in $NEW_LIBS; do cp -fv "$i" "${NEW_ROOT}${i}"; done
[root@master1 /]# chroot /tmp/myimage/ /bin/bashBash - 4.2 -# /bin/ls /Bin lib64 bash - 4.2# /bin/ls -l /binTotal 1060-rwxr-xr-x 10 0 964536 Jan 29 08:52 bash-rwxr-xr-x 10 0 117608 Jan 29 08:55 ls bash-4.2# exit
exit
[root@master1 /]# 
Copy the code

Centos7-bash

[root@master1 /]# tar -C /tmp/myimage/ -c . | docker import - centos7-bash
sha256:b3bb78b50d050bc047920b1745c4d47717786ded575e9d09b7cc37fd889c03fb
Copy the code
  • View the image size: 4.37MB
[root@master1 /]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE Centos7 -bash latest b3bb78b50d05 5 seconds ago 4.37MBCopy the code

Test the container image

[root@master1 /]# docker run -it centos7-bash /bin/bashBash - 4.2 -# pwd                                                                                                                                         / bash - 4.2# /bin/ls /Bin dev etc lib64 proc sys bash-4.2# exit
exit
[root@master1 /]# 
Copy the code

reference

  • www.cnblogs.com/jipinglong/…
  • www.manongjc.com/article/148…

  • Use it first, know Kubernetes (K8S) through operation practice, accumulate more natural understanding
  • Share the knowledge of understanding, benefit from the field, complacent fate
  • The pursuit of simplicity, easy to understand, knowledge is also part of the context of knowledge, such as version, time, etc
  • You are welcome to leave messages and ask questions. Generally, you can reply and improve the document over the weekend
  • [email protected] 2022-3-29