Step-by-Step Guide to Install Ubuntu on Docker for Lightweight, Efficient Development Environments
Forget bulky VMs—discover how leveraging Ubuntu within Docker containers slashes resource usage while preserving a native Linux feel, transforming how you build and test software.
If you're a developer looking for a nimble, consistent, and isolated Linux environment without the heavy overhead of traditional virtual machines, running Ubuntu on Docker is a game changer. This approach lets you spin up clean Ubuntu instances almost instantly, use minimal resources, and keep your host system clutter-free. In this practical guide, I’ll walk you through installing Ubuntu on Docker step-by-step, so you can accelerate your development and testing workflows today.
Why Ubuntu on Docker?
- Lightweight & Fast: Docker containers share your system’s kernel, meaning they use far fewer resources than VMs.
- Consistent Environments: Ship the same Ubuntu setup across different machines, eliminating the “it works on my machine” problem.
- Easy Cleanup: Remove containers easily once projects are done––no residual clutter.
- Native Linux Experience: Since Ubuntu is a full Linux distribution, you get the familiar bash shell, apt package manager, and all the tools you love.
Prerequisites
Before getting started, ensure you have the following:
- A machine with Docker installed (Docker Desktop on Windows/Mac or Docker Engine on Linux).
- Basic familiarity with command-line interfaces.
- Internet connection to pull Ubuntu images.
If you haven't installed Docker yet, check Docker's official install guide.
Step 1: Check Docker Installation
Open your terminal (Command Prompt, PowerShell, or Linux shell) and run:
docker --version
You should see something like:
Docker version 20.10.7, build f0df350
If this command fails, Docker isn’t correctly installed—go back to the Docker install guide.
Step 2: Search for Ubuntu Docker Image
Docker Hub hosts official Ubuntu images maintained by Canonical. To find available images:
docker search ubuntu
You’ll see a list, with the official “ubuntu” image at the top.
Step 3: Pull the Latest Ubuntu Image
To download the latest Ubuntu image (as of writing, Ubuntu 22.04 Jammy Jellyfish is the default latest):
docker pull ubuntu
Docker will download the layers and store the image locally, like this:
latest: Pulling from library/ubuntu
...
Digest: sha256:...
Status: Downloaded newer image for ubuntu:latest
Step 4: Run Ubuntu Container Interactively
Launch an Ubuntu container in interactive mode with a bash shell:
docker run -it ubuntu bash
You should now see a prompt that looks like:
root@<container-id>:/#
This means you are inside the Ubuntu container shell, isolated from your host system.
Step 5: Update Package Lists inside the Container
Since Docker images are minimal, update the apt
package lists:
apt update
You can now install any packages you want using apt install
, for example:
apt install -y build-essential git curl
Step 6: Try Some Commands
Verify the Ubuntu version inside the container:
lsb_release -a
Output might be:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy
Run any Linux commands you typically use during development. For example:
git --version
gcc --version
Step 7: Exiting the Container
When you’re finished, exit the container shell:
exit
This stops the container. If you want to keep a container running, use the -d
flag or manage container lifecycle differently.
Step 8: Managing Your Ubuntu Containers
List Running Containers
docker ps
List All Containers (running or stopped)
docker ps -a
Restart a Container
docker start -ai <container_id_or_name>
Remove a Container
docker rm <container_id_or_name>
Bonus: Create a Persistent Development Container
Sometimes you want Ubuntu with your projects and tools already set up—here’s how you can create a simple Dockerfile to customize your Ubuntu image.
Create a file named Dockerfile
:
FROM ubuntu:latest
RUN apt update && apt install -y \
build-essential \
git \
curl \
vim
CMD ["bash"]
Build your customized image:
docker build -t my-dev-ubuntu .
Run it:
docker run -it my-dev-ubuntu
Now you have a reusable Ubuntu environment tailored to your needs.
Conclusion
Running Ubuntu inside Docker containers lets you effortlessly deploy lightweight, consistent Linux environments ideal for development and testing. This approach eliminates VM bloat and boots up in seconds, letting you focus on what matters: building great software. Try it yourself—once you experience the speed and convenience, switching back to heavier virtual machines will feel like a step backward!
Have questions or want to share your Docker Ubuntu workflows? Drop a comment below or reach out on Twitter!
Happy coding! 🚀