Docker Guide
Understanding Dockerfile, Creating Images, DockerIgnore | Docker series in Nepali | EverydayKarma
config→ image(environment or template)→ container
Sample docker config file (Dockerfile)
FROM "node:21-alpine"WORKDIR /appCOPY . .CMD ["npm","run","start"]After server and .node_modules are created
FROM "node:21-alpine"WORKDIR /appCOPY package.json .RUN npm install //install dependencies in docker envCOPY . .CMD ["npm","run","start"]Build image
docker build .List docker images
docker images or docker image lsGet ID of docker images
docker image ls -qRemove docker image
docker rmi <IMAGE_ID> or docker image rm <IMAGE_ID>docker rmi -f <IMAGE_ID> //forcefully removing image as sometimes container is running using the image
//to delete all imagedocker image rm 234 2342 3423//ordocker image rm $(docker image ls -q) //get all image id and deleteRemove docker container
docker rm <CONTAINER_ID>docker container rm <CONTAINER_ID> //same as above
//similarly to remove all containersdocker container rm $(docker container ls -a -q)Add tag name to image
docker build . -t mynode:1.0Run docker image (containerize)
docker run <IMAGE_ID or NAME>:<VERSION> //eg: docker run mynode:1.0//use -d to run in detached modedocker run -d -p 5000:4000 2344254 //runnning in detached mode with port mappingSee container list
docker container ls //show only running containerdocker ps //same command as abovedocker container ls -a //to see all containerdocker container ls -la //last active containerGet into docker image environment (i.e. operating system where our app is running). For this run docker image in interactive mode(it). This allows to access shell (generally bash in Linux) of used OS
docker run -it <IMAGE_ID or NAME> /bin/shSet environment (env) variable
docker run -e <NAME>=<VALUE> <IMAGE_ID> //eg docker run -e NODE_ENV="production" 35a80docker run -e NODE_ENV="production" -e DB_NAME="trydb" 35a80
docker run --env-file=<ENV_LOCATION> <IMAGE_ID>//eg docker run --env-file=.env 35a80NOTE: RUN command runs during image building whereas CMD commands runs after container is created
Map port of docker to local
docker run -p <LOCAL_PORT>:<DOCKER_PORT> 23421Stop docker container
docker stop <CONTAINER_ID> or <NAME>docker container stop <CONTAINER_ID> or <NAME>.dockerignore file to list the files that should not be added in the docker
Login docker
docker loginPush image to dockerHub
docker push romanpoudel/imagenamePull image to local
docker pull romanpoudel/imagenameName the container
docker run -p 4000:4000 --name <NAME> romanpoudel/imageStop container running
docker stop <unique id of image>e.g docker stop 234docker kill werewr //this is to stop forcefully immediatelyto write command in multiline for readability use backslash \ and hit enter. This is the feature of shell not docker.
Ignore data from cache
docker build --no-cache . -t romanpoudel/nodejsDocker Compose (docker-compose.yml)
docker compose comes pre-installed with docker
docker-compose --versionsample yaml file
version: "3.8" //version of dockercomposename: "main_container_of_compose"
services: web: image: "mero_image_from_compose" container_name: "mero_node_container" build: dockerfile: ./Dockerfile ports: -"4000:4000" depends_on: -db
db: container_name: "mero_db_container" image: postgres environment: POSTGRES_DB: mydatabase POSTGRES_USER: myuser POSTGRES_PASSWORD: mypasswordDocker compose CMD
docker-compose up //this creates a running containerdocker-compose build //to build imagedocker-compose downDocker volume
add
-v or —versionto sync vscode to docker and specify location i.e.-v $(pwd):/app
If you don’t want to synchronize some file then use -v location i.e.
-v /app/node_modules
You can make separate file for development and production. In that case, you change the name of the file like Dockerfile.dev and Dockerfile. In this case, you can build the image as
docker build -t mynode:v1 -f Dockerfile.dev .In this case it searches the file in . i.e. present directory.
See docker networks
docker network psInspect container
docker inspect <CONTAINER_NAME>Create separate network
docker network create <NAME>Assigning network while running the container
docker run -d -p 4001:4000 --name <NAME> --network <NETWORK_NAME> mynode:v1To run docker in host network, we should not mention port as it uses computer network by default. It is not supported by Mac and Linux for security reasons.
docker run -d --name mynodeapp --network host mynode:v1Default type network is bridge network, it uses subnet address and bridge connects all the container to computer network.
Remove unused networks
docker network prune