Docker quick note

6/17/2025, 7:49:54 PM

This is a quicknote for docker for when i need a handy reference for everyday commands. It covers the essential Docker commands, explains how to build and run containers, with some useful tips,These notes were taken from an online course so it's junior-level.

beginner commands:

docker pull node --> pulls node image from web

docker ps -a --> lists all containers on system

docker run -it node --> open an interactive terminal session type 'exit' or press 'ctrl + d' to exit


Run a node container

  1. create file named "Dockerfile" in your project directory then write these:
FROM node

WORKDIR /app

COPY . /app

RUN npm install

EXPOSE 80

CMD [ "node" , "server.js" ]

*optimized code:

FROM node

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

EXPOSE 80

CMD [ "node" , "server.js" ]
  1. then open terminal and run below command (make sure you are in the project directory and have Dckerfile there): docker build .

  2. then copy the image id (i.e. sha256:1c7...)
    *we use tag name later

  3. then run this code : docker run -p 3000:80 sha256:1c7...

  4. then open the browser and go to this address: localhost:3000


To stop the container you can run below code (preferably in another terminal):

  1. docker ps --> to show active containers, so you can copy the name

  2. docker stop name_of_container

*one way to update the source code is to rebuild image


image: a package full of code and tools which can be used to start apps based on it

  • an image might contain multiple apps

  • docker logs -f container_name show the inputs in the project. (-f to tail results actively)

docker run -p 8000:80 img-name --> for network communications
docker start -a container_name --> for direct terminal interaction with container

  • -p maps a port on host to port in the container, allowing external access to services
  • -a attaches your terminal to the running container, allowing to see its output and interact with it

docker run -i image_name --> interactive mode for when program requires user input
docker start -a -i container_name --> attached + interactive mode - doesn't work properly first try


docker run -p 3000:80 -d --rm image_id --> makes a container that gets removed automatically when stopped


docker image inspect img-name

docker cp img/. container_name:/img/ --> to copy all files in folder to container


docker run -p 3000:80 -d --rm --name container_name img_name --> create container with desired name

docker build -t name:tag . --> build img with tag(name); you can write project's path instead of .

docker rmi -f name:tag --> remove the image; -f forces the process even if there is a running container

share: docker pus Host:Name pull: docker pull Host:Name


to push on dockerhub:

  1. create depository in account
  2. docker login
  3. create or rename an image to repository name (i.e. "sinasnp/node-hello-world")
  4. then run this: docker push repository_name

you can rename by running: docker tag old_name new_name


data types in docker:

  1. Applicatio(code + environment) : read-only, stored in images
  2. Temporary app date(user inputs): read+write, stored in containers
  3. Permanent App Data(user accounts): read+write, stored withcontainers+volumes

two types of volumes:

  1. Anonymus: created and managed by docker
  2. Named: created by user(these will be used in newer containers)(feedback)

to make program to save volumes:

  1. make the original programto save in a folder (feedback in -3)
  2. build imgae
  3. docker run -p 3000:80 --rm --name feed-cont -v feedback:/app/feedback feed-img

*Anonymous volumes won't be removed if you don't use --rm when creating container. also with each run a new volume will be created and the last volume [which is useless] won't be removed. You can clear 'Anonymous volumes'using these:

  • docker volume ls

  • docker volume prune

  • you can list all volumes using docker volume ls

  • volumes are great for data which 'should be persistent but you don't need to edit directly'

**create volume and mount

dokcker run -p 3000:80 --rm -v feedback:/app/feedback -v /path/to/volume:/app -v app/node_modules --name feed-cont feed-img

now changes in our source are imported in container live

(-v app/node_modules: Anonymous colume - required to prevent docker to overwrite the files in path of previous volume)


(table)

\Anonymous volumesNamed volumesBind Mounts
infocreated specifically for a single containercreated in general not tied to any specific containerlocation on host file system, not tied to any specific container
survives container sutdown/rebootyes, unless --rm is usedyes, removal via Docker CLIyes, removal on host fs
can be shared across containerNOYesYES
can be re-used for same containerNO, even on same image. sincce it's anonymousYES (across restarts)YES (across restarts)

-by default volumes are read-write

  • add -v /app/temp when running a container so it shows other changes(ie server.js) live

    • doesn't work when 'wsl2' is active (windows)
  • the bind mount volume won't be shown in docker volume ls since it's not managed by docker

  • docker volume inspect volume_name --> also shows path in 'host machine'

  • use '.dockerignore' file to ensure that certain folders and file are not copied into IMAGE

in projects when you want to use a 'localhost' link, you better replace it with 'host.docker .ineternal' (requests from container to host machine)


  • we can make containers communicate with each other by creating network. we should create network by our own: docker network create net-name

  • then we connect containers to network by addin below code to run command --network net-name

  • we can also use a container name instead of localhost. for example: mongose.connect(mongodb://mongodb:27017/swfavorites) --> request from container to another container

docker run --name mongodb -d --rm --network net-net mongo:44.18

* docker will not replace your source code. it simply detects outgoing requests and resolves the IP requests
* you don't need to build 'mongo image' when you run mongo. it automatically builds it