Introduction to Docker(Using UbuntuVM)

Akandeadedayo
5 min readMay 25, 2024

Table of Content

  • What is Docker?
  • What are Containers?
  • What is DockerHub?
  • Sign up on DockerHub
  • Installation of Docker on Ubuntu
  • How to Work with Docker Containers?
  • How to Work with Docker Images
  • What is a Dockerfile?
  • How to Deploy a Dockerized App

Prequisites:

1.0 What is Docker?

According to AWS, Docker images are read-only templates that contain instructions for creating a container. A Docker image is a snapshot or blueprint of the libraries and dependencies required inside a container for an application to run.

2.0 What is a Container?

A container is a standard unit of software that packages up code and all its dependencies so the application can run quickly and reliably.

3.0 What is DockerHub?

DockerHub serves as a registry for storing Docker Images.

3.1 Sign up on DockerHub:

  1. Go to the [DockerHub] (https://hub.docker.com/) website.
  2. Click on the “Sign Up” button.
  3. Fill in the required information like your email address, username, and password.
  4. Complete the sign-up process by verifying your email address.

3.2 Installation of Docker on Ubuntu:

  1. Click on this link: Install Docker Engine on Ubuntu | Docker Docs
  2. Follow the installation and make sure your Ubuntu machine is one of these versions:
  • Ubuntu Noble 24.04 (LTS)
  • Ubuntu Mantic 23.10 (EOL: July 12, 2024)
  • Ubuntu Jammy 22.04 (LTS)
  • Ubuntu Focal 20.04 (LTS)

3.3 To Confirm Installation:

 $ sudo groupadd docker 
# Add your user to the docker group
$ sudo usermod -aG docker $USER
# Restart VM first or logout and Login
$ docker version
$ docker info

4.0 Understanding the django application

$ sudo apt update
$ sudo apt install python3
  • Install pip if it is not installed with python3 with
$ sudo apt install python3-pip
  • Create Python virtual environment
$ sudo apt install python3-virtualenv
$ virtualenv --version
$ cd ProjectApplication/
$ virtualenv venv
  • Install django
$ pip install django
  • Start the django application
$ python3 manage.py runserver 0.0.0.0:8000

To see your application on your computer if you are using Ubuntu on VirtualBox, please follow the steps in the images below.

1. Open VirtualBox and Click on your Ubuntu machine

2. Click On settings

3. Click on Network

4. Click on Port forwarding

5. Add

  • Name: Django
  • Protocol: TCP
  • Host port: 8000
  • Guest port: 8000

Then navigate to any brower on your computer and enter:

127.0.0.1:8000/login_view/

5.0 Working with Images

Create an image from Dockerfile.

Let’s first understand “What is a Dockerfile?**

Docker builds images automatically by reading instructions from a Dockerfile, which is a text file that contains all commands in order to build a given image.

This is an example of a Dockerfile:

FROM python:3-alpine
WORKDIR /projectapplicationCOPY . .EXPOSE 8000COPY requirements.txt .RUN pip3 install -r requirements.txt --no-cache-dirENTRYPOINT ["python3"]CMD ["manage.py", "runserver", "0.0.0.0:8000"]

Let’s break this down:

  • FROM: The image as the ground, which is Python.
  • WORKDIR: The directory where we want to run commands.
  • COPY: Copy everything into WORKDIR.
  • EXPOSE: Specify the ports where we want to access the application on the container.
  • COPY: Copy requirements.txt into WORKDIR.
  • RUN: Installs what is in requirements.txt.
  • ENTRYPOINT: Use python3 to run.
  • CMD: Command to start the container.

Let’s understand some terminologies which are registries, Images, and tags.

Registry: A service responsible for hosting and distributing images.

Repository: A collection of related images (usually providing different versions of the same application/service).

Tag: An alphanumeric identifier attached to images within a repository.

Now, Let’s build an Image:

$ docker build –t appmigro01/projectapplication:1.0v .

7.0 Working with Docker Containers:

First, let’s pull our image from DockerHub:

To check the image:

$ docker images

Now let’s run it:

$ docker run –d –p 8001:8000 appmigro01/projectapplication:1.0v

The first 8001 is Host port and the second 8000 is Container port.

  • d specifies : run the container in the background
  • p: means traffic from the host of port 8001 will be forward to container on port 8000 as well.

Check if the container is running:

$ docker ps -a
$ curl 127.0.0.1:8001/login_view/
# The result should be
$ <h1> Welcome to Appmigro, Enjoy your stay <h1>

8.0 Deploying a Dockerized Application

After it has been successfully started, let’s login into Docker using our credentials when we signed up on GitHub with the command:

$ docker login
# Then input username and password:$ Username:
$ Password:
# Then push the built image into DockerHub using the command:$ docker push appmigro01/projectapplication:1.0v

9.0 Stopping and Restarting a Docker Container:

# Stop container
$ docker stop CONTAINER_ID
# Start container
$ docker start CONTAINER_ID
# Restart container
$ docker restart CONTAINER_ID

9.1 Deleting a Container & Images

# Remove/Delete container
$ docker rm CONTAINER_ID
# Remove/delete Image
$ docker rmi IMAGE_ID

10.0 Pull images from Dockerhub

# let’s pull our image from DockerHub:
$ docker pull appmigro/projectapplication:1.0v

About the Author:

A Leading automation expert on DevOps and Cloud Migration.
 Multiple years of experience in DevOps execution using cutting edge tools in the market today.
 Adept in Containerisation, Orchestration, LoadBalancing, Fault Tolerance, CICD, Monitoring and Logging among others.

A Leading automation expert on DevOps and Cloud Migration.
Multiple years of experience in DevOps execution using cutting edge tools in the market today.
Adept in Containerisation, Orchestration, LoadBalancing, Fault Tolerance, CICD, Monitoring and Logging among others.

Senior DevOps Engineer, Appmigro

(11) Akande (Olalekan) Adedayo | LinkedIn

--

--