Open In App

How can I Make my own Base Image for Docker?

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Developing a custom base image for Docker is a significant opportunity to make your development and deployment workflows more efficient. A base image is a foundation for building with DockisPrimary Terminologies

  • Docker: Docker is an open-source platform that works with the medium of delivering software as packages called containers, used with OS-level virtualization, containers are lightweight, standalone, executable software packages that encompass everything an application's code or runtime, libraries, or dependencies to run the application.
  • Container: A lightweight, stand-alone, executable software package that includes everything needed to run an application—code, runtime, system tools, libraries, and settings. They isolate containers from one another and the host system so that they are consistent across all infrastructures.
  • Image: A read-only template used in creating containers. In this template, there is application code, runtime, libraries, environment variables, and configuration files. Images are built as layered and incremental changes, one layer at a time from the previous layer. Images can be stored in container registries, for example, Docker Hub.
  • Dockerfile: A Dockerfile is a text document that contains the necessary commands for creating an image in Docker. The Dockerfile consists of instructions on a base image, application code, dependencies, configuration settings, and commands to run within the container.
  • Docker Hub: Docker Hub is a cloud-based registry service that enables users to store and distribute Docker images, it provides both public and private repositories, allowing sharing of the photos with other users or downloading images managed by Docker and other companies.
  • Layer: Each instruction in a Dockerfile creates a layer in the image, which is stacked and forms the final image.
  • FROM: A Dockerfile instruction is used to specify the base image.

Step-by-Step Process to Make Own Base Image for Docker

Step 1: Launch EC2 Instance

  • Login to AWS Console and navigate to EC2 Dashboard
  • Select a Base Operating System like Linux, ubuntu etc.
Launch EC2 Instance
  • Launch EC2 instance
Screenshot-2024-06-22-133347-(1)

Step 2: Install docker

  • Install docker in our local machine by using following commands
sudo yum -y install docker
Install docker

Step 3: Start Docker Daemon

Start and enable docker by using following commands

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Start Docker Daemon

Step 4: Create a Dockerfile

  • Create a new directory for your Docker project and inside it, create a file named Dockerfile. This file will contain the instructions to build your base image.
# Use the official Alpine Linux image as the base
FROM alpine:latest
# Set the maintainer label
LABEL maintainer="[email protected]"
# Install common dependencies
RUN apk update && apk add --no-cache bash curl
# Set environment variables
ENV MY_ENV_VAR=my_value
# Copy necessary files
COPY my_script.sh /usr/local/bin/
# Make the script executable
RUN chmod +x /usr/local/bin/my_script.sh
# Define the default command to run when the container starts
CMD ["bash"]
Create a Dockerfile

Step 5: Create a Bash Script file

Assuming my_script.sh contains the following content:

#!/bin/bash
echo "Hello, We are from GeeksforGeeks. A warm Welcome for writers...!"
Create a Bash Script file

Step 6: Build the Docker Image

  • Open your terminal, navigate to the directory containing the Dockerfile, and build the image using the following command:
docker build -t my-base-image .
Build the Docker Image

Step 7: Verify the Image

  • Once the build process is complete, you can verify the image by running a container from it:
docker run -it my-base-image
  • You should see a bash prompt, indicating that your base image is functional and ready to be used as a foundation for other images.
Verify the Image
  • Figure shows that our custom base image and script are working perfectly. The script output confirms that the file was copied correctly and is executable.

Conclusion

Creating your Docker base image in a controlled way is the best way to ensure consistency, efficiency, and reliability between your development and deployment environments. Customize the base image by adding only the necessary dependencies and configurations to make building, testing, and deploying your applications more accessible.

In this tutorial, we have walked through basic terminologies step by step in the process of creating a custom base image and also demonstrated with practical examples, we saw in this guide everything from selecting a minimal base operating system and constructing a Dockerfile with necessary instructions to building the image and verifying its functionality.

Understanding and applying these principles put you in a position to optimize your Docker workflow. Not only will custom base images save time by avoiding repeating setup tasks, but they will also provide a consistent environment for your applications to run, thereby lowering the risks associated with problems emerging from differences in environments, this ensures that the application runs seamlessly across different development and deployment stages.

With more experience using Docker, you'll make your base images better and better by including more best practices and optimizations. Keep your base images small and secure; only include what is necessary. Keep them updated with the newest security patches.


Next Article
Article Tags :

Similar Reads