Installing Linux Packages Inside a Docker Container

Docker containers provide an isolated environment for running applications. After installing Docker on your Linux machine, you can create custom images by starting with a base OS image and adding packages. This article demonstrates how to install packages inside a Docker container using Ubuntu as the base image.

We'll install three essential packages: vim editor, Firefox, and Python 3.7. There are two approaches to accomplish this − using CLI commands step-by-step or creating a Dockerfile for automated builds.

Method 1 − Step-by-Step CLI Approach

Running the Base Ubuntu Container

First, pull and run the Ubuntu image with an interactive bash session −

sudo docker run -it ubuntu bash

This command downloads the Ubuntu image (if not present locally) and opens an interactive terminal inside the container.

Installing Packages Inside Container

Inside the container's bash session, execute these commands to install the required packages −

apt-get -y update
apt-get -y install vim
apt-get -y install firefox
apt-get -y install software-properties-common
add-apt-repository ppa:deadsnakes/ppa
apt-get -y update
apt-get -y install python3.7

These commands update the package list, install vim and Firefox, add the Python repository, and install Python 3.7. You can verify the Python installation −

python3.7 --version
exit

Committing Changes to New Image

After exiting the container, find the container ID and commit the changes −

sudo docker ps -l
sudo docker commit <CONTAINER_ID> my-ubuntu-custom

Verify the new image was created −

sudo docker images

Method 2 − Using Dockerfile

Create a file named Dockerfile with the following content −

# Create ubuntu as base image
FROM ubuntu

# Update package list and install packages
RUN apt-get -y update && \
    apt-get -y install vim firefox software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt-get -y update && \
    apt-get -y install python3.7 && \
    apt-get clean

Building and Running the Image

Build the image using the Dockerfile −

sudo docker build -t my-ubuntu-custom .

Run the newly created image −

sudo docker run -it my-ubuntu-custom bash

Comparison of Methods

Aspect CLI Method Dockerfile Method
Reproducibility Manual steps, prone to errors Automated, consistent builds
Version Control Difficult to track changes Easy to version and share
Image Size Larger due to intermediate layers Optimized with layer consolidation
Debugging Interactive, good for testing Requires rebuilding for changes

Best Practices

  • Use apt-get clean to remove package cache and reduce image size

  • Combine multiple RUN commands with && to minimize layers

  • Use specific version tags instead of latest for production images

  • Remove unnecessary packages and dependencies to keep images lean

Conclusion

While the CLI method is useful for experimentation and debugging, the Dockerfile approach is recommended for production environments. Dockerfiles provide better reproducibility, version control, and automated builds, making them essential for maintaining consistent development and deployment workflows.

Updated on: 2026-03-17T09:01:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements