Docker
most of the commands are from the course
to create image through Dockerfile
FROM node
WORKDIR /app
COPY ./package.json .
RUN npm install
COPY . .
EXPOSE 5123
CMD [ "node","./app.js" ]
to run this Dockerfile
docker build .
to get list of all container
docker ps
to stop this comtainer
dockere stop name_of_contaier
to remove this comtainer
dockere rm name_of_contaier
Working with images & Containers
To use prebuild images from dockerhub
docker pull image_name
To check all the process
ps
docker ps -a
Intrective Session
docker run -it image
to create custom images
- [] file extension name
Dockerfile
. - [] **IF WORK DIR DECLARE UPSIDE COPY THEN THE PWD WILL ALREADY WILL BE IN /APP DIR **
- [] this will now run when container are created that is why we use CMD, CMD should always bee last command
- [] Th expose port on the FILE EXPOSE IT"S JUST FOR COTAINER BUT TO EXPOSE FOR PC THE
docker run -p 3000:5123 image_id
FROM image_name
COPY . /app # sorce destination.
WORKDIR /app # where to create app project
RUN npm install
EXPOSE 5123
CMD node ./server.js # this will now run when container are created that is why we use CMD
to delete docker images
docker rmi image_id
rmi
to delete multiple docker images
docker rmi $(docker images -a)
rm
to delete multiple docker containers
docker rm $(docker ps -a)
Read-only Containers
In Docker its layer based every thing is cachced, unless it is updated,when one layer change then all the subsequien layers are reexecuted To take a updated source code...
we dont have to install node_modules when ever updated
and for that
FROM image_name
WORKDIR /app
# we will first copy package.json file
COPY ./package.json .
RUN npm install # then we will run install, so if package.json changes then all the code wil be rexecuted
# then we will copy the source code
COPY . .
EXPOSE 5123
CMD node ./server.js # this will now run when container are created that is why we use CMD
Attach mode/Detach mode
docker attach image_name
to get logs
docker logs image_name
to get follow/further logs
docker logs -f image_name
Interactive mode
Python Example
from random import randint
min_numbr = int(input("Please enter min number : "))
max_numbr = int(input("Please enter max number : "))
if (min_numbr > max_numbr ):
print("Input in valid")
else:
rnd_int = randint(min_numbr,max_numbr)
print(rnd_int)
docker run --help
# you can use -it flag for taht
docker run -it container_name
to Start in attach mode anfter shutdown and to input mode -i
# you can use -it flag for taht
docker start -a -i container_name
Reemov dockr container automatically
by adding flag --rm
dockr run -p 5123:5123 -d --rm cont_id