Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Docker Installation on Ubuntu and Essential Commands Guide

Tech May 8 4

Installing Docker on Ubuntu

Updating apt package index

sudo apt-get update

Installing dependencies for HTTPS-based repositories

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Adding Docker's official GPG key

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

Configuring Docker stable repository

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

Refreshing package indexes

sudo apt-get update

Installing Docker Engine (Community Edition)

sudo apt-get install docker-ce

Verifying the installation

sudo docker run hello-world

Configuring sudo-less access

After installation, you can avoid using sudo before every Docker command by adding your user to the docker group. You must log out and log back in for changes to take effect:

sudo usermod -a -G docker $USER

Managing Docker Service

Docker daemon starts automatically after installation. Use these commands for manual control:

# Start Docker service
sudo service docker start

# Stop Docker service
sudo service docker stop

# Restart Docker service
sudo service docker restart

Working with Docker Images

Understanding Docker images

A Docker image consists of layered filesystem chenges stacked on top of a base filesystem. The bottom layer contains the boot filesystem (bootfs), similar to standard Linux/Unix boot systems. Docker automatically unmounts bootfs once a container starts, freeing up memory.

Images package applications and their dependencies into a single executable file. This file serves as a template for creating containers. Multiple container instances can run from a single image simultaneously.

Images are binary files that can inherit from other images and add custom configurations. For instance, you could take an Ubuntu base image and layer Apache server installation on top of it.

Images are portable across machines. You can transfer an image file to another system and it will functon identically. Using pre-built images from registries is more efficient than building from scratch. When customization is needed, extend an existing image rather than starting from nothing.

Images are stored in registries for sharing. Docker Hub (https://hub.docker.com/) is the primary public registry, though private registries are common in enterprise environments.

Listing local images

docker image ls

Output columns include:

  • REPOSITORY: Name of the repository
  • TAG: Version or variant identifier
  • IMAGE ID: Unique identifier for the image
  • CREATED: Creation timestamp
  • SIZE: Disk space used

Docker uses tags to differentiate multiple versions within the same repository. Tags like latest, 18.04, or stable label specific sets of image layers. The same repository can contain numerous tagged versions.

When running containers, you can specify a particular tagged version using the format repository:tag. For example:

docker run --name web_server -it ubuntu:18.04 /bin/bash

This command starts a container from the ubuntu:18.04 image specifically.

Pulling images from registry

Docker maintains image registries containing both public and private repositories. Docker Hub is the largest public registry. Private registries are commonly used for internal corporate images.

Download an image to your local system using the pull command:

docker image pull library/hello-world

In this syntax, library/hello-world represents the image location where library is the namespace and hello-world is the image name. Official images in Docker Hub use library as their default namespace, so you can simplify:

docker image pull hello-world

Removing images

docker image rm image_name_or_id

Example:

docker image rm hello-world

Managing Docker Containers

Creating containers

docker run [options] image_name [command]

Key options:

Option Description
-i Interactive mode, keeping STDIN open
-t Allocates a pseudo-terminal
--name Assigns a custom name to the container
-v Mounts host directory to container path (host:container)
-d Runs container in detached (background) mode
-p Maps host port to container port (host:container)
-e Sets environment variables
--network=host Shares host's network namespace with container

Interactive containers

Create a foreground container with terminal access:

docker run -it --name=myubuntu ubuntu /bin/bash

This launches an interactive Ubuntu environment. Exiting the shell terminates the container.

Detached containers

Create a background container that continues running after you exit:

docker run -dit --name=myubuntu2 ubuntu

Accessing running containers

Connect to an existing container's shell:

docker exec -it container_name_or_id command

For example:

docker exec -it myubuntu2 /bin/bash

Listing containers

# Show running containers
docker container ls

# Show all containers including stopped ones
docker container ls --all

Container lifecycle management

# Stop a running container
docker container stop container_name_or_id

# Start a stopped container
docker container start container_name_or_id

# Forcefully terminate a container
docker container kill container_name_or_id

Removing containers

docker container rm container_name_or_id

Converting Containers to Images

Transform a container into a reusable image:

docker commit container_name image_name

This captures the current container state as a new image layer.

Image Backup and Transfer

Exporting images to files

Save a image to a tar archive for transfer or backup:

docker save -o output_file.tar image_name

Example:

docker save -o ./ubuntu_backup.tar ubuntu

Importing images from files

Load a saved image into local registry:

docker load -i image_file.tar
Tags: dockerubuntu

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.