How to write Dockerfile for installing packages
Install git, gradle and openjdk 11
Using Ubuntu as base image¶
FROM ubuntu:latest
RUN apt update && apt install -y --no-install-recommends
git
wget
openjdk-11-jdk
gradle
&& rm -rf /var/lib/apt/lists/*
Using Centos as base image¶
FROM centos:latest
ENV PATH=$PATH:/opt/gradle/gradle-7.0.2/bin
RUN yum update -y && yum install -y
git
wget
unzip
java-11-openjdk-devel
&& yum clean all
RUN mkdir /opt/gradle
&& wget -q https://services.gradle.org/distributions/gradle-7.0.2-bin.zip
&& unzip gradle-7.0.2-bin.zip -d /opt/gradle/
&& rm -f gradle-7.0.2-bin.zip
Using Alpine as base image¶
FROM alpine:latest
ENV PATH=$PATH:/opt/gradle/gradle-7.0.2/bin
RUN apk add --no-cache
git
openjdk11
RUN mkdir /opt/gradle
&& wget -q https://services.gradle.org/distributions/gradle-7.0.2-bin.zip
&& unzip gradle-7.0.2-bin.zip -d /opt/gradle/
&& rm -f gradle-7.0.2-bin.zip
Important Tips¶
Tip
Clean Up: Always clean up package manager caches (e.g., rm -rf /var/lib/apt/lists/*, yum clean all) in the same RUN instruction to keep the image size small.
Note
No Cache: For Alpine Linux, use apk add --no-cache. This installs the package and cleans up the cache in a single step, eliminating the need for a separate rm command.
๐ง Quick Quiz โ Package Management¶
#
Which command is used to install packages in an Ubuntu-based Docker image?
#
Why do we use && to chain commands in a Dockerfile?
#
What is the package manager for Alpine Linux?
๐ฌ DevopsPilot Weekly โ Learn DevOps, Cloud & Gen AI the simple way.
๐ Subscribe here