To create an Ubuntu 20.04 Docker image using debootstrap, follow these steps:
Prerequisites
Make sure you have the following installed:
debootstrapchroottardocker
You can install debootstrap on your system if it’s not already there:
sudo apt update
sudo apt install debootstrap
1. Create the Ubuntu 20.04 Root Filesystem with debootstrap
Use debootstrap to create a minimal Ubuntu 20.04 filesystem in a local directory (e.g., ubuntu-20.04):
sudo debootstrap --arch=amd64 focal ubuntu-20.04 http://archive.ubuntu.com/ubuntu/
This command downloads and installs a basic Ubuntu 20.04 environment into the ubuntu-20.04 directory. The --arch=amd64 option specifies the architecture, and focal is the codename for Ubuntu 20.04.
2. Configure the Filesystem (Optional)
You can perform additional configuration within the new root filesystem. For example, set up a basic sources.list for apt or install some packages.
To enter the chroot environment:
sudo chroot ubuntu-20.04
Inside chroot, you can set up a sources.list file or install any necessary packages. For example, to update the package list:
apt update
Once you’re done, exit the chroot environment:
exit
3. Package the Filesystem as a TAR Archive
Next, create a tarball from the ubuntu-20.04 directory:
sudo tar -C ubuntu-20.04 -c . | sudo docker import - ubuntu:20.04
This command:
- Uses
tarto archive the contents of theubuntu-20.04directory. - Pipes the archive to Docker’s
importcommand. - Tags the imported image as
ubuntu:20.04.
4. Verify the Docker Image
You can verify that the image has been created by listing your Docker images:
docker images
You should see ubuntu with the tag 20.04 in the list.
5. Run a Container from the Image
To test the new image, run a container:
docker run -it ubuntu:20.04 /bin/bash
You should now be inside a running container based on your custom Ubuntu 20.04 image created with debootstrap.