7.1 Base Image
Base images play an important role in building Docker images since they provide the starting point for the build and define the environment where your app will run. Choosing and configuring the right base images can significantly affect the performance, size, and security of the final image. In this lecture, we'll go over how to choose and set up base images for Docker.
A base image (or foundation image) is a Docker image that is used as the initial layer for creating a new Docker image. It can include a minimal operating system, necessary tools and libraries, or even be empty (scratch image). The choice of a base image determines which resources and packages will be available in your container by default.
Choosing a Base Image
When selecting a base image, several factors should be considered:
- Image Size: Smaller images download faster, take up less disk space, and can speed up application deployment.
- Support and Updates: Using official images with regular updates provides a higher level of security, vulnerability fixes, and operational stability.
- Compatibility: The base image should include the necessary libraries and dependencies your app needs to run properly.
Popular Base Images
1. Alpine Linux
A very lightweight image, taking up about 5 MB. It's perfect for apps that require a minimal operating system. However, developers should note that Alpine lacks many standard libraries, so some apps might need extra configuration.
FROM alpine:3.12
2. Debian/Ubuntu
More full-featured images that include a wide array of tools and libraries. They are suitable for complex applications that need a richer environment or a familiar standard Linux library set.
FROM ubuntu:20.04
3. Official Language Images
Official images for programming languages such as Node.js, Python, Ruby, and others. These images already include the necessary environment for developing and running apps in the respective language, making setup easier.
FROM node:14
7.2 Setting Up the Base Image
After picking a base image, you've got to set it up to meet your app's needs. This setup includes installing necessary packages, copying app files, and configuring the environment.
Example: Setting Up a Base Image Based on Alpine Linux
# Using a base image Alpine Linux
FROM alpine:3.12
# Installing necessary packages
RUN apk add --no-cache python3 py3-pip
# Setting up the working directory
WORKDIR /app
# Copying app files
COPY . .
# Installing dependencies
RUN pip3 install -r requirements.txt
# Specifying the command to run the app
CMD ["python3", "app.py"]
In this example, the alpine:3.12 base image is used, necessary packages are installed, app files are copied, and dependencies are installed. The final command alpine:3.12
specifies how to run the app.
Example: Setting Up a Base Image Based on Ubuntu
# Using a base image Ubuntu
FROM ubuntu:20.04
# Updating and installing necessary packages
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
# Setting up the working directory
WORKDIR /app
# Copying app files
COPY . .
# Installing dependencies
RUN pip3 install -r requirements.txt
# Specifying the command to run the app
CMD ["python3", "app.py"]
This Dockerfile uses the ubuntu:20.04 image and installs Python and pip. Then it copies app files and installs dependencies, just like in the previous example.
7.3 Best Practices
How to choose and configure base images properly:
1. Choose minimal images: start with the smallest images that meet your app's requirements. This will reduce the final image size and improve security.
2. Avoid installing unnecessary packages: only install dependencies that are actually required by your app. This will reduce the image size and minimize potential vulnerabilities.
3. Clean up package manager caches: after installing packages, delete temporary files and caches to make the image lighter.
RUN apt-get update && apt-get install -y python3 python3-pip && rm -rf /var/lib/apt/lists/*
4. Use multi-stage builds: they let you separate build and runtime stages, helping to create more lightweight and secure images.
# Build stage
FROM node:14 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Final stage
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
5. Regularly update images: use up-to-date base images to include the latest security fixes and updates.
GO TO FULL VERSION