Docker 101: Create Custom image (UBUNTU)

PART VI

ยท

2 min read

Docker 101: Create Custom image (UBUNTU)

CREATE OWN IMAGE

EVERY IMAGE HAS BASE IMAGE (TO PROVIDE A base image provides the starting point for your container. It includes the basic operating system, system libraries, and essential files required for your application to run.)

CREATING DOCKERFILE AND IMAGE

Step 0: Decide the Content of the Working Directory

  1. Start with a Clean Directory: Begin with a clean directory that only contains the files and directories required for your application. Remove any temporary, unnecessary, or sensitive files.

  2. Use .dockerignore: Create a .dockerignore file in the same directory as your Dockerfile. This file is similar to a .gitignore file and allows you to specify patterns for files and directories that should be excluded from the build context. For example:

     # .dockerignore
     .git
     .vscode
     node_modules
    
  3. Organize Your Directory Structure: Organize your working directory logically. Place your application code, configuration files, and dependencies in appropriate subdirectories. This helps keep the directory clean and organized.

  4. Isolate Dockerfile: If possible, place your Dockerfile in a location where it has access to only the necessary files. For example, if your Dockerfile only needs access to the "app" directory, place it inside that directory or in a location that has access to just that directory.

By following these steps, you can ensure that only the essential files are sent to the Docker daemon when building your image. This optimization reduces build times and keeps your Docker image lightweight and efficient.

STEP 1: CREATE DOCKER FILE

touch Dockerfile

STEP 2: ADD DETAILS IN DOCKER FILE: below command is to open vim editor

vi Dockerfile

STEP 3: EXAMPLE FILE CONTENT

FROM ubuntu
CMD ["echo", "Hello my name is Dev"]

STEP 4: LOGIN INTO DOCKER HUB BEFORE BUILDING

docker login

STEP 5: SEE CONTENT OF DOCKERFILE

cat Dockerfile

STEP 6: BUILDING A DOCKER IMAGE IN THE CURRENT DIRECTORY

docker build -t my_image .

ย