Docker commands for new Devops Engineers

Docker basic commands for beginners:

  • dicker ps
    • To see running docker processes.
  • docker ps -a
    • To see all the docker processes, including stopped ones.
  • docker exec -it czcmysql /bin/bash
    • To login into particular container, here czcmysql is mysql’s container name, you can also use container id instead of name to login.
  • docker image ls
    • List all docker images.
  • docker container ls
    • List all docker containers.
  • sudo docker start czcbackend
    • To start a particular docker container, here czcbackend is my container’s name.
  • sudo docker stop czcbackend
    • To stop a particular docker container, here czcbackend is my container’s name.
  • sudo docker restart czcjenkins
    • To restart a particular docker container, here czcjenkins is my jenkins container name.
  • sudo docker update –restart=always <container>
    • This will update the already running docker containers restart behavior.
  • sudo docker volume ls
    This lists down the volumes.
  • sudo docker system prune -a:
    Removes all the stopped containers (very risk, dont use unless it is lower env and doing some stuffs, make sure you have taken relevant data’s)
  • sudo docker container prune
    This removes only the not used containers
  • sudo docker volumes prune
    This removes only the not used volumes
  • sudo docker network prune
    This removes unused networks.

We have a command something like this for dangling images/none images/dangling volumes.

docker images –filter dangling=true -qa

This is something which docker uses intermittently to create the requested docker and left as it as after the completion of that operation, but you can go ahead and prune it if you required.

To Restart all the running docker containers:

  • sudo docker restart $(docker ps -a -q) in lower docker versions
  • sudo docker restart $(docker ps -q) in higher docker versions
  • sudo docker logs czcbackend
    • To see the logs details of a particular docker container, here czcbackend is my container’s name.
  • sudo docker logs czcbackend -f
    • To monitor the logs details of a particular docker container, here czcbackend is my container’s name.
  • docker logs –tail 10 czcbackend
    • This shows the last 10 lines of the container czcbackend.
  • sudo docker logs czcbackend  | grep ‘nullpointerexception’
    • To monitor the logs details with the text “nullpointerexception” of a particular docker container, here czcbackend is my container’s name.
  • sudo docker rm d724fc6c512c31585bfeebccce1196df09cdc3c9152f01eb627aa3c198982c09
    • Removing docker container
  • sudo docker inspect container_id
    • Gives detailed information about the container.
  • sudo docker cp callmeuissr.sh czcjenkins:/var/jenkins_home/script/
    • This copies the callmeuissr.sh file from your current linux machine location to your docker container named czcjenkins and in this path /var/jenkins_home/script.
  • docker container exec -u 0 -it czcjenkins bash
    • This command can be used something similar to this functionality, (getting into container called czcjenkins (for jenkins) and update the war file(jenkins docker container update)).
    • If you are looking for the complete docker jenkins update details, then this medium post can help you.
  • sudo docker logs czcbackend >& czcbackend-may14.log
    • This command copies the czcbackend container complete logs to czcbackend-may14.log file.

This can be verified by proceeding the below way:

  1. sudo docker exec -it czcjenkins /bin/bash (getting into czcjenkins docker container)
  2. cd /var/jenkins_home/script (going into the pasted path)
  3. ls (will show the list along with your copied file callmeuissr.sh file)

Can I clean up docker overlay2 folder ?

This will contain lot of stuffs used by the docker, it is bit difficult to identify which is not needed by you, so always proceed with system prune (but this removes all the not used containers/images, personally I ran and removed my stopped container which is needed for me, so use system prune docker command with your own risk.)command mentioned below to safely remove the unused stuffs by docker.

How to clean up the docker (like stopped containers, unused stuffs etc.) ?

docker system prune

docker system prune

Please refer this link for detailed understanding on do’s and don’t.

How to export the docker container?

sudo docker export czcjenkins > czcjenkins.tar

Here czcjenkins is my docker container name.

How to backup and restore mysql running on docker containers ?

Backup

docker exec ngmysql /usr/bin/mysqldump -u root --password=MYSQL_PASSWORD ngdeveloper > ngdeveloper_Aug8_2019.sql
  • ngmysql: docker container name
  • root: mysql root username
  • MYSQL_PASSWORD: mysql root password
  • ngdeveloper: database name which you want to backup

Restore

cat ngdeveloper_Aug8_2019.sql | docker exec -i ngmysql /usr/bin/mysql -u root --password=MYSQL_PASSWORD ngdeveloper

How to restart all docker containers at once?

sudo docker restart $(docker ps -a -q)

Leave a Reply