In order to work with docker
- Docker 23.0.1
- Docker Desktop 4.17.0
docker version
docker
docker run -d -p 80:80 docker/getting-started
//Active containers
docker ps
// All containers
docker ps -a
docker stop Container_ID
docker rm Container_ID
docker run -d -p 80:80 alexwhen/docker-2048
docker exec -it Container_ID_or_Container_Name sh
docker run -d -p 8080:80 nginx
docker run --name Container_Name -d -p 8081:80 wordpress
docker rm -f Container_ID_or_Container_Name
docker run -p 3000:80 -p 5000:80 -p 9000:80 -d docker/getting-started
docker image ls
or
docker images
-
the image and the containers do have a relationship , therefore we cannot remove the image before removing its containers
-
The docker images, are stored inside the docker directory: /var/lib/docker/ images are stored there.
docker rmi Image_Repository
or
docker image rm Image_Name
docker pull Image_Name
A good practice is to pull the image then to run the container
docker image inspect Image_Name
-
Docker Daemon is the tool that executes commands from the client to the docker host
-
Docker Daemon is a part of Docker host
-
The client issues commands to docker Daemon via the unix socket that is located in "/var/run/docker.sock"
docker run bash -c "echo foo > bar.txt & cat bar.txt"
docker run bash -c "cat bar.txt"
gives this result
cd Desktop
mkdir bind-mount
pwd
echo $PWD
output
docker run -v $PWD:/tmp bash bash -c "echo foo > tmp/bar.txt & cat tmp/bar.txt"
docker run -v $PWD:/tmp bash bash -c "cat tmp/bar.txt"
output
Volumes are used to share data between the host and the container
via this link https://startbootstrap.com/theme/sb-admin-2
cd Desktop/dashboard
docker run --name dashboard -d -p 8080:80 nginx
docker run --name dashboard -v $PWD:/usr/share/nginx/html -d -p 8080:80 nginx
accessing the link http://localhost:8080/
will give the dashboard theme instead of the classic nginx welcome page
the changes made to index.html page on the bootstrap theme folder will be replicated in the container
AMIGOSCODE on the sidebar instead of SB admin
docker volume --help
docker volume ls
docker build . -t dashboard
- -t stands for tag name
(using built dashboard image instead of nginx image)
docker run --name dashboard -d -p 8080:80 dashboard
note that we don't need the volume argument because it is already specified in the DockerFile
Creating the user-api
docker run -w /src -v $PWD:/src --rm node npm init --yes
docker run -w /src -v $PWD:/src --rm node npm i -S express
- -w creates a folder inside the container /src
- -v mounting a volume
- -rm removes the container when it exists
- node the image name from docker registry
- npm init --yes / npm i -S express commands to be executed in docker container
after creating the Dockerfile inside the user-api folder
docker build . -t user-api
docker run --name user-api -d -p 3000:3000 user-api
http://localhost:3000/api/v1/users
docker pull postgres
docker pull postgres:14.7
docker tag --help
docker tag dashboard:latest dashboard:1.0
cd ~/Desktop/dashboard
docker build . -t dashboard:latest -t dashboard:1.0
docker run --name dashboard-v1 -d -p 8080:80 dashboard:1.0
docker run --name dashboard-latest -d -p 8081:80 dashboard
note that when we don't specify the tag latest will be chosen by default
docker build . -t dashboard:latest -t dashboard:2.0
docker run --name dashboard-latest -d -p 8082:80 dashboard
note that we should never use the tag latest in production
docker login
-
we enter the UserName and the Password of the dockerHub Account
-
docker login is necessary to pull private images from dockerHub account
note that if we log out from docker the auths filed will be empty
-
we first create the docker repository in the dockerHub ( specifying if it is private or public )
-
the next step is to re-tag the image in order to be pushed into our account repositories
-
we push the newly tagged image using the command : docker push Account_Name/Image_Name:tag
docker tag user-api:latest anayaro/user-api:latest
docker images
docker push anayaro/user-api:latest
docker inspect ContainerName_or_ContainerID
docker inspect dashboard-v1
docker logs ContainerName_or_ContainerID
docker logs dashboard-v1
docker start ContainerName_or_ContainerID
docker start dashboard-v1
docker exec ContainerName_or_ContainerID command
docker exec ContainerName_or_ContainerID env
docker exec dashboard-v1 env
docker exec user-api env
docker exec ContainerName_or_ContainerID ls
docker exec dashboard-v1 ls
docker exec user-api ls
docker exec -it user-api bash
//or
docker exec -it user-api sh
#ls
#pwd
#top
- -it stands for interactive mode
- sh is the default shell for nginx
- with this command we get inside the container
- press Ctrl + c or Ctrl +d to quit the commandline
- we need to know what type of command line that is supported by the container sometimes it does not support neither sh nor bash
docker network --help
docker network create networkName
docker network create mongo
docker inspect networkName
docker run --name mongo -d --network mongo -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=username \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongo
docker run --name mongo-express --network mongo -d -p 8081:8081 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=username \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_MONGODB_SERVER=mongo mongo-express
- -e stands for environment args
Note that mongo-express container is connected to our mongo container via the network mongo
docker run --rm -it mongo sh
# mongosh
- --rm removes the container as when we exit the sh commandline
- we use mongosh instead of mongo for the newer versions
# mongosh --host mongo -u username -p password
- note that this container cannot connect to the mongo container be cause it is not using the mongo network
- the host argument has to be specified by the container's name
docker run --network mongo --rm -it mongo sh
# mongosh --host mongo -u username -p password
- this time it is working (y)
# show databases
docker start user-api
docker start dashboard-v1
docker network create test
docker network connect test user-api
docker network connect test dashboard-v1
docker exec -it dashboard-v1 sh
# curl user-api:3000/api/v1/users
- successfully connected to user-api from dashboard (y)
docker network disconnect network_name container_name
//V1
docker-compose --help
//V2
docker compose --help
//creating the folder
cd Desktop
mkdir docker-compose
cd docker-compose
//creating the docker-compose file
touch docker-compose.yml
ls
we have docker-compose.yml file that contains the 2 services mongo and mongo-express
inside docker compose file we can specify
- services
- networks
- volumes
docker compose up -d
docker compose start
docker compose stop
docker compose down
//deprecated will no longer be supported after April 13th, 2023.
docker scan image_name
// use the scout instead of scan
docker scout cves image_name
docker scout cves dasboard
- more often than not we cant get vulnerabilities to 0
- we should focus on treating the high/ medium risk ones
check these links for more information
https://trivy.dev/ https://aquasecurity.github.io/trivy/v0.38/
trivy scan for two types of security issues
- vulnerabilities
- misconfigurations
docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v $HOME/Library/Caches:/root/.cache/ aquasec/trivy:0.38.3 image Image_Name
- they are very small
- Using distroless images in production is best practice
- distroless images only contain our application and its dependencies ,
- they do not contain package managers , nor shells or any other programs found in a standard linux distribution
check these links for more information
https://github.com/GoogleContainerTools/distroless
docker scout cves gcr.io/distroless/java17-debian11
docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v $HOME/Library/Caches:/root/.cache/ aquasec/trivy:0.38.3 \
image gcr.io/distroless/java17-debian11
check these links for more information
https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html


















