How To Install and Use Docker on Ubuntu 22.04

how-to-install-and-use-docker-on-ubuntu-22-04
Spread the love

Introduction

Docker is an open software platform enabling users to package, allowing you to build, test, and run applications in containers. Containers are isolated, similar to virtual machines, but more portable and resource-friendly. The isolation allows for executing many containers simultaneously on a single host.

In this blog, we install and Use Docker on Ubuntu 22.04 by following the steps below.

Prerequisites

  1. Ubuntu 22.04 installed.
  2. A user account with sudo privileges.
  3. An active internet connection.
  4. Access to the terminal (Ctrl + Alt + T).

    Installing Docker on Ubuntu 22.04

    Ubuntu is the number one platform for managing Docker or Kubernetes containers. This is because Ubuntu runs the containers at scale, it is fast, secure, and open-source, powering millions of machines worldwide. 

    To install Docker on Ubuntu follow the steps below:

    1. Uninstall Old Versions Docker(Optional)

    If you have an older version of Docker installed, you should uninstall it:

    $ sudo apt remove docker docker-engine docker.io containerd runc

    2. Update the Package Repository

    Run the following command to update your existing list of the system’s packages and ensure the latest prerequisite packages are installed on your system.

    $ sudo apt update

    3. Install Prerequisite Dependencies

    Docker requires a few prerequisite dependencies to be installed on the system:

    $ sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

    4. Download Docker’s Official GPG Key

    Add Docker’s official GPG key for verification to your system:

    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

    5. Set Up the Docker Repository

    Run the following command to add the Docker’s official repository to your APT sources:

    $ 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

    6. Install Docker Engine

    Run the following command to update your package database again and install Docker:

    $ sudo apt update
    $ sudo apt install docker-ce docker-ce-cli containerd.io

    7. Check Docker Version

    To verify that Docker is installed successfully, check the version:

    $ docker --version

    8. Start Docker and Enable it at Boot

    Docker should start automatically after installation, but you can ensure it’s enabled to start on boot with the following:

    $ sudo systemctl start docker
    $ sudo systemctl enable docker

    9. Check Docker Status

    Check if Docker is running on your system:

    $ sudo systemctl status docker

    10. Run Docker as a Non-root User (Optional)

    By default, Docker commands need to be run with sudo. To run Docker as a non-root user:

    1. you can add users to the Docker group, allowing them to run Docker without root privileges:

    $ sudo usermod -aG docker $USER

    2. To apply these changes you can type the following to apply the changes in the current session:

    $ newgrp docker

    11. Test Docker Installation

    Run Postgres in the Docker container to verify the installation:

    1. Pulling the latest Official Postgres Docker Image from Docker Hub.

    $ docker pull postgres

    This command pulls the Postgres image from Docker Hub.

    2. Enter the following docker run command to start a new Postgres container: 

    $ docker run --name=postgres-db -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres@123 -e POSTGRES_DB=testdb -p 5432:5432 -d postgres:latest 

    3. This command allows you to enter a running container:

    $ docker exec -it postgres-db bash

    4. login to Postgres db

    psql -h localhost -p 5432 -U postgres -d testdb

    5. Give all privileges 

    GRANT ALL PRIVILEGES ON DATABASE testdb TO postgres;

    12. Working With Docker Volumes

    A Docker volume is a filesystem mechanism allowing users to preserve data generated and used by Docker containers. The volumes ensure data persistence and provide a better alternative to persisting data in a container’s writable layer because they don’t increase the Docker container size.

    Use Docker volumes for databases or other stateful applications. Since the volumes are stored on the host, they don’t depend on the container but allow for easy data backups and sharing between multiple containers.

    1. Create a Docker Volume

    $ sudo docker volume create [volume_name]

    2. Remove a Docker Volume

    $ sudo docker volume rm [volume_name]

    13. Using Docker Commands

    You can now use Docker commands to manage containers, images, and networks. Here are a few basic commands:

    1. List all containers (stopped ones included):

      $ docker ps -a

      2. List Docker images:

          $ docker images

          3. Remove an image:

          $ docker rmi <image_name>

          4. Stop a running container:

          $ docker stop <container_id>

          5. Remove a container:

          $ docker rm <container_id>

          6. To stop a running docker:

          $ sudo systemctl stop docker

          7. To stop a running docker service:

          $ sudo service docker stop

          8. To stop the docker socket:

          $ sudo systemctl stop docker.socket

          14. Docker Network Commands

          1. List of Docker networks:

          $ docker network ls

          2. Create a new docker network:

          $ docker network create -d bridge <network_name>

          3. Outputs detailed information on the specified network or networks

          $ docker network inspect [network-name]

          4. Removes all unused networks in a container

          $ docker network prune

          5. Removes the specified network or networks

          $ docker network rm [network-name]

          Docker Community Edition (CE) is now installed and ready to use on Ubuntu 22.04.

          Also Read: What is Kubernetes? | Kubernetes explained

          15. Define Docker Container Networking so Containers can Communicate

          1. Get what your network looks like:

          $ docker network ls

          2. Create a new network:

          $ docker network create -d my-network

          3. Start the first container: 

          $ docker run -d -p 5080:5080 --name "container_one" --network="my-network" <image1:v0.1>

          4. Check out network settings for the first container :

          $ docker inspect first_container

          “Networks”: should have ‘my-network’

          5. Start the second container: 

          $ docker run -d -p 5090:5090 --name "container_two" --network="my-network" <image2:v0.1>

          6. Check out network settings for the second container:

          $ docker inspect second_container

          “Networks”: should have ‘my-network’

          7. SSH into your second container: 

          $ docker exec -it container_two sh

          8. Inside of the second container, run the following command:

          apt-get update && apt-get install -y iputils-ping

          9. Inside of the second container, you can ping the first container by:

          ping container_one

          Also, your code calls such as http://localhost:5080 can be replaced by http://container_one:5080.

          Conclusion

          This guide showed how to install Docker on Ubuntu 22.04 and start using Docker to pull images and start a new Postgres container.

          Also Read: Kubernetes Architecture for PostgreSQL


          Spread the love

          Be the first to comment

          Leave a Reply

          Your email address will not be published.


          *