Docker CLI cheatsheet

Docker Containers

LIST CONTAINERS
# lists all active containers [use alias 'ps' instead of 'container']
docker ps
docker container ls # lists all containers [active and in-active/exited/stopped] docker ps -a docker ps --all
docker container ls -a
[lists only container ID] docker ps -a | awk '{print $1}'
docker container ls -q [lists only the in-active/exited/stopped containers] docker ps -f "status=exited" docker ps --filter "status=exited" [lists only the created containers] docker ps --filter "status=created" [lists only the running containers] docker ps --filter "status=running"
[lists can also be filtered using names]
docker ps --filter "name=xyz" CREATE CONTAINERS # create a container without starting it [status of docker create will be 'created'] docker create image_name docker create --name container_name image_name (i.e, docker create --name psams redis) # create & start a container with/without -d (detach) mode [-i, interactive keeps STDIN open even on detach mode] [docker run = docker create + docker start] docker run -it image_id/name docker run -it -d image_id docker run -it -d image_name (i.e, docker run -it -d ubuntu) # create & debug a container [the container status become 'exit' after exit from console] docker run -it image_id bash docker run -i -t image_id /bin/bash (i.e, docker run -it ee8699d5e6bb bash) # name docker container while creation docker run --name container_name -d image_name (i.e, docker run --name psams -d centos) # publish a container's exposed port to the host while creating container [-p in short, --publish] docker run -d -p local-machine-port:internal-machine-port image_name (i.e, docker run -d -p 8081:80 nginx) # mount volume while creating a container [-v, volume maps a folder from our local machine to a relative path in a container] docker run -d -v local-machine-path:internal-machine-path image_name (i.e, docker run -d -p 80:80 -v /tmp/html/:/usr/share/nginx/html nginx) [http://localhost/sams.html where, sams.html is located in local machine path] # auto-restart containers [in-case, if there is a failure or docker stops by itself] docker run -dit --restart=always container_id docker run -dit --restart always container_id [restart only on failure] docker run -dit --restart on-failure container_id [restart unless stopped] docker run -dit --restart unless-stopped container_id

# update specific container's restart service
docker update --restart=always container_id
[update all the available containers]
docker update --restart=no $(docker ps -a -q) MANIPULATE CONTAINERS # debug/enter a running docker container [-i, interactive and -t, -tty is mandate for debugging purpose] docker exec -it container_id bash (i.e, docker exec -it 49c19634177c bash) # rename docker container docker rename container_id target_container_name docker rename container_name target_container_name (i.e, docker rename 49c19634177c sams) START/STOP/REMOVE CONTAINERS # stop container [stop single container] docker stop container_id docker container stop container_id
[stops all the containers]
docker stop $(docker ps -aq)
# kill container [docker stop and kill does the same job; but, 'stop' does safe kill and 'kill' does not]
[docker stop -> send SIGTERM and then SIGKILL after grace period]
[docker kill -> send SIGKILL]
[kill single container]
docker kill container_id
docker container kill container_id
[kills all the containers]
docker kill $(docker ps -aq)
# start container
[start a single container]
docker start container_id
docker container start container_id
[start all containers]
docker start $(docker ps -aq)
# restart container
[restart a single container] docker restart container_id docker container restart container_id
[restarts all containers]
docker restart $(docker ps -aq) # remove all containers docker rm $(docker ps -aq) # remove a single container [works only on exited container] docker rm container_id docker container rm container_id (i.e, docker rm 49c19634177c) # remove all the exited containers docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm # force remove single container [works on active containers by stopping them] docker rm -f container_id (i.e, docker rm -f 49c19634177c) OTHERS # container full details docker inspect container_name/id (i.e., docker inspect 49c19634177c)

# get specific information from the container details
docker inspect -f '{{ property_key }}' container_id
(i.e., docker inspect -f '{{ .Config.Hostname }}' 40375860ee48)

# see specific container's logs
docker logs --follow container_name/id
(i.e., docker logs --follow 40375860ee48)

Docker Images

DOWNLOAD IMAGES
# pull docker images
docker pull image_name
(i.e, docker pull centos)
(i.e, docker pull prashanthsams/selenium-ruby)

# list all images
docker images

