Docker

How do I Run “apt-get install” in Docker

The Docker platform is a simpler and easier solution for building, deploying, and delivering complex projects and applications. It uses containers to pack the source program, necessary packages, and configuration settings. The application dependencies are usually installed in containers using “Dockerfile” instructions.

More specifically, Dockerfile is referred to as an instruction file that generates an image to contain an application or project. It uses the “apt-get install” command in the “RUN” instruction to install the essential dependencies of the containerized project.

This blog will guide you on how to run “apt-get install” in Docker.

How to Run “apt-get install” in Docker?

The “apt-get install” is a Linux command line utility used in the Ubuntu Linux distribution. It utilizes the Advanced packaging tool (APT) to install the project dependencies. The APT is one of the huge package repositories that have numerous packages and libraries.

To use the “apt-get install” in Dockerfile for essential dependencies installation, go through the below-given syntax:

RUN apt-get update && apt-get install -y \ \ && \
apt-get clean && \ rm -rf /var/lib/apt/lists/*

For the demonstration, check out the mentioned procedure.

Step 1: Create Dockerfile
First, make a new file named “Dockerfile” and copy the provided instructions in the code block into the file. These instructions will containerize and execute the Python program:

FROM ubuntu
RUN apt-get update && apt-get install -y --no-install-recommends \
  && apt-get install -y python3 \
  && rm -rf /var/lib/apt/lists/*
CMD python3 -c "print('Docker is more simple Deployment Tool')"

Here:

  • FROM” instruction is utilized to define the base image for the container. For instance, we have specified “ubuntu” as a base image.
  • RUN” instruction runs the mentioned commands. In our scenario, we are installing the required package, such as “python3” using the “apt-get install” command to run the Python program.
  • CMD” defines the executables or defaults for the container. Here, we will execute the Python program:

Step 2: Generate the Docker Image
Next, create a Docker image by utilizing the “build” command of Docker. The “-t” option allocates the name to Docker image:

> docker build -t ubuntu:latest .

Step 3: Run Docker Image
Next, execute the Docker image using “docker run” to build and fire up the container:

> docker run ubuntu:latest

From the output, you can see that we have successfully deployed the Python program in Docker:

This is all about how to use “apt-get install” to install packages in Docker.

Conclusion

The “apt-get install” is a Linux command line utility used in the Ubuntu Linux distribution. To use the “apt-get install” command in Docker, first, create a Dockerfile and specify the “RUN” instruction along with the “apt-get install” command to install packages and other dependencies. This write-up has demonstrated how to run the “apt-get install” in Docker.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.