How To Install and Use Docker on Ubuntu 22.04

Administration 9 min read

Docker install on Ubuntu

 

Docker has become a ubiquitous tool for developers and system administrators, allowing for the creation and deployment of applications in isolated containers. This article guides you through the process of installing and using Docker on Ubuntu 22.04 server, providing a comprehensive understanding for both beginners and experienced users.

 

 


Basic information about Docker

 

Docker [install docker ubuntu] utilizes containerization technology to package applications with their dependencies into standardized units. These containers share the underlying operating system kernel but run in isolation, ensuring consistent behavior regardless of the host environment. This approach offers numerous benefits, including:

 

    Portability: Docker containers can seamlessly run on any system with Docker installed, promoting application portability across different environments.
    Isolation: Containers share no resources beyond what is explicitly defined, enhancing security and preventing conflicts between applications.
    Reproducibility: Docker containers guarantee a consistent environment, simplifying development workflows and deployment processes.
    Efficiency: Containers utilize the host system's resources efficiently, making them lightweight and ideal for microservices architectures.

 

 

 

Prerequisites

 

Before diving into the installation, ensure you meet the following prerequisites:

    Ubuntu 22.04 (Jammy Jellyfish): Verify your Ubuntu version by running lsb_release -a in your terminal. If using a different version, refer to the official Docker documentation for specific instructions.
    Administrative Privileges: The installation process requires administrative access (sudo). Use a user account with sudo privileges to proceed.

 

 

 

Installing Docker Engine

 

There are two primary methods to install Docker on Ubuntu 22.04:

 

Method 1: Using Official Docker Repositories

 

    Update Package Lists: Begin by ensuring your package lists are up-to-date:


        sudo apt update

 

    Add Docker GPG Key: Import the Docker GPG key to verify the authenticity of packages during installation:


        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

    

    Add Docker Repository: Add the official Docker repository for your Ubuntu version to your system's sources list:


        sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu jammy stable"

    

    Update Package Lists (Again): Refresh the package lists to reflect the newly added repository:


        sudo apt update

    
    

    Install Docker Engine, Containerd, and Docker Compose (Optional): Install the core Docker engine components along with containerd (runtime environment) and optionally Docker Compose (for managing multi-container applications):


        sudo apt install docker-ce docker-ce-cli containerd docker-compose -y

 

 

 

Method 2: Downloading DEB Packages (Alternative)


