Running Ubuntu within Docker

Hey guys!
This post will enable running Ubuntu in a Docker container. You might need this for some reasons, for me it was to compile Ubuntu Touch under Arch Linux.

I wanted to try out Ubuntu Touch on my aging phone, since Android was getting too slow due to Google Play Services even on KitKat. Since Ubuntu Touch was based off AOSP 4.4.2, I expected the build process to work on Arch Linux too.

Unfortunately, there were dependencies to GCC 4.8 and dpkg. Therefore I considered running it where it feels at home, in Ubuntu.

An Ubuntu VM would be computationally wasteful, as I'm already running a Linux kernel. I tried chroot, but it was so complex that I had to scrap it. I then considered using Docker and voilĂ , a perfect Ubuntu environment within Arch Linux.
You need to have Docker installed (the Community Edition is enough). This tutorial will be based on Arch Linux, but the commands to run will be similar.
 I started off by searching for an Ubuntu Docker container, which I found here. Next, I tried it out by running the following:
 $ sudo docker run ubuntu
I waited for that to finish. Once done, nothing happened.
I searched to find that Docker created the container, but it exited immediately because it had nothing else to do. So, i made the Ubuntu container run interactively like so:
$ sudo docker run -t -i ubuntu /bin/bash
And i was greeted with a cozy root shell:
root@0514e592e538:/#
...until, I tried to exit and get back in: 
root@edd4b44c703b:/#
If you didn't catch that, notice the host name in both shells. They're different, signifying that they were two different containers, having no persistent data. Bummer.

More searching led me to the following commands:
$ sudo docker start <container name>
$ sudo docker attach <container name>
These commands can restart stopped containers (including their data)  and reattach their stdin and stdout to the current terminal.

You can get the container name from the following command:
$ sudo docker ps -al
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
edd4b44c703b        ubuntu              "/bin/bash"         6 minutes ago       Exited (0) 2 minutes ago                       pedantic_cray

The line under NAMES is the container name (pedantic_cray in this case).

You can run your familiar Ubuntu commands as usual. Make sure to update the APT package lists with "apt update". Take note that the Ubuntu Docker image is very minimal, so you might want to add more packages to make it more familiar.

You now have an Ubuntu environment within Arch Linux at no computational overhead :D

Cheers!
Technohacker

Comments

Popular Posts