How can I use a local image as the base image with a dockerfile?
Categories:
Using a Local Image as the Base Image in a Dockerfile

Learn how to leverage local Docker images as base images in your Dockerfiles, enabling offline builds, faster development cycles, and custom environments.
Dockerfiles are essential for defining how your applications are built and run within Docker containers. Typically, you specify a base image from a public registry like Docker Hub (e.g., FROM ubuntu:latest
). However, there are scenarios where you might want to use a local Docker image as your base. This approach is particularly useful for offline development, creating highly customized base images, or distributing internal base images without relying on external registries. This article will guide you through the process of using a local image as a base image in your Dockerfile.
Why Use a Local Base Image?
Using a local image as a base offers several advantages:
- Offline Development: Build new images even without an internet connection, provided the base image is already present locally.
- Faster Builds: Avoid network latency associated with pulling images from remote registries, leading to quicker build times.
- Customized Environments: Create a highly tailored base image with specific tools, configurations, or security hardening, then use it across multiple projects.
- Security and Compliance: Maintain tighter control over your base images, ensuring they meet internal security standards before being used in application builds.
- Reduced Registry Dependencies: Minimize reliance on external registries, which can be beneficial in air-gapped environments or for internal distribution.
flowchart TD A[Start] --> B{Local Image Exists?} B -- No --> C[Build/Pull Local Image] C --> D[Tag Local Image] B -- Yes --> D D --> E[Create Dockerfile] E --> F["FROM local-image:tag"] F --> G[Add Application Layers] G --> H[Build New Image] H --> I[End]
Workflow for using a local image as a Dockerfile base
Prerequisites: Having a Local Image
Before you can use a local image as a base, you need to have that image available in your local Docker daemon's image store. There are a few ways to achieve this:
- Pull from a Registry: Even if you intend to use it locally, you might initially pull it from a public or private registry.
- Build from a Dockerfile: You can build a custom base image from its own Dockerfile.
- Load from a Tar Archive: If someone provides you with a
.tar
file of a Docker image, you can load it.
For this guide, let's assume you've either pulled an image or built one locally. The key is that it appears when you run docker images
.
docker pull ubuntu:22.04
docker images
Pulling an image and verifying its presence locally
Step-by-Step: Using a Local Image in Your Dockerfile
Once your desired base image is available locally, the process is straightforward. The Docker daemon automatically checks its local image cache before attempting to pull from a remote registry.
1. Step 1: Verify Local Image
Ensure the image you want to use as a base is present in your local Docker image list. You can check this by running docker images
. Note down its repository and tag (e.g., my-custom-base:1.0
).
2. Step 2: Create Your Dockerfile
Create a new Dockerfile
in your project directory. In this Dockerfile, specify your local image as the base using the FROM
instruction. The Docker daemon will first look for this image locally.
3. Step 3: Add Application Layers
Continue adding your application-specific layers (e.g., RUN
, COPY
, CMD
, EXPOSE
) as you normally would. These layers will be built on top of your local base image.
4. Step 4: Build Your New Image
Build your new Docker image using the docker build
command. Docker will automatically use the local base image if it finds a match for the specified FROM
instruction.
# Dockerfile
FROM ubuntu:22.04
LABEL maintainer="Your Name <your.email@example.com>"
RUN apt-get update && apt-get install -y \
nginx \
curl \
--no-install-recommends && \
rm -rf /var/lib/apt/lists/*
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Example Dockerfile using a local ubuntu:22.04
image as base
docker build -t my-web-app:latest .
docker run -p 8080:80 my-web-app:latest
Building and running the new image
FROM
instruction, it first checks its local image cache. If the image (repository and tag) is found locally, it uses that. Only if it's not found locally will it attempt to pull from configured registries.Considerations and Best Practices
While using local base images is powerful, keep these points in mind:
- Image Tagging: Always use specific tags (e.g.,
my-custom-base:1.0
) instead oflatest
for your local base images to ensure reproducibility. - Updates: Local images don't automatically update. If your custom base image needs updates (e.g., security patches), you'll need to rebuild or re-pull it and then rebuild any images that depend on it.
- Distribution: If you need to share your custom local base image with others, you'll eventually need to push it to a private registry or distribute it as a
.tar
file. - Layer Caching: Docker's build cache works efficiently with local base images, speeding up subsequent builds if the base image hasn't changed.
FROM
instructions with images that are only available locally and not pushed to any registry. If another developer tries to build your Dockerfile without that specific local image, their build will fail, as Docker won't be able to find it remotely.