How to create Ubuntu docker base image

Have you just started working on docker containers? You may have also thought about how to create docker base image of the Operating System. In this post, we will learn to create Ubuntu docker base image.

Note: This method is applicable to create docker base image of all Ubuntu version. You can find Ubuntu release code name from here – https://wiki.ubuntu.com/Releases .

Goal

Create Ubuntu 18.04 LTS docker base image.

Prereqs

Debootstrap: We will use the deboostrap script to create the docker base image. To know more about debootstrap visit https://wiki.debian.org/Debootstrap .
Ubuntu Release Code Name: To create docker image we need ‘Ubuntu release code name’ for eg. “Bionic Beaver” or “Xenial Xerus”.

About our system

We are using Ubuntu 16.04 LTS Desktop but particular Operating System is not restrictive for creating docker base image. You can create Ubuntu docker image from other Linux system also but your system should have debootstrap script.

Steps to create Ubuntu docker base image

We are using our Ubuntu 16.04 LTS Desktop for creating base image.Hence,all commands are for debian/ubuntu system.

  1. Login as root or superuser
  2. Either login as root or become superuser by using command sudo su

  3. Create directory for docker image workshop
  4. Create directory for docker base image work and change to after creating it.

    mkdir -p /opt/docker_base_images
    
    cd /opt/docker_base_images
    
  5. Install debootstrap
  6. apt install debootstrap
    

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# apt install debootstrap
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following NEW packages will be installed:
      debootstrap
    0 upgraded, 1 newly installed, 0 to remove and 35 not upgraded.
    Need to get 37.2 kB of archives.
    After this operation, 262 kB of additional disk space will be used.
    Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 debootstrap all 1.0.78+nmu1ubuntu1.6 [37.2 kB]
    Fetched 37.2 kB in 0s (60.1 kB/s)      
    Selecting previously unselected package debootstrap.
    (Reading database ... 380627 files and directories currently installed.)
    Preparing to unpack .../debootstrap_1.0.78+nmu1ubuntu1.6_all.deb ...
    Unpacking debootstrap (1.0.78+nmu1ubuntu1.6) ...
    Processing triggers for man-db (2.7.5-1) ...
    Setting up debootstrap (1.0.78+nmu1ubuntu1.6) ...
    root@sharadchhetri:/opt/docker_base_images#
    
  7. Run debootstrap
  8. Here,we are creating Ubuntu 18.04 LTS docker base image. For this, we will use the Ubuntu release code name that is ‘Bionic’. Command will take some time to finish.

    After command get completed, you will see the directory relevant to Ubuntu code name you used.In our case directory called ‘bionic’ is created.

    debootstrap bionic bionic > /dev/null
    

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# debootstrap bionic bionic > /dev/null
     
    root@sharadchhetri:/opt/docker_base_images# 
    root@sharadchhetri:/opt/docker_base_images# ls -l
    total 4
    drwxr-xr-x 21 root root 4096 Oct  8 23:22 bionic
    root@sharadchhetri:/opt/docker_base_images# 
    
  9. Explore the newly created docker image directory
  10. Let’s explore what you got from debootstrap command. We are listing the files/directories in ‘bionic’ directory.

    In given below you can find the lsb_release output, showing the release details.

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# ls bionic/
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    root@sharadchhetri:/opt/docker_base_images# cat bionic/
    bin/   dev/   home/  lib64/ mnt/   proc/  run/   srv/   tmp/   var/   
    boot/  etc/   lib/   media/ opt/   root/  sbin/  sys/   usr/   
    root@sharadchhetri:/opt/docker_base_images# cat bionic/etc/l
    ld.so.cache     ld.so.conf.d/   libaudit.conf   locale.gen      logcheck/       logrotate.conf  lsb-release
    ld.so.conf      legal           locale.alias    localtime       login.defs      logrotate.d/    
    root@sharadchhetri:/opt/docker_base_images# cat bionic/etc/lsb-release 
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=18.04
    DISTRIB_CODENAME=bionic
    DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"
    root@sharadchhetri:/opt/docker_base_images#
    
  11. Docker import
  12. Now import the docker image in local system.

    sudo tar -C bionic -c . | docker import - bionic
    

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# sudo tar -C bionic -c . | docker import - bionic
    sudo: unable to resolve host sharadchhetri
    sha256:48660241bce0fc8350e07f2ea84cbdc09e2027f3fc6cde21c2a6f8b7ee039967
    root@sharadchhetri:/opt/docker_base_images# 
    
  13. List docker images
  14. Now list docker images, you can easily find your freshly imported docker image.

    docker images
    

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    bionic              latest              474342d581ce        9 seconds ago       289MB
    root@sharadchhetri:/opt/docker_base_images#
    
  15. Verify the new docker image
  16. Run this command to verify the Ubuntu docker image release version.

    docker run bionic cat /etc/lsb-release
    

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# docker run bionic cat /etc/lsb-release
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=18.04
    DISTRIB_CODENAME=bionic
    DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"
    root@sharadchhetri:/opt/docker_base_images# 
    

    In given below Steps, we will push the image in Docker Hub.It is your choice if you want to share your docker image in internet .

  17. Login to docker hub
  18. Now login to Docker Hub.Always remember, you must have registered login id in https://hub.docker.com for this step. We already have our docker hub login.

    Run the given below command.

    docker login
    

    Output from our system
    Use your username of docker hub.

    root@sharadchhetri:/opt/docker_base_images# docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
    Username: sharadchhetri
    Password: 
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store
    
    Login Succeeded
    root@sharadchhetri:/opt/docker_base_images#
    
  19. Push image to Docker Hub
  20. Now push the newly created image to Docker Hub. Here, first we will tag the image and the push to Docker Hub.
    Use your Docker Hub username instead of ‘sharadchhetri’ in given below command.

    docker tag bionic sharadchhetri/bionic:latest
    
    docker push sharadchhetri/bionic:latest
    

    Output from our system

    root@sharadchhetri:/opt/docker_base_images# docker tag bionic sharadchhetri/bionic:latest
    
    root@sharadchhetri:/opt/docker_base_images# docker images 
    REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
    sharadchhetri/bionic   latest              474342d581ce        27 minutes ago      289MB
    bionic                 latest              474342d581ce        27 minutes ago      289MB
    root@sharadchhetri:/opt/docker_base_images#
    root@sharadchhetri:/opt/docker_base_images# docker push sharadchhetri/bionic:latest
    The push refers to repository [docker.io/sharadchhetri/bionic]
    cd4e432e52ec: Pushed 
    latest: digest: sha256:e9cf9ce4d13b2dbcd6c4ed038c0d7ae1922a8d458dac1b1bfe10f564b51efd6e size: 529
    root@sharadchhetri:/opt/docker_base_images# 
    
  21. Check your new image in Docker Hub
  22. Login to Docker Hub. You will see your freshly created image there. You can use this image for creating docker containers directly pulling from your own Docker Hub repository.

2 thoughts on “How to create Ubuntu docker base image”

  1. Hi, I am using ubuntu 16.04lts and have done so for about 3 years now.
    I am curious about the docker, what is its use.? It seems to favour the 18.04lts program, which I have a small problem with. I downloaded 18.04 and horror, it is no longer Debian based, I still have problems using 16.04, I follow to the letter trying to E.G. Install Pretty Good Solitaire, which relies on “Wine”. I have a printed copy of how to do this, but as always, it gets to a point where I am asked to open something, but that something does not exist on my machine, as you can imagine it is frustrating. If I went to 18.04 I would be back at square one and have to re-learn everything again. Being a senior person (75) it is hard to retain knowledge. Perhaps some kind soul out there will open my eyes and explain how simple things are to change, (step by step).
    Thanks for reading my groans….

    Rod.

Comments are closed.