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
- 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" ]
-
then open terminal and run below command (make sure you are in the project directory and have Dckerfile there):
docker build .
-
then copy the image id (i.e. sha256:1c7...)
*we use tag name later -
then run this code :
docker run -p 3000:80 sha256:1c7...
-
then open the browser and go to this address: localhost:3000
To stop the container you can run below code (preferably in another terminal):
-
docker ps
--> to show active containers, so you can copy the name -
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:
- create depository in account
docker login
- create or rename an image to repository name (i.e. "sinasnp/node-hello-world")
- then run this:
docker push repository_name
you can rename by running:
docker tag old_name new_name
data types in docker:
- Applicatio(code + environment) : read-only, stored in images
- Temporary app date(user inputs): read+write, stored in containers
- Permanent App Data(user accounts): read+write, stored withcontainers+volumes
two types of volumes:
- Anonymus: created and managed by docker
- Named: created by user(these will be used in newer containers)(feedback)
to make program to save volumes:
- make the original programto save in a folder (feedback in -3)
- build imgae
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 volumes | Named volumes | Bind Mounts |
---|---|---|---|
info | created specifically for a single container | created in general not tied to any specific container | location on host file system, not tied to any specific container |
survives container sutdown/reboot | yes, unless --rm is used | yes, removal via Docker CLI | yes, removal on host fs |
can be shared across container | NO | Yes | YES |
can be re-used for same container | NO, even on same image. sincce it's anonymous | YES (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