Docker - a beginner friendly guide

'But it works on my machine' - that's one of the most important problem docker solves, lets get to know what docker really is, how it works and to get started with docker

#DevOps#Docker
26 Jul 2025
Docker - a beginner friendly guide

What exactly is docker

docker is a simple tool or a piece of software that helps you package your application and all of its dependencies (runtime, dependent modules, config, environment etc) into one single box called a container.

best part is this container can run on any machine with any operating system as long as it has the docker engine installed, and it works the same on all machines.

can't say "but it works on my device" anymore.

The problem it solves

Before docker, running any software on different machine was pain in the back.

lets take a nodejs project you just made:

it works fine on your windows device.

but when you try to deploy it on a linux machine, you might face the following issues:

the classic it works on my machine issue.

in the production environment with multiple developers it gets even worse.

How docker fixes it

Docker lets you pack your whole app including the code and all types of dependencies it require into one single unit (container).

this container:

How it works

Docker uses client-server architecture. The Docker client talks to the docker daemon which does all the hard task of building, running and distributing the containers.

The docker client and server communicate through REST API, over UNIX sockets or a network interface.

docker-architecture

docker-architecture

Docker daemon

the docker daemon dockerd lilistens stens for the docker API and manages all the stuff such as containers, images, networks and volumes. One daemon can also talk to other daemons.

Docker images

a docker image is a read-only template with required instructions to create a docker container. it can be thought of a recipe. Most of the time an image is based on another image, with some customization. for example you can create an image based on node:alpine and install some node dependencies on it as well as other config details to make your application run.

Docker conatiner

a docker container is a runnable instance of an image. you can create, start, stop, delete, move a container using the docker API or the docker CLI. you can connect a container to one or more networks, attack storage.

Docker registries

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker looks for images on Docker Hub by default. You can even run your own private registry.


enough terminologies but how does it actually works?

so docker conatiner is kind of a virtual machine but not with the full potential.

It shares the host machine's kernel, but it's isolated using linux's features like namespaces and cgroups.

Docker-compose

Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience.

Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single YAML configuration file. Then, with a single command, you create and start all the services from your configuration file.

How to use Docker

lets start with a pre-built app in a container:

install the docker engine first, obv.

lets start by running redis:

docker run -d --name redis-server -p 6379:6379 redis

now lets dockerize our own app:

lets take one nextjs project i want to dockerize - crux, fun fact crux is totally self hosted using docker, for better speed (almost 2x faster than vercel, lets talk about it some other time).

Dockerfile:

Dockerfile is just a set of instructions that tells docker to create the image of you requirements.

Dockerfile is created in the root folder of the app with the exact namespace Dockerfile, when you run docker build command docker takes the Dockerfile from the root of the project and creates an image based on the instructions you provided in the Dockerfile.

lets make a very basic Dockerfile according to my requirements.

FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npx prisma generate RUN npm run build CMD npm start

lets build the image

docker build -t crux .

the -t stands for tag means we are tagging the image as crux. (afaik)

check the image using:

docker images

this will list all the images on the device.

docker images

docker images

now you can use this image anywhere you want.

there is a issue with this image - it is very big.

this can be solved with multi-stage build. (reference)

the final optimized Dockerfile will look like:

FROM node:18-alpine AS deps WORKDIR /app RUN apk add --no-cache libc6-compat COPY package*.json ./ RUN npm install FROM node:18-alpine AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . RUN npx prisma generate RUN npm run build FROM node:18-alpine AS runner WORKDIR /app ENV NODE_ENV production RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs COPY --from=builder /app/public ./public RUN mkdir .next RUN chown nextjs:nodejs .next COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static USER nextjs EXPOSE 3000 ENV PORT 3000 ENV HOSTNAME "0.0.0.0" CMD ["node", "server.js"]

this multi-stage method will decrease the size of the image to around 20% of the original. in this case it went from ~2GB to 200MB.

Conclusion

this was a very basic overview of the docker for beginners, docker is much more than this. You should definately read the official docs to understand things more clearly.

Thanks for reading!

If you read this far, get a job you no-lifer (love you).

These are all my personal opinions and beliefs. If you find something wrong — please don’t tell me. Let me live in my own bubble.

Until next time.