How to Install Linux Packages Inside a Docker Container?
Once you understand how to pull base Docker Images from the Docker registry, you can now simply pull OS distributions such as Ubuntu, CentOS, etc directly from the Docker hub. However, the OS Image that you have pulled simply contains a raw file system without any packages installed inside it. When you work on projects inside Docker Containers, you will definitely need some libraries and packages either right from the beginning of the development phase or on the go.
In this article, we will see how to install packages and libraries inside Docker Containers using 2 different methods. We will also try to install Firefox and vim text editor inside Ubuntu Docker Container.
Method 1: Using Command Line Interface
Step 1: Open the terminal of your local system and run the Ubuntu Docker Image from the Docker Registry. If your system has no previous pulls, it will start pulling from the registry.
sudo docker run -it ubuntu bash
Step 2: Now, you have opened the bash of your Ubuntu Docker Container. To install any packages, you first need to update the OS.
apt-get -y update
Step 3: After you have updated the Docker Container, you can now install the Firefox and Vim packages inside it.
apt-get -y install firefox apt-get -y install vim
You can now easily use these packages through the bash itself.
Method 2: Using Dockerfile
You can also directly specify the packages you need to install in the dockerfile using the RUN instruction. This method is preferable over the CLI method because building a dockerfile is very essential if you are working on projects inside Docker. It gives better version control and provides a blueprint of the entire Docker Image.
Step 1: Write your instructions inside a dockerfile.
FROM ubuntu:latest RUN apt-get -y update && apt-get -y install firefox && apt-get -y install vim
Step 2: Build the Image using the Docker Build command.
sudo docker build -t sample-image .
Step 3: Run the container and verify the installed packages
sudo docker run -it sample-image bash vim firefox