Ubuntu
Docker
Container
Version Check
Linux Commands

Verify the version of ubuntu running in a Docker container

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Knowing the exact Ubuntu version inside a Docker container matters for package compatibility, security patching, and debugging. Docker images are often tagged with version labels like ubuntu:22.04, but the actual OS version inside the container can differ from the tag if the image was built from a custom base. There are several commands to check the version — from inside a running container, from outside using docker exec, or by inspecting the image without running it.

Method 1: cat /etc/os-release

The most reliable method. This file exists on all modern Ubuntu images and contains structured version data:

bash
1# From inside the container
2cat /etc/os-release
3
4# From outside (running container)
5docker exec my-container cat /etc/os-release

Output:

 
1PRETTY_NAME="Ubuntu 22.04.3 LTS"
2NAME="Ubuntu"
3VERSION_ID="22.04"
4VERSION="22.04.3 LTS (Jammy Jellyfish)"
5VERSION_CODENAME=jammy
6ID=ubuntu
7ID_LIKE=debian
8HOME_URL="https://www.ubuntu.com/"
9SUPPORT_URL="https://help.ubuntu.com/"
10BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
11PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
12UBUNTU_CODENAME=jammy

The key fields are VERSION_ID (e.g., 22.04) and VERSION_CODENAME (e.g., jammy).

Method 2: lsb_release -a

The lsb_release command gives a clean summary, but it is not installed in minimal Docker images:

bash
docker exec my-container lsb_release -a

Output:

 
1Distributor ID: Ubuntu
2Description:    Ubuntu 22.04.3 LTS
3Release:        22.04
4Codename:       jammy

If the command is not found, install it:

bash
docker exec my-container bash -c "apt-get update && apt-get install -y lsb-release && lsb_release -a"

For minimal images, cat /etc/os-release is preferred since it requires no extra packages.

Method 3: cat /etc/lsb-release

A simpler file that exists on most Ubuntu images:

bash
docker exec my-container cat /etc/lsb-release

Output:

 
1DISTRIB_ID=Ubuntu
2DISTRIB_RELEASE=22.04
3DISTRIB_CODENAME=jammy
4DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"

Method 4: /etc/issue

A short one-liner that shows the version:

bash
docker exec my-container cat /etc/issue

Output:

 
Ubuntu 22.04.3 LTS \n \l

The \n and \l are escape codes for hostname and terminal line — they appear literally in Docker since there is no login session.

Method 5: Without Running the Container

You can inspect the image metadata or run a throwaway container:

bash
1# Run a temporary container just to check the version
2docker run --rm ubuntu:latest cat /etc/os-release
3
4# Inspect image labels (if the image was built with version labels)
5docker inspect ubuntu:22.04 | grep -i version
6
7# Check the image tag you pulled
8docker images ubuntu
 
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       22.04     6b7dfa7e8fdb   2 weeks ago   77.8MB
ubuntu       latest    6b7dfa7e8fdb   2 weeks ago   77.8MB

Note that ubuntu:latest may point to the same image as a specific version tag.

Method 6: From a Dockerfile

During a build, you can verify the base image version:

dockerfile
1FROM ubuntu:22.04
2
3# Print version during build (appears in build output)
4RUN cat /etc/os-release
5
6# Or store it as an environment variable
7RUN . /etc/os-release && echo "Building on Ubuntu $VERSION_ID"

Scripting Version Checks

Extract specific fields for use in scripts or CI pipelines:

bash
1# Get just the version number
2docker exec my-container grep VERSION_ID /etc/os-release | cut -d'"' -f2
3# Output: 22.04
4
5# Get the codename
6docker exec my-container grep VERSION_CODENAME /etc/os-release | cut -d= -f2
7# Output: jammy
8
9# Use in a shell variable
10UBUNTU_VERSION=$(docker exec my-container grep VERSION_ID /etc/os-release | cut -d'"' -f2)
11echo "Container is running Ubuntu $UBUNTU_VERSION"

Ubuntu Version Reference

VersionCodenameLTSEnd of Support
24.04Noble NumbatYesApril 2034
22.04Jammy JellyfishYesApril 2032
20.04Focal FossaYesApril 2030
18.04Bionic BeaverYesApril 2028 (ESM)

LTS releases receive five years of standard support and up to ten years with Extended Security Maintenance (ESM).

Common Pitfalls

  • Trusting the image tag: docker pull ubuntu:latest does not always mean the latest LTS. The latest tag is set by the image maintainer and may not update immediately. Always verify inside the container.
  • Missing lsb_release in minimal images: Docker Ubuntu images are stripped down. Use /etc/os-release instead of installing lsb-release just to check the version.
  • Confusing host and container versions: cat /etc/os-release on the host shows the host OS, not the container. Always use docker exec to run the command inside the container.
  • Assuming all Ubuntu-based images are Ubuntu: Images built FROM ubuntu:22.04 may install packages that change /etc/issue or add custom /etc/os-release entries. Check the Dockerfile or image history with docker history <image>.
  • Alpine or Debian images mistaken for Ubuntu: Some Docker images use Debian or Alpine as the base. /etc/os-release will show the actual distribution. Check ID=ubuntu to confirm it is actually Ubuntu.

Summary

  • /etc/os-release is the most reliable method — works on all Ubuntu Docker images without extra packages
  • lsb_release -a gives a clean summary but requires the lsb-release package
  • Use docker exec <container> cat /etc/os-release to check from outside the container
  • Use docker run --rm <image> cat /etc/os-release to check an image without starting a persistent container
  • Do not trust image tags blindly — always verify the actual version inside the container
  • Extract specific fields with grep and cut for scripting and CI pipelines

Course illustration
Course illustration

All Rights Reserved.