DEV Community

Cover image for [For developers] Install Docker on Ubuntu and build a virtual machine
Takahashi Akari
Takahashi Akari

Posted on

[For developers] Install Docker on Ubuntu and build a virtual machine

Introduction

Although it can be used in a production environment, this time we will describe how to build a virtual machine with Ubuntu 22.04LTS (desktop) for development.
Reasons why a virtual machine is necessary include 'I want to quickly create a common environment for the team,' 'I can rebuild it even if it breaks,' and 'I can restore it to its original state even if I make a mistake.'
For the time being, this time, I will describe how to build it quickly.

*Use Terminal.

build environment

It is the environment of the PC at the time of writing the article.
OS: Ubuntu 22.04LTS (Desktop)
CPU: 4Core
Memory: 16GB
Storage: 512MB

Now let's install Docker.

1. Update packages

Update the package to the latest version.

$ sudo apt update -y
$ sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Install Docker

Install Docker now.
The installation procedure is as follows.

$ sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update -y
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-compose
Enter fullscreen mode Exit fullscreen mode

Docker installation is now complete.

Make Docker commands available without sudo.

$ sudo group add docker
$ sudo gpasswd -a $USER docker
$ sudo systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

Now that you can use Docker commands without sudo, let's try it out.

check docker docker-compose installation

$ docker -v
Docker version 20.10.17, build 100c701

$ docker-compose
docker-compose version 1.29.2, build unknown
Enter fullscreen mode Exit fullscreen mode

It is OK if the version is displayed.
Now, let's install the latest UbuntuOS on Docker.

Install the latest UbuntuOS on Docker using docker-compose

Create a directory anywhere here.

$ mkdir ~/temp && cd ~/temp
$ touch docker-compose.yml
$ vim docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

Make your docker-compose.yml look like this: (simple configuration)

version: '3'
services:
   ubuntu:
     image: ubuntu:latest
     tty: true
Enter fullscreen mode Exit fullscreen mode

Run the latest Ubuntu on Docker using docker-compose

Now, it's finally time to run Ubuntu on Docker.

$ docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

That's it. Wait for a while.

Check if Ubuntu is running on docker and log in

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b671926c5cd ubuntu:latest "bash" 5 minutes ago Up 5 minutes docker_ubuntu_1
Enter fullscreen mode Exit fullscreen mode

One virtual machine is running! Great success
Now let's log into the virtual machine. Copy the above CONTAINER ID and execute the following command.

docker exec -it 4b671926c5cd /bin/bash
root@4b671926c5cd:/#
Enter fullscreen mode Exit fullscreen mode

I was able to log in as root! Now let's update the package.

root@4b671926c5cd:/# apt update -y && apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

You can exit the container with "exit".

stop the virtual machine

You can't run a virtual machine all the time. Now stop the virtual machine.

$ docker-compose stop
Enter fullscreen mode Exit fullscreen mode

delete all Docker virtual machines

It can also consume machine resources, so you may need to clear it as well.
I will show you how to delete it.

$ docker-compose down
$ docker system prune -f -a
Enter fullscreen mode Exit fullscreen mode

that's all.

Top comments (0)