How to run Ubuntu 18.04 in docker container ?

This post is in continuation with previous post “How to install Docker on Ubuntu 18.04 ?” , in this post we will demonstrate how you can run Ubuntu 18.04 inside docket container.

Search for the Ubuntu images from the repository as,

$ sudo docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   11424               [OK]                
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   469                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   249                                     [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   227                                     [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   110                 [OK]                
neurodebian                                               NeuroDebian provides neuroscience research s…   72                  [OK]                
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   44                  [OK]                
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   24                                      [OK]
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   24                                      
1and1internet/ubuntu-16-apache-php-5.6                    ubuntu-16-apache-php-5.6                        14                                      [OK]
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        13                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       11                                      [OK]
1and1internet/ubuntu-16-nginx-php-5.6                     ubuntu-16-nginx-php-5.6                         8                                       [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             7                                       [OK]
1and1internet/ubuntu-16-apache-php-7.1                    ubuntu-16-apache-php-7.1                        6                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             5                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   4                                       
1and1internet/ubuntu-16-nginx-php-7.0                     ubuntu-16-nginx-php-7.0                         4                                       [OK]
pivotaldata/ubuntu16.04-build                             Ubuntu 16.04 image for GPDB compilation         2                                       
smartentry/ubuntu                                         ubuntu with smartentry                          1                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              1                                       
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  1                                       [OK]
1and1internet/ubuntu-16-php-7.1                           ubuntu-16-php-7.1                               1                                       [OK]
pivotaldata/ubuntu16.04-test                              Ubuntu 16.04 image for GPDB testing             0                                       

Now, you can create download the Ubuntu 18.04 docker image using below command,

$ sudo docker create ubuntu:18.04

This will download ubuntu image as,

Unable to find image 'ubuntu:18.04' locally
18.04: Pulling from library/ubuntu
171857c49d0f: Pull complete 
419640447d26: Pull complete 
61e52f862619: Pull complete 
Digest: sha256:646942475da61b4ce9cc5b3fadb42642ea90e5d0de46111458e100ff2c7031e6
Status: Downloaded newer image for ubuntu:18.04

Alternatively if you are looking for any latest Ubuntu docker image, you can download the latest ubuntu docker image using below command,

$ sudo docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
d72e567cc804: Pull complete
0f3630e5ff08: Pull complete 
b6a83d81d1f4: Pull complete 
Digest: sha256:bc2f7250f69267c9c6b66d7b6a81a54d3878bb85f1ebb5f951c896d13e6ba537
Status: Downloaded newer image for ubuntu:latest

Once you downloaded ubuntu image, you can check the docker images as,

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              9140108b62dc        3 weeks ago         72.9MB
ubuntu              18.04               56def654ec22        3 weeks ago         63.2MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

As you can see now, we can 3 docker images locally, 1. helloworld 2. Ubuntu Latest version 3. Ubuntu 18.04

Now, you can run the Ubuntu 18.04 docker ubuntu using “IMAGE ID” as,

$ sudo docker run -it 56def654ec22

This command is “$ sudo docker run -it “IMAGE ID” ” , so we replace IMAGE ID with our ubuntu 18.04 IMAGE ID i.e. 56def654ec22

Once you run it, you will get the Ubuntu shell with root user as,

[email protected]:/#

You can also check the running docker images using below command,

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f22b02725263        56def654ec22        "/bin/bash"         4 minutes ago       Up 4 minutes                            blissful_hoover

Now, we have the Ubuntu 18.04 docker image running, where you got the shell and do anything you want once you get to console and use exit command to exit from the docker shell.

But this still has one limitation, suppose you get to console and does some work like creating files, installing some packages etc, and “exit” from that shell and after some time you again started the same Ubuntu image, you will see all the work you done previously has been lost, and you have to start all over again.. In our next post, “How to save / commit changes to docker image ?” we will demo how you can save the data in docker ubuntu image.

Leave a Comment