Docker Commands for Beginners

Docker Commands for Beginners

Tanish Bhandari··5 min read

Docker lets you package an app with everything it needs and run it the same way on any machine. This post covers the commands you'll actually use in your first weeks, in the order you'll need them.

[!note] Prerequisite Install Docker Engine (Linux) or Docker Desktop (Mac/Windows), then check it works with docker --version.

The mental model

Four words matter. Everything else is detail.

  • Dockerfile: a text recipe describing how to build an image.
  • Image: a read-only template (your app + its dependencies).
  • Container: a running instance of an image.
  • Registry: a place to store and share images (Docker Hub, GHCR).

How Docker pieces fit together

1. Your first container

bash
docker run hello-world

Docker looks for the image locally, pulls it from Docker Hub if missing, creates a container and runs it.

A more useful example is a web server:

bash
docker run -d --name web -p 8080:80 nginx
FlagMeaning
-ddetached: run in the background
--name webgive the container a friendly name
-p 8080:80map host port 8080 to container port 80

Open http://localhost:8080 and you'll see the nginx welcome page.

2. Looking around

bash
docker ps            # running containers
docker ps -a         # all containers, including stopped ones
docker images        # images stored locally
docker logs web      # output of a container
docker logs -f web   # follow the logs live
docker stats         # live CPU / memory usage

3. Container lifecycle

A container moves between a few states, and each has a command.

Container lifecycle

bash
docker stop web      # graceful stop
docker start web     # start it again
docker restart web
docker pause web     # freeze processes
docker unpause web
docker rm web        # delete a stopped container
docker rm -f web     # force delete, even if running

4. Getting inside a container

bash
docker exec -it web sh        # open a shell in a running container
docker exec web nginx -t      # run a single command
docker run -it --rm ubuntu bash   # throwaway container; removed on exit

[!tip] Use --rm --rm deletes the container as soon as it exits, which keeps docker ps -a from filling up with junk.

5. Building your own image

Create a Dockerfile next to your app:

dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Then build and run it:

bash
docker build -t my-app:1.0 .
docker run -d --name my-app -p 3000:3000 my-app:1.0

Copying package*.json and installing before copying the rest means Docker caches the dependency layer, so rebuilds are much faster.

6. Ports, volumes and networks

Containers are isolated by default. These three flags connect them to the world.

Ports, volumes and networks

bash
# Volume: data that survives container deletion
docker volume create pgdata
docker run -d --name db \
  -e POSTGRES_PASSWORD=secret \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16

# Network: containers on the same network reach each other by name
docker network create app-net
docker run -d --name api --network app-net my-app:1.0
docker run -d --name db2 --network app-net -e POSTGRES_PASSWORD=secret postgres:16
# inside "api", the database is reachable at host "db2"

7. Sharing images

bash
docker login
docker tag my-app:1.0 yourname/my-app:1.0
docker push yourname/my-app:1.0
docker pull yourname/my-app:1.0

8. Cleaning up

Docker keeps everything until you tell it not to.

bash
docker image rm my-app:1.0     # remove an image
docker container prune         # remove all stopped containers
docker image prune             # remove dangling images
docker system df               # see what is using disk space
docker system prune -a         # remove everything unused (careful!)

[!warning] Prune with care docker system prune -a removes all unused images and stopped containers. Volumes are kept unless you add --volumes, and that flag really does delete your data.

9. Docker Compose in one minute

Once you run more than one container, write them down in a compose.yaml:

yaml
services:
  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
bash
docker compose up -d       # start everything
docker compose ps
docker compose logs -f web
docker compose down        # stop and remove (add -v to delete volumes)

Cheat sheet

GoalCommand
Run a containerdocker run -d --name X -p H:C image
List runningdocker ps
See logsdocker logs -f X
Shell insidedocker exec -it X sh
Stop / startdocker stop X / docker start X
Build imagedocker build -t name:tag .
Delete containerdocker rm -f X
Clean updocker system prune

That's enough to run real projects. Next steps worth learning: multi-stage builds, health checks, and running containers behind a reverse proxy like Traefik.


T

Tanish Bhandari

DevOps Engineer · FieldCISO