Skip to content

Ubuntu - Docker

Ubuntu - Introduction to Docker Containers

Section titled “Ubuntu - Introduction to Docker Containers”

Docker is a powerful platform for developing, shipping, and running applications in containers. Containers allow you to package an application with all of its dependencies (libraries, system tools, code, runtime) into a standardized unit for software development. This ensures your application runs consistently across different environments.

The official website for Docker is https://www.docker.com/

While Docker is available in Ubuntu’s default repositories (docker.io package), it’s often recommended to install Docker Engine from Docker’s official APT repository for the latest version. However, for simplicity in this introductory tutorial, we’ll use the docker.io package. For production or advanced use, refer to the official Docker documentation for installing docker-ce.

Step 1.1 − Update your package list:

sudo apt update

Step 1.2 − Install the docker.io package:

sudo apt install -y docker.io

The -y flag automatically confirms the installation.

Step 1.3 − Verify Docker is running:

sudo systemctl status docker

It should show ‘active (running)’. If not, start and enable it: sudo systemctl start docker and sudo systemctl enable docker.

2. Running Docker Commands without sudo (Best Practice)

Section titled “2. Running Docker Commands without sudo (Best Practice)”

By default, Docker commands require root privileges (using sudo). To run Docker commands as a non-root user, add your user to the docker group (this group is created during Docker installation).

Step 2.1 − Add your user to the docker group:

sudo usermod -aG docker ${USER}

${USER} is an environment variable that automatically resolves to your current username.

Step 2.2 − Apply the group changes. You’ll need to either log out and log back in, or run the following command in your current terminal session:

newgrp docker

After this, you should be able to run Docker commands without sudo.

3. Working with Docker Images and Containers

Section titled “3. Working with Docker Images and Containers”

Let’s pull a simple image (e.g., hello-world) and run it as a container to verify your Docker setup.

Step 3.1 − Pull the hello-world image from Docker Hub (the default public registry):

docker pull hello-world

Docker will download the image layers. You’ll see output indicating the download progress.

Step 3.2 − List downloaded images:

docker images

You should see hello-world in the list, along with its tag, image ID, creation time, and size.

Step 3.3 − Run the hello-world image as a container:

docker run hello-world

This command creates a new container from the hello-world image, runs it, and then the container exits. The output should display a message from Docker confirming that your installation appears to be working correctly.

You can run other operating systems as containers. Let’s try CentOS.

Step 4.1 − Pull the latest CentOS image:

docker pull centos:latest

Step 4.2 − Run an interactive terminal session inside a new CentOS container:

docker run -it centos /bin/bash
  • -it makes the session interactive (-i) and allocates a pseudo-TTY (-t).
  • centos is the image name.
  • /bin/bash is the command to run inside the container (starts a Bash shell).

Your terminal prompt will change, indicating you are now inside the CentOS container (e.g., [root@container_id /]#). You can run CentOS commands here (e.g., cat /etc/os-release). Type exit to leave the container and return to your Ubuntu shell.

Docker enables powerful workflows for application isolation, reproducible environments, microservices, and more. Key concepts include:

  • Images: Read-only templates used to create containers (e.g., ubuntu, nginx, python).
  • Containers: Runnable instances of images. They are isolated environments.
  • Docker Hub: A public registry for storing and sharing Docker images.
  • Dockerfile: A text file containing instructions to build a custom Docker image.

This is just a brief introduction. The world of Docker is vast and full of possibilities for modern software development and deployment.