Docker 101: How to Run Your First Container on Ubuntu

Docker 101: How to Run Your First Container on Ubuntu

Docker is an open-source platform for building, shipping, and running containerized applications. Containers package an application together with everything it needs to run – code, libraries, and system tools – into a standardized unit that can run reliably in any environment. This makes deployment faster and more consistent: as AWS notes, Docker lets you “ship code faster, standardize application operations, [and] seamlessly move code” to different machines. Official docs also remind us that Docker Engine runs on 64-bit Ubuntu (22.04 LTS “Jammy” or newer), so the steps below apply to Ubuntu 22.04 and later. In this guide, we’ll install Docker on Ubuntu 22.04+, run a test container, and explain each step along the way.

Installing Docker on Ubuntu

To install Docker on Ubuntu, we add Docker’s official repository and install the Docker Engine packages. The commands below come from the Docker documentation. First, update the package index and install some prerequisite tools (ca-certificates and curl). Then create a directory for Docker’s GPG key, download and add the official key, and set up the Docker APT repository. Run these commands in order:

sudo apt-get update
sudo apt-get install -y ca-certificates curl

# Create keyrings directory (for signed-by)
sudo install -m 0755 -d /etc/apt/keyrings

# Download Docker’s official GPG key into /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
    -o /etc/apt/keyrings/docker.asc

# Ensure the key file is readable
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the Docker APT repository for your Ubuntu version
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
  https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$UBUNTU_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update the package index again to include Docker’s packages
sudo apt-get update
  • sudo apt-get update refreshes Ubuntu’s package index so we install the newest versions.
  • sudo apt-get install -y ca-certificates curl installs tools needed to fetch Docker’s GPG key.
  • sudo install -m 0755 -d /etc/apt/keyrings makes a directory for storing signed GPG keys.
  • sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc downloads Docker’s official GPG key and saves it.
  • sudo chmod a+r /etc/apt/keyrings/docker.asc makes the key readable by apt.
  • The echo "deb …" line adds Docker’s repository URL to Ubuntu’s APT sources, using the correct Ubuntu codename (e.g. jammy for 22.04). The signed-by option tells APT to trust the Docker key we added.
  • Finally, sudo apt-get update is run again to load packages from the new Docker repo.

Next, install Docker Engine (the docker-ce package) along with the CLI and related tools. Run:

sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

This command installs Docker Engine (docker-ce), the Docker command-line tools (docker-ce-cli), the container runtime (containerd.io), and useful plugins (docker-buildx and docker-compose). After installation, the Docker daemon (dockerd) is started automatically.

Verifying the Installation

After Docker is installed, verify it’s running by checking the Docker service and running the test image:

sudo systemctl status docker

This should show that the Docker service is active (running). You may need to press q to exit the status output.

Then, run the built-in hello-world test container. This will download a tiny image and run it:

sudo docker run hello-world

If everything is set up correctly, you should see output like:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

According to Docker’s documentation, running docker run hello-world “downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits”. In our example above, Docker printed Hello from Docker! confirming that it pulled the hello-world image and ran it successfully.

Tip: On Ubuntu, Docker commands typically require sudo unless your user is in the docker group. If you see a permission denied error, you may either continue using sudo or add your user to the docker group (e.g. sudo usermod -aG docker $USER). Docker’s docs note that the docker group is created empty, which is why root is needed initially. After adding yourself to the group, log out and back in so the change takes effect.

Running Your First Container

Congratulations – you’ve installed Docker and confirmed it works. Now let’s pull and run our first container. The hello-world container is the simplest example. When you ran docker run hello-world, Docker pulled the image from Docker Hub (if needed) and executed it. You don’t need to pull it explicitly beforehand, but you can if you like:

sudo docker pull hello-world

This fetches the image from Docker Hub and stores it locally. You can see all downloaded images with:

docker images

To run the container again, use:

docker run hello-world

Each docker run command starts a container, runs it, and then stops it. The hello-world container simply prints its message and exits, as we saw.

To illustrate running other containers, you can try the busybox image. For example:

docker run busybox echo "hello from BusyBox"

This command fetches the tiny BusyBox image and runs the echo command inside it. You should see:

hello from BusyBox

This shows Docker running a shell command in an isolated container and returning the output. Try exploring further: use docker run ubuntu to start an interactive Ubuntu shell, or check out popular images like nginx or mysql on Docker Hub.

Summary and Next Steps

In this tutorial, you installed Docker on Ubuntu 22.04+, pulled the hello-world image, and ran it as a container. You learned how to update apt, add Docker’s repository and GPG key, install Docker Engine, and verify the installation. Running docker run hello-world confirmed that Docker is working correctly.

As a next step, you might:

  • Run more containers (e.g. busyboxubuntu) and practice commands like docker psdocker images, and docker rm to list and remove containers/images. For instance, docker run busybox echo "hello from BusyBox" prints hello from BusyBox inside a BusyBox container.
  • Explore Docker Hub (hub.docker.com) to find useful images.
  • Learn to build your own image using a Dockerfile, so you can containerize your own application.
  • Consider installing Docker Compose (included above) to manage multi-container applications.

Docker opens up a world of isolated, reproducible environments for your apps. From here, you can dive into Docker’s documentation and try out more advanced tutorials.

Posts Carousel

Leave a Comment

Your email address will not be published. Required fields are marked with *

Latest Posts

Most Commented

Featured Videos