# list all dangling images
[the term 'dangling' means unused]
docker images -f dangling=true REMOVE IMAGES # remove single image [works only on images with no active containers] docker image rm image_id (i.e, docker image rm e9ebb50d366d) # remove all images docker rmi $(docker images -q) # removes all dangling images docker images -f dangling=true docker images purge
docker rmi -f $(docker images -f "dangling=true" -q) OTHERS # save and load image [save an existing image]
docker save existing_image > "target_image.tar"
(i.e., docker save nginx > “image_name.tar")
[load the newly generated image]
docker load -i target_image.tar



# get complete details about an image
docker inspect image_name/id
(i.e., docker inspect prashanthsams/selenium-ruby) # to check history of the specific image docker history image_name (i.e., docker history prashanthsams/selenium-ruby)

MISC

# auto-start docker daemon service on device boot
LINUX
sudo systemctl enable docker
MAC
sudo launchctl start docker

# restart docker daemon service on device boot
LINUX
sudo systemctl restart docker

# copy a file into docker container from local (manual)
docker cp /source_path container_id:/target_path
(i.e., docker cp /tmp/source_file.crx c46e6d6ef9ba:/tmp/dest_file.crx)

# copy a file from docker container to local (manual)
docker cp container_id:/source_path /target_path
(i.e., docker cp c46e6d6ef9ba:/tmp/source_file.crx /tmp/dest_file.crx)

# remove stopped containers, images with no containers, networks without containers 
docker system prune -a

# search docker hub for an image
docker search search_by_name
(i.e., docker search prashanthsams/selenium-ruby)



# check container memory usage (like top in linux) 
docker stats



# check container memory using 3rd party lib
LINUX
brew install ctop
ctop -a



# find changes to the container's filesystem from start
docker diff container_id

# zip and export container
docker export -o "container_name.tar" container_id
docker export --output "container_name.tar" container_id
docker export container_id > "container_name.tar"

DOCKER-COMPOSE

# run docker compose to create, start and attach to containers for a service
[the below cmd executes the rules written under docker-compose.yml file in your project]
docker-compose up
[run docker compose in a detach mode (background)]
docker-compose up -d
[run specific service from your yml file]
docker-compose up service_name

# similar to 'docker-compose up' but it overrides the cmds used in the service config
['run' priorities which cmd to run first; if the service config starts with bash, we can override it]
docker-compose run service_name python xyz.py
docker-compose run service_name python xyz.py shell
['run' does not create any of the ports specified in the service configuration, so use '--service-ports']
docker-compose run --service-ports service_name
[manually binding ports]
docker-compose run --publish 8080:80 service_name

# scale up containers in a service
docker-compose up --scale service_name=5
(i.e., docker-compose up --scale firefoxnode=5)
# quits/shuts down all the services docker-compose down # check if all the composed containers are running docker-compose ps
# start/stop service
[make sure the container state exists in-case if you need to start it]
docker-compose stop
docker-compose stop service_name
docker-compose start
docker-compose start service_name

# pause and unpause docker container's state that runs on a service
docker-compose pause
docker-compose unpause

# check all environments variables available to your running service
docker-compose run service_name env



OTHERS
[check all the active images that runs on a service]
docker-compose images
[update docker images with the latest changes in yml file]
docker-compose pull
[check logs]
docker-compose logs

DOCKER HUB – REMOTE REGISTRY (workflow)

# convert your Dockerfile to a docker image
[use -t to set image name and tag]
docker build .
docker build dockerfile_path
docker build -t custom_image_name dockerfile_path
[use tag if needed]
docker build -t custom_image_name:tag dockerfile_path



# commit docker container for an image to be prepared
[create a container]
docker run -it -d locally_created_image
[get container id]
docker ps
[make commit]
docker commit container_id custom_image_name
docker commit container_id username/custom_image_name
(i.e., docker commit 822b26bdd62d prashanthsams/psams)

# login to your docker hub account with username & passwrod
[dynamically provide dockerhub login details on runtime]
docker login
[pre-stored details for dockerhub login]
docker login -u your_dockerhub_username -p your_dockerhub_password

# push the commit to your remote docker hub account
docker push custom_image_name
docker push username/custom_image_name
(i.e., docker push prashanthsams/psams)

# pull your newly created remote docker image
docker pull newly_created_image
(i.e., docker pull prashanthsams/psams)

 

Jenkins environment variable setup

Manual Feed

# set custom job name
jobName="your_job_name"
(note: by default, you can use $JOB_NAME which doesn't require any env setup)

# create a shell file and add the below statement
echo 'export VN_NAME="$jobName"' > ~/load_env.sh

# set user privilege
chmod 750 ~/load_env.sh

# temp env variable call
source ~/load_env.sh
echo $VN_NAME

Jenkins Plugin to inject ENV variable

  • By default you will have the Jenkins plugin EnvInject API Plugin
  • Go to Job > Build Environment  and select Inject environment variables to the build process
  • Set variable key and value as in below

Default Jenkins Configure

The environment variables can also be set through Global properties

  • Go to Manage Jenkins > Configure System > Global properties
  • Add the variable key and value