current position:Home>Make Linux system into docker image

Make Linux system into docker image

2022-01-26 22:23:11 Yan Long


DockerHub There are many images on the , Can meet most of the needs . In this case, we can go directly through docker pull ** from DockerHub Get the corresponding image on . Of course, there are some cases , The image we need DockerHub Does not exist on , for example :Linux Environment of Xinchuang , stay DockerHub It's hard to know the corresponding image , If you need this kind of image, you need to make the image through the running system .

Specific steps

1. Compress the system

  • View system directory
$ cd /
$ ls
bin  boot  data  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

/proc/sys/run/dev These directories are automatically generated when the system starts , Although it is also part of the file system , But they change every time they start up , So you should ignore them when packing

  • tar Complete the system compression package 1、 You can choose / Follow directory , then --exclude Exclude unnecessary directories 2、 You can also directly select the directory to be compressed
tar -cvpf /tmp/system.tar --directory=/ --exclude=proc --exclude=sys --exclude=dev --exclude=run --exclude=boot .


tar -cvpf /tmp/system.tar bin data  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  root  sbin  srv  tmp  usr  var

Parameter description : -c or --create Create a new backup file -f< Backup file > or --file=< Backup file > Specify the backup file -p or --same-permissions Restore files with original file permissions -v or --verbose Display during execution of instructions z or --gzip or --ungzip adopt gzip Instruction processing backup file --numeric-owner Replace user name and group name with user ID and group ID

tar For more parameters of the command, please refer to : Linux tar command Linux tar.gz、tar、bz2、zip Equal decompression 、 Detailed explanation of compression order

2. Import docker

Import docker Before , You'd better revise it docker The default storage path for . For specific modification methods, please refer to : Linux Modify the Docker Default storage path

  • tar Import docker
$ docker import /tmp/system.tar linux:10.1
// linux:10.1   Is a custom image and tag name 


cat /tmp/system.tar | docker import - linux:10.1

Run the image container

tips: When running the imported image, you must bring command, Otherwise, the startup will report the following error . final /bin/bash It can't be less

$ docker run -it -d **** /bin/bash

Tips : The created image file is too large , It will cause failure when starting the container : Getting the final child's pid from pipe caused "EOF"

Reference article : [Docker Image export and import ]

copyright notice
author[Yan Long],Please bring the original link to reprint, thank you.

Random recommended