- Create an image from Scratch
- Create an image from a Dockerfile
- Create an image from a Container
Building Docker images FROM scratch means starting from an empty base, without any preinstalled OS, ensuring lightweight, secure, and fully controlled containerization.
- Minimal size
- Enhanced security
- Full control over dependencies
FROM scratch
COPY hello /
CMD ["/hello"]
A Dockerfile is a script that defines step by step how to build a Docker image in an automated, efficient, and reproducible way. It acts as a recipe, ensuring consistency across deployments.
The essential command to create a Docker image:
docker build -t image_name path
- Define the base image
FROM ubuntu:20.04
- Set up the environment
LABEL maintainer="Jrad Mariem"
ENV APP_ENV=production
WORKDIR /app
- Define the startup behaviour
- The CMD command is the default command that will be executed but if an argument is entered when running the container it would ignore the CMD Statement
- The Entypoint is a base command which prioritary comparing it with CMD .
Note
ENTRYPOINT
vs. --entrypoint
in Docker
ENTRYPOINT
(Dockerfile) sets a fixed command that runs by default.RunningENTRYPOINT ["ping", "-c", "4"]
docker run my-image google.com
executes:ping -c 4 google.com
.--entrypoint
(CLI) temporarily overridesENTRYPOINT
.Runsdocker run --entrypoint ls my-image
ls
instead ofping
.- Use
ENTRYPOINT
for default behavior,--entrypoint
for runtime overrides.
ENTRYPOINT ["echo"]
- Run commands during build which creates a temporary container and executes the command and from it an image will be created
RUN apt-get update && apt-get install -y python3
Use docker commit
to save a container’s current state as a new image.
docker commit [container_id] [image_name:tag]