Today, we’ll dive into how to run Ubuntu as a Docker container, combining the flexibility of Ubuntu with the efficiency of Docker.
Docker has revolutionized how we deploy and manage applications, making it possible to run lightweight, portable containers on any system.
This guide will teach the fast and easy way to run Ubuntu as a Docker container, perfect for beginners and seasoned tech enthusiasts. Check out my video tutorial below for those who prefer a visual walkthrough.
Why Run Ubuntu in a Docker Container?
Running Ubuntu as a Docker container offers several advantages:
- Isolation: Each container runs independently, ensuring that your applications run in a secure environment.
- Portability: Docker containers can run on any system that supports Docker, making it easy to share and deploy applications.
- Efficiency: Containers require fewer resources than traditional virtual machines, as they share the host system’s kernel.
requirements
Before we start, ensure you have Docker installed on your system. If you still need to install Docker, refer to my previous posts on how to install Docker on Ubuntu, MacOS, or Windows.
Step-by-Step Guide to Run Ubuntu as a Docker Container
Step 1: Pull the Ubuntu Image from Docker Hub
First, we need to pull the official Ubuntu image from Docker Hub. Open your terminal and run:
docker pull ubuntu:latest
This command downloads the latest Ubuntu image to your local machine. If you need a specific version of Ubuntu, you can tag it like ubuntu:20.04.
Step 2: Run the Ubuntu Container
With the Ubuntu image ready, it’s time to run it as a container. Execute the following command:
docker run -itd --name=ubuntu ubuntu:latest
Here’s what this command does:
- Docker run tells Docker to create and start a new container.
- -itd allows you to interact with the container through your terminal and detach
- ubuntu:latest specifies the image and version to use for the container.
You’re now inside the Ubuntu container, running in your terminal. Feel free to explore and install any packages you need.
Step 3: Managing the Container
Here are some basic commands to manage your Ubuntu container:
- List Containers: docker ps -a shows all containers, including inactive ones.
- Start a Container: docker start <container-id> to start a specific container.
- Attach to a Container: docker attach <container-id> to access the terminal of a running container.
- Stop a Container: docker stop <container-id> to gracefully stop a container.
Step 4: Customizing Your Ubuntu Container
To customize your Ubuntu container, consider creating a Dockerfile. This allows you to build an image with your configurations, software, and files. Here’s a simple example:
FROM Ubuntu
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile creates an Ubuntu-based image with Nginx installed. To build this image, run docker build -t my-ubuntu. In the directory containing your Dockerfile.
Best Practices
- Keep Images Small: Only install the necessary packages to keep your images lightweight.
- Use Tags: Specify tags when pulling images to ensure consistency across environments.
- Security: Regularly update your images to include the latest security patches.

Conclusion
Running Ubuntu as a Docker container is straightforward and offers significant advantages regarding isolation, portability, and efficiency. You’ll be up and running by following the steps outlined in this guide in no time. Remember to explore further customizations and optimizations to suit your specific needs.
For more insights into Docker and containerization, check out my posts on application monitoring with Docker, deploying Kubernetes, and setting up a monitoring stack with Prometheus and Grafana.
Happy containerizing!
