How to Install a Package in Docker
Install software in Docker by adding the package installation command to a Dockerfile, then building and running your own image. This makes the package part of the image, so it is present every time a new container starts. You can also install a package interactively in a running container for testing, but those changes are not a reliable deployment method.
How Packages Work in Docker Images
A Docker image is a reusable template. A container is a running instance created from that template.
When you install a package inside a running container, the package exists only in that container’s writable layer. If you remove the container and start a new one from the original image, the package is gone.
For anything that must remain installed in newly started or deployed containers, put the installation in a Dockerfile and build a custom image rather than altering a live container.
The command you use depends on the Linux distribution used by the base image:
| Base Image Family | Package Manager | Typical Use |
|---|---|---|
| Ubuntu or Debian | apt-get |
Most general-purpose Linux images |
| Alpine | apk |
Small images commonly used for services |
| Fedora, RHEL, or Rocky Linux | dnf |
Red Hat-based environments |
| Distroless | None | Build dependencies in another stage or choose a different base image |
Before building, make sure Docker is running:
docker version
If Docker is not installed yet, install Docker Engine using Docker’s official Ubuntu installation instructions or install Docker Desktop for your operating system.
How to Install a Package in a Docker Image
Use the base image’s package manager in a Dockerfile, then build and test a custom image with the installed package.
Step 1: Choose the Right Base Image and Package Manager
Start with the image your application already uses when possible. Changing from Ubuntu to Alpine, for example, may reduce image size, but it can also change package names, available libraries, shell behavior, and compatibility with compiled dependencies.
For a Debian or Ubuntu image, use apt-get. For Alpine, use apk. Avoid assuming that every image includes a package manager: minimal and distroless images often do not.
A clean-container reproduction is the first check when the package name is uncertain, because it separates package-manager problems from application problems.
Step 2: Create a Dockerfile
Create a file named Dockerfile in your project directory. This example starts with Ubuntu and installs curl plus CA certificates:
FROM ubuntu:24.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
curl \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
CMD ["bash"]
This does several useful things:
apt-get updatedownloads the current package index.apt-get install -yinstalls packages without asking interactive questions.--no-install-recommendsavoids optional packages that may not be needed.- Removing
/var/lib/apt/lists/*removes the downloaded package index from the finished image.
Keep apt-get update and apt-get install in the same RUN instruction. Docker caches image layers, and separating them can cause a later build to use an outdated package index.
Install packages before a USER instruction that switches to a non-root application user. Package managers normally need root privileges during the image build, while the final application should usually run without them.
Do not use sudo in a Dockerfile. Most base images do not include it, and Dockerfile build instructions run as root unless the image changes users.
Step 3: Build the Custom Image
From the directory containing the Dockerfile, build an image and give it a meaningful tag:
docker build --tag my-app:with-curl .
The final period means “use the current directory as the build context.” Docker sends files in that directory to the build process, so add a .dockerignore file if your project contains large folders, secrets, local virtual environments, or build artifacts.
For example, a basic .dockerignore file might contain:
.git
node_modules
.venv
.env
__pycache__
Step 4: Run and Test the Package
Run the package directly in a temporary container to confirm it is present:
docker run --rm my-app:with-curl curl --version
If the command prints the installed version, the package is available in the image. The --rm flag removes the test container after it exits; it does not remove your image.
To open an interactive shell in the custom image:
docker run --rm -it my-app:with-curl bash
If the base image does not include Bash, use sh instead.
Install Packages in Alpine, Debian, and Red Hat Images
The Dockerfile pattern stays the same, but the package manager changes.
Alpine Images
Alpine uses apk. Its small size makes it popular for containers, but package names and library compatibility can differ from Ubuntu or Debian.
FROM alpine:latest
RUN apk add --no-cache curl ca-certificates
CMD ["sh"]
The --no-cache option prevents Alpine’s package index from being retained in the image.
Build and test the image:
docker build --tag my-app:alpine-curl .
docker run --rm my-app:alpine-curl curl --version
Fedora and Red Hat-Based Images
Fedora, Rocky Linux, and similar images commonly use dnf.
FROM fedora:latest
RUN dnf install -y curl ca-certificates \
&& dnf clean all
CMD ["bash"]
Use the package manager provided by the image rather than trying to install another package manager inside it.
Installing Python 3.7 in an Ubuntu Container
If the Ubuntu base image and configured package repositories provide Python 3.7, run this inside the Ubuntu container shell:
apt-get -y install python3.7
Check the package index before using the installation command.
After installation, verify the executable:
python3.7 --version
For production Python services, a purpose-built Python base image is often simpler than manually installing Python, especially when your application requires a particular interpreter version.
Use a Custom Image With Docker Compose
Docker Compose can build your Dockerfile before starting the service. This is the right approach when a package must be available before deployment.
Given the Dockerfile that installs curl, create or update compose.yaml:
services:
app:
build:
context: .
dockerfile: Dockerfile
image: my-app:with-curl
command: ["sh", "-c", "sleep infinity"]
Build the image and start the service:
docker compose up --build --detach
Confirm the package exists in the running Compose service:
docker compose exec app curl --version
Stop and remove the test service when finished:
docker compose down
For a real application, replace the sleep infinity command with your application’s normal startup command. Keep the package installation in the Dockerfile, not in the Compose command field. A Compose command runs when the container starts; it should not be responsible for reconstructing the software environment every time.
Install a Package Temporarily in a Running Container
Interactive installation is useful for diagnosing an issue, checking a package name, or exploring an unfamiliar image. It is not a substitute for updating the Dockerfile.
Start an Ubuntu container with an interactive shell:
docker run --rm -it ubuntu:24.04 bash
Inside the container, update the package list:
apt-get update
Install a package such as curl:
apt-get install -y curl
Verify that it works:
curl --version
Exit the container:
exit
Because this example uses --rm, Docker removes the container when you exit. The installed package disappears with it. That is useful for experiments because it leaves no forgotten containers behind.
You can also enter an existing running container if it has a shell available:
docker exec -it <container-name> sh
Replace <container-name> with the actual container name. Installations made this way are still temporary from a deployment perspective: recreating the container from its original image removes the changes.
Docker can save a modified container as an image with docker commit, but this is generally a poor long-term workflow. The resulting change is difficult to review, reproduce, version, and rebuild in CI. Put the working commands in a Dockerfile instead.
Add Local Files, Scripts, and Binaries
Installing a package from a Linux repository is different from adding a file you already have. Use COPY in a Dockerfile to add application files, scripts, configuration, or a compatible binary.
For example, to copy a script into the image and make it executable:
FROM ubuntu:24.04
COPY ./mytool /usr/local/bin/mytool
RUN chmod +x /usr/local/bin/mytool
CMD ["/usr/local/bin/mytool"]
Build the image:
docker build --tag mytool:latest .
Run it:
docker run --rm mytool:latest
Use COPY for files that belong in the image. Use a bind mount instead when you want a container to access a local development file without rebuilding the image.
Do not copy credentials, private keys, .env files, or production secrets into an image. Images are commonly stored in registries and shared with build systems.
Verify Package Installation and Fix Common Problems
For packages that provide a command-line executable, run that executable to verify it is available in the image.
Use the package’s version or help output as a basic check:
docker run --rm my-app:with-curl curl --version
If the command fails, use the error to narrow down the issue.
apt-get: not foundorapk: not found: The base image uses a different Linux distribution, has no package manager, or is a minimal image.- Package not found: Run the package manager’s update command, confirm the package name, and check whether that package version exists for your base image.
- Permission denied during installation: Install packages before switching to a non-root
USERin the Dockerfile. - The image still has an older package version: Docker may have reused a cached build layer. Rebuild without the cache when you specifically need fresh package metadata.
docker build --no-cache --tag my-app:with-curl .
- The image becomes unexpectedly large: Install only the packages needed at runtime, clean package metadata, and consider a multi-stage build for compilers and build tools.
- A package works in an interactive container but not after deployment: Add the tested installation command to the Dockerfile, rebuild the image, and redeploy the new image tag.
Frequently Asked Questions
Can I install software directly in a running Docker container?
Yes, but it is best for testing and troubleshooting. Packages installed with an interactive shell or docker exec exist only in that container’s writable layer, so they disappear when the container is removed and recreated. Use a Dockerfile when the software must be present in future containers.
How do I install Python 3.7 in an Ubuntu Docker container?
Inside an Ubuntu container shell that has a repository offering that version, install it with apt-get -y install python3.7. Then verify the installed executable with python3.7 --version. The available Python version depends on the Ubuntu image and package repositories used by the container.
Can I install any EXE file in Docker?
Not in a typical Linux container. A Windows .exe requires a compatible Windows environment, so it generally belongs in a Windows container with a matching Windows container base image. Linux containers normally run Linux binaries, packages, scripts, and libraries.
Should I use docker commit after installing a package manually?
Usually no. docker commit can capture a changed container, but it does not document the installation steps in source control or make them easy to reproduce in automated builds. A Dockerfile is the maintainable way to create a custom image.
How do I install Docker itself rather than a package inside Docker?
Install Docker on the host machine first, then use it to build and run images. On Windows, Docker Desktop provides the usual setup flow; Docker’s Windows installation documentation includes the supported installer options.
Can I use Ubuntu as a basic Docker image for installing software?
Yes. Start the Dockerfile with Ubuntu, refresh its package index, and use apt-get to install software while building the image. The documented Ubuntu procedure, for example, adds Python 3.7 through apt-get.
How should I install packages before deploying a Docker Compose service?
Put package-installation commands in the Dockerfile that creates the image you will deploy. Give the Compose service a build configuration so Docker builds that image before it starts the container.
