Creating a Dockerfile is a declarative way to define the steps for building a docker image. A Dockerfile is a simple text-based script that contains the instructions for building a docker image, making it a reproducible and shareable artifact.
How to Create Docker Images?
Create application directory:
Begin by creating a dedicated directory for the application. This Directory will contain both the app and Dockerfile.
Create an empty Docker file:
In this directory, create an empty Dockerfile using a text/code editor.
sudo nano Dockerfile
Add instruction to the Dockerfile:
Define the steps for building the Docker image in the Dockerfile. For example:
#Set parent image FROM ubuntu:22.04 as build #Set working directory in the container WORKDIR /app #Copy the local scripts and file to the container COPY lyrics.sh . # Install any needed packages RUN apt-get update && apt-get install -y \ curl && \ apt-get clean #Make scripts executable and run the script RUN chmod +x lyrics.sh && ./lyrics.sh
Build and Tag Docker image:
Build the Docker image in the same directory (“.”) as the Dockerfile. Tagging the image helps organize it, especially if you plan to push it to a Docker repository. Use meaningful names or version numbers as tags.
docker build -t imagename:youtag . docker tag imagename:youtag imagename:image1
Log into docker account and push image to docker repository
Log into docker account:
docker login
Push image to a docker repo. This will make it accessible to others:
docker push tagname