Download DEB Packages: Visit the official Docker download page (https://docs.docker.com/get-docker/) and download the appropriate DEB packages for your architecture (e.g., amd64).

Install Downloaded Packages: Use dpkg to install the downloaded DEB packages:


    sudo dpkg -i docker-desktop-<version>.deb libcontainerd-<version>.deb containerd.io-<version>.deb

 

Install Dependencies (if necessary): Resolve any dependency issues that may arise during installation:


    sudo apt install -f

 

 


Verifying Docker Installation

 

Once the installation is complete, verify if Docker is running correctly:


    sudo systemctl status docker

 

A successful output should display the Docker service as active (running).

 

 

 

Getting Started with Docker


The basic syntax for working with Docker looks like this:

 

     docker [option] [command] [arguments]
    

To see a list of all possible commands, you just need to type the docker command in the console without parameters and you should see the following list:

     Output
          attach      Attach local standard input, output, and error streams to a running container
          build       Build an image from a Dockerfile
          commit      Create a new image from a container's changes
          cp          Copy files/folders between a container and the local filesystem
          create      Create a new container
          diff        Inspect changes to files or directories on a container's filesystem
          events      Get real time events from the server
          exec        Run a command in a running container
          export      Export a container's filesystem as a tar archive
          history     Show the history of an image
          images      List images
          import      Import the contents from a tarball to create a filesystem image
          info        Display system-wide information
          inspect     Return low-level information on Docker objects
          kill        Kill one or more running containers
          load        Load an image from a tar archive or STDIN
          login       Log in to a Docker registry
          logout      Log out from a Docker registry
          logs        Fetch the logs of a container
          pause       Pause all processes within one or more containers
          port        List port mappings or a specific mapping for the container
          ps          List containers
          pull        Pull an image or a repository from a registry
          push        Push an image or a repository to a registry
          rename      Rename a container
          restart     Restart one or more containers
          rm          Remove one or more containers
          rmi         Remove one or more images
          run         Run a command in a new container
          save        Save one or more images to a tar archive (streamed to STDOUT by default)
          search      Search the Docker Hub for images
          start       Start one or more stopped containers
          stats       Display a live stream of container(s) resource usage statistics
          stop        Stop one or more running containers
          tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
          top         Display the running processes of a container
          unpause     Unpause all processes within one or more containers
          update      Update configuration of one or more containers
          version     Show the Docker version information
          wait        Block until one or more containers stop, then print their exit codes

 


So now that Docker is installed, let's look at some basic commands:

 

1. Hello World! Run your first Docker container:


    docker run hello-world

 

This command pulls and executes the official "hello-world" image, printing a simple message to the console.

 


2. Listing Images: View available Docker images on your system:


    docker images


3. Pulling Images: Download a new Docker image from the Docker Hub registry:


    docker pull ubuntu:latest

This pulls the latest version of the official Ubuntu image.

 


4. Running Containers: Launch a container based on the downloaded image:


    docker run -it ubuntu:latest bash

 

The -it flag provides an interactive terminal within the container. Type exit to exit the container.

 


5. Viewing Running Containers (continued):


    docker ps

 

This displays information about running containers, including container ID, image name, status, ports, and creation time.

 


6. Stopping and Removing Containers:

    Stop a container:

        docker stop <container_id>

 

Remove a stopped container:

        docker rm <container_id>

 

 

7. Managing Container Logs:


    docker logs <container_id>

This displays the logs generated by the container.

 


8. Working with Volumes:

Volumes enable persistent data storage within containers. Data written to volumes persists even after the container is stopped or removed. Here's an example:


    docker run -v my-data-volume:/data ubuntu:latest bash

 

This creates a volume named "my-data-volume" and mounts it to the "/data" directory inside the container. Any changes made to the "/data" directory persist within the volume.

 

9. Docker Hub and Sharing Images:

Docker Hub is a public registry for sharing Docker images. You can explore and download pre-built images for various applications and functionalities. Additionally, you can create and push your own Docker images to the Hub for sharing with others.

 

 

 


Docker for Developers and System Administrators

 

Docker offers significant benefits for developers and system administrators:

 

For Developers:

    Rapid Development Environment Setup: Quickly set up consistent development environments with all dependencies pre-packaged in Docker images.
    Simplified Testing and Debugging: Isolate applications and their dependencies for easier testing and debugging.
    Microservices Architecture Support: Docker excels at deploying and managing microservices-based applications.

 

For System Administrators:

    Standardized Deployments: Ensure consistent application deployments across different environments.
    Resource Efficiency: Docker containers utilize resources efficiently, making them ideal for high-density deployments.
    Scalability: Easily scale applications by deploying additional containers.
    Simplified Application Management: Manage and update applications by working with their respective Docker images and containers.

 

 

 


Beyond the Basics: Advanced Docker Concepts

This article provides a foundational understanding of Docker on Ubuntu 22.04. As you delve deeper, consider exploring these advanced topics:

    Docker Compose: Manage multi-container applications with ease using Docker Compose.
    Docker Networks: Create and manage custom networks for container communication.
    Docker Volumes in Depth: Explore advanced volume management techniques like named volumes and Docker volumes plugins.
    Docker Security: Implement best practices for securing your Docker environment, including user permissions, network policies, and image scanning.
    Building Docker Images: Craft custom Docker images to package your applications and their dependencies for streamlined deployment.

 

By mastering these advanced concepts, you can leverage Docker's full potential for efficient application development, deployment, and management on your Ubuntu system.

 

 

 

Conclusion

Docker revolutionized application development and deployment by promoting containerization. In this article, we have tried to give you the basic Docker knowledge that will allow you to install Docker and run your first container. But as you gain experience, explore Docker's vast ecosystem of tools and resources to improve your development and deployment workflows.

2024-05-23 08:14

VISA
MasterCard
Bitcoin
PayPal
Ethereum
Bitcoin Cash
Litecoin
Dogecoin
Tether
Monero
Privat24