Docker Ubuntu 20.04

2/13/2022by admin

Install a Docker container and then install Kubernetes with two nodes on Ubuntu 20.04. How to install Docker; How to install Kubernetes; How to configure a master and two worker node; How to join a worker node to a Kubernetes cluster; So, let's start the installation. Step 1 - Update Ubuntu. Always recommended updating the system packages.

Installing Docker. Docker should now be installed, the daemon started, and the process enabled to start on boot. To verify: NOTE: To avoid using sudo for docker activities, add your username to the Docker Group. Installing docker-compose. Notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 20.04 ( focal ). Finally, install Docker: sudo apt install docker-ce. Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running.

  • Docker is a combination of platform as a service products that use virtualization to provide software in packages called containers which can communicate with each other through well-defined channels. This tutorial focuses on Installing the latest Docker release on Ubuntu 20.04 LTS Focal Fossa.
  • See full list on hub.docker.com.

Focus

Today we’ll create an Asp.net core 3.1 hello world web application and host it inside a docker container on Ubuntu 20.04. The main intention of writing this blog is to focus on hosting web apps on minimum hardware resources. Appropriately, I have chosen Ubuntu Server and working only with the terminal window command to build and host the application. I have tried using 512 MB RAM server, but it works extremely slow. Hence would not recommend it. You will need minimum 1 GB RAM machine. A 2 GB RAM machine will run very smoothly in this scenario.

You can try using SSD based machines with lower configuration for better performance but I haven’t tried it.

Install dotnet core sdk and runtime version

Ensure dot net core sdk and runtime are installed on your Ubuntu Server from terminal window:

Run this command in terminal:
dotnet --list-sdks

If it does not return the sdk version, then run the following commands in the terminal to install dotnet sdk on your Ubuntu Server 20.04. The instructions (microsoft docs) here do not work correctly for Ubuntu 20.04. Hence run these commands.

If Ubuntu is prompting you “permission denied” for most of your terminal commands and you want to skip typing sudo every time, give the following command
sudo chown <username> <directory>
OR
However, run the following only if you know what you are doing
sudo -s

Run this command again to verify if dotnet sdks are installed correctly: dotnet --list-sdks

Lets create the asp.net core hello world application

Run command: dotnet new webapp -o aspnetcoreapp --no-https --force

This creates the web app into aspnetcoreapp folder. Using the --no-https parameter forces dotnet to not use developmental SSL certificate while running the app. Right now, it would make things simpler to build the first hello world app.

Change the default Program.cs file. Run command nano Program.cs to open a text based editor in your terminal window. Replace the contents in that file with the one below:

I have used http://0.0.0.0:5000. The “0.0.0.0” is used so that the site can easily be accessible from even outside the docker container

Press Ctrl-X, Yes, to save and close the nano editor.

Swtich to that folder and run command: dotnet watch run. This will run the site on port 5000.
You can run curl http://localhost:5000 in your terminal window and check the HTML output.

Very important point – Remember that the site runs on port 5000. However, when the same site is hosted inside the docker container, you need to keep in mind that the site is running on port 5000 even inside the container.

Install docker on the Ubuntu server

Install docker, run:
sudo apt install docker.io

Start docker service, run:
sudo systemctl start docker
sudo systemctl enable docker

Check if docker service is running, run:
sudo systemctl status docker

Create a docker base image

Assuming you are in aspnetcoreapp folder, create a Dockerfile in that folder. As a second hint, you should be in the folder where you .csproj file exists.

Results

To create the Dockerfile, run nano Dockerfile and paste the following contents inside it.

You need to run this command to build the docker image with your web app inside it. This instructs the docker process to generate your docker image.

sudo docker build -t counter-image -f Dockerfile .

This will also add donet SDK and asp.net core runtime inside the container. This can take a while depending on your internet connection speed. Secondly, this also publishes your web app into /app folder inside the container.

The output will be like the screen shot below:

To check what docker images are installed on the server, run:

docker images

Now you base image is ready with the name counter-image.

Run your asp.net core site inside the docker container

Create a docker container with your base image:

Docker Ubuntu 20.04 Python 3.8

sudo docker run -d -p 8080:5000 --name myapp counter-image

Output – 8f18f4d4cde39820e2ad9a89c914b3a46ee71a238d8e62fd7f4af0b0f20b5909

You will see an output similar to what’s mentioned above. This command means that you have created a docker container with base image of counter-image. Your container name is myapp.

