Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 cleanto remove package cache and reduce image sizeCombine multiple RUN commands with
&&to minimize layersUse specific version tags instead of
latestfor production imagesRemove 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.