Importantly, when you write 8080:5000, your site will be accessible on host Ubuntu machine from 8080 port. Further, on your host machine (Ubuntu server) you can type http://localhost:8080. This will redirect to your request to container’s site running on port 5000. Keep in mind that the site is running on port 5000 inside your container.

You don’t need any web server to be running on your Ubuntu Server (host machine).

Run command docker ps -a to check which containers are running: (output will be like)

You will see that our container is in running state. Check the STATUS column which says “Up XX minutes”. If the status says exited, then your container isn’t running.

You are done! Check your site running status

If you want to run a command inside the container, run:

Cached

docker exec myapp curl http://localhost:5000

It if gives your page’s html output, that means your site is running successfully inside the container.

Now you can access the site from your host machine (Ubuntu server). Type:

Docker ubuntu 20.04 minimal

curl http://localhost:8080ORwget http://localhost:8080

If you see HTML output of your site, you can access your site from your host server. Additionally, if your host server has a public Static IP address assigned to it, then your site is live on the internet.

Additional tip

My setup: My super host machine is Windows 10. I installed VMWare Player and installed the Ubuntu Server as a guest machine. The docker container runs inside the Ubuntu Server. We’ll be doing port forwarding to access the site from my super host machine.

So to check your site inside your browser on Windows 10 (super host :)), you will need to do the following steps. I’m assuming that your Ubuntu Server is running with a NAT type of network connection on VMWare:

  1. My Ubuntu Server’s IP address is 192.168.26.150.
  2. You need to open and edit vmnetnat.conf inside your C:ProgramFilesVMWare folder. Use your favourite text editor (notepad++) in administrator mode.
  3. Jump to section [incomingtcp] and add this line 8888 = 192.168.26.150:8080. Save the file.
  4. Restart VMware NAT Service in your services console on Windows 10.
  5. Open TCP port 8888 on your Windows 10 Firewall.
  6. This means that any request coming on my super host machine (Windows 10) on port 8888 will be redirected to my Ubuntu Server on port 8080. From there on, the request will be redirected to the container site running on port 5000.
  7. Now to check the site from my Windows 10 browser, I now just need to type http://localhost:8888 on my browser and voila!, the site opens up on Windows 10.

Cleanup – docker container and image

Stop the container by running: docker stop myapp

Remove your container, run: docker rm myapp

Remove your docker base image, run: docker rmi counter-image:latest --force
Remember, if you remove the image counter-image, you have to re-build it with the docker build command above.

Conclusion

Even after running the docker container, the RAM usage remains around 430 MB. So an SSD based machine with 1 GB of RAM would also be sufficient to host your small static sites inside docker on Ubuntu machines.

I hope you enjoyed building and hosting your Asp.net core 3.1 hello world application inside a docker container on Ubuntu 20.04. Thank you for your time. Let me know your thoughts on how to make this article better.

  • Overview – Install Docker on Ubuntu 20.04

Overview – Install Docker on Ubuntu 20.04

Docker allows you to run container images and is quickly becoming the standard way to deploy applications. In this tutorial we will walk through how to install Docker on Ubuntu 20.04. And how to start a container image. If you are new to Linux, Ubuntu or Docker, this is a great tutorial to get you up and running.

This tutorial to install Docker on Ubuntu 20.04 has only been tested on 20.04. However it should be applicable to other version of Ubuntu with zero or minimal changes.

Step 1 – Find the Docker package in Ubuntu’s apt repository

The name of the Ubuntu package for Docker is docker.io. But just to confirm it hasn’t changed lets use the apt search command to find the package.

apt search docker.io

This command searches the Ubuntu package repository cache for packages containing docker.io in the name and description. You will see out put similar to this:

Sorting… Done
Full Text Search… Done
docker-doc/focal-updates,focal-security 19.03.8-0ubuntu1.20.04.1 all
Linux container runtime -- documentation

docker.io/focal-updates,focal-security 19.03.8-0ubuntu1.20.04.1 amd64
Linux container runtime

python3-docker/focal 4.1.0-1 all
Python 3 wrapper to access docker.io's control socket

ruby-docker-api/focal 1.22.2-1 all
Ruby gem to interact with docker.io remote API

We are looking for the second entry docker.io, Linux Container Runtime.

Step 2 – Install the Docker on Ubuntu 20.04

To install the Docker package on Ubuntu 20.04 we will use the apt install command:

Docker Ubuntu 20.04

sudo apt install docker.io

You will see output indicating all the packages that will be installed as a result. These packages are dependencies of the Docker.io package. Additionally you will be prompted to confirm the installation. The output will look similar to this:

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
libgd3 libxpm4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
bridge-utils cgroupfs-mount containerd dns-root-data dnsmasq-base libidn11 pigz runc ubuntu-fan
Suggested packages:
ifupdown aufs-tools debootstrap docker-doc rinse zfs-fuse zfsutils
The following NEW packages will be installed:
bridge-utils cgroupfs-mount containerd dns-root-data dnsmasq-base docker.io libidn11 pigz runc ubuntu-fan
0 upgraded, 10 newly installed, 0 to remove and 35 not upgraded.
Need to get 69.7 MB of archives.
After this operation, 334 MB of additional disk space will be used.

Package installation can take some time. Hence, if your connection is slow you might be waiting minutes or longer for all the packages to download.

Step 3 – Start and enable the Docker service

Unlike some package installations, the Docker service will not be running or enable to start at boot. As a result we must start it manually and configure it start on boot. We can do that with the systemctl command.

First let’s enable Docker to start on boot:

sudo systemctl enable docker

After running systemctl enable docker you should see output similar to this:

Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.

Docker will start on your next reboot. Next let’s start the Docker service so we can explore it without rebooting. Again we will use systemctl:

sudo systemctl start docker

If this command executes without error congrats! Docker should be running. But lets confirm just to be sure by looking at the process table. Use a combination of the ps and grep commands. The ps command lists the process table. We pipe the output of ps to the grep command so we can search for the string docker. Run the command pipeline as follows:

ps aux grep docker

You should see output similar to the out from process table. In my case the docker process is running with a PID of 31130:

ps aux grep docker
root 31130 0.3 8.1 861288 81660 ? Ssl 22:39 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Step 4 – Pull a Docker container image

Before we boot our first Docker container we must download an image. We will use the NGINX webserver image. This image runs the popular NGINX server and is based on Alpine Linux. Alpine is a distribution commonly used in container images. Because of its small size and optimizations it makes a great base distribution for building container images. Additionally its small size means we won’t be waiting very long for our download.

To get the image we will use the docker pull command:

sudo docker pull nginx

The result of the command should be similar to this:

Using default tag: latest
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete 5a9f1c0027a7: Pull complete b5c20b2b484f: Pull complete
166a2418f7e8: Pull complete
1966ea362d23: Pull complete
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest

The image digest hash might be different because the image gets updated frequently. Now let’s use the docker image command to confirm the image has been pulled and installed to our local Docker registry:

sudo docker image ls

The output should be similar to this:

Docker Ubuntu 20.04 Minimal

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 11 days ago 133MB

The NGINX image has been pulled down to our Docker host. As you can see the image size is only 133MB.

Step 5 – Run a Docker Container

The moment you have been waiting for, running your first container! To start a container we tell Docker daemon to create a new container based on the NGINX image we installed. We do this using the docker container create command:

sudo docker container create nginx

This command will return the ID of the container created. Next we use the docker ps command to view running containers:

sudo docker ps

It should return an empty list that looks like this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Why is the list empty? Because we created the container but we have not started it yet. However if we pass docker ps the -a flag it will show us all containers. Even stopped containers:

sudo docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea48a441d83b nginx '/bin/sh' 7 minutes ago Exited (0) 54 seconds ago vibrant_boyd

To start the container we use the docker start command:

Docker Ubuntu 20.04 Proxy

sudo docker start ea48a441d83b

You will need to replace the container id (ea48a441d83b) with the ID of your container. Which you can get from the docker ps -a command.

Check the docker ps command again. The container should be running now:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2f842b42f99 nginx '/docker-entrypoint.…' 17 seconds ago Up 2 seconds 80/tcp romantic_ishizaka

Step 6 – Run a command in the container

Your NGINX container is running. Let’s run a command inside the container and confirm we see the NGINX process. To do this let’s use the docker exec command:

sudo docker exec -it e2f842b42f99 ls

Breaking down this command, -it are flags telling docker to run in interactive mode and to allocate a tty. Then we pass in the container ID and finally the ls command which runs in the container.

Docker Ubuntu 20.04

Learn more about Docker

Looking to increase your Docker skill set? We highly recommend the following books on Docker:

Install Docker on Ubuntu 20.04 – Conclusion

You now have a basic NGINX container running on your Linux system. But this is just the tip of the iceberg. We will be following up with more tutorials that explain how to customize the NGINX image to include your app or website. And additionally how to expose it on the network.

Reference Links

  • The Docker website.
  • Ubuntu package search results for docker.io.
  • Alpine Linux.
  • The NGINX docker image page on Dockerhub.
  • The alpine docker image page on Dockerhub.
Comments are closed.