Docker

Docker daily useful commands

Below is the list of most useful docker commands.

Download docker image from the repository

docker pull image_name

Check a list of all docker images

docker images

Check if Docker containers are up & running

 docker ps -a 

Start and stop a container

docker start container_name
docker stop container_name

Get inside of running docker container

docker exec -i -t container_name command

e.g for ubuntu image
docker exec -i -t ubuntu_container_name bash

Remove all docker containers

 docker rm $(docker ps -a -q) 

Forcefully remove all docker containers

 docker rm -f $(docker ps -a -q) 

Remove all docker images

 docker rmi $(docker images -a -q) 

 Forcefully remove all docker images

 docker rmi -f $(docker images -a -q) 

See the logs from docker container

 docker logs -ft container_name 

Docker compose commands

Build all Images

 docker-compose build 

Build and run all containers

 docker-compose up -d 

Stop all containers

 docker-compose stop 

Remove all containers

 docker-compose rm 

Restart only one container

 docker-compose restart container_name 

 MY SQL Docker Container

Sometimes we need to run SQL queries inside docker-containers to check database data. Below is the list of useful commands to run inside my SQL container.

Get inside running MySQL container

 docker exec -it mysql_container-name mysql -uroot -p 

“root” is the username for MySQL database.
After running above command it will ask you a password.

Select database

 USE Database_name

Show tables

 show tables; 

Select table

 SELECT * FROM table_name; 

Take back of MySQL database from docker container

 
docker exec mysql_container-name /usr/bin/mysqldump 
-u root 
--password=root 
database_name > backup.sql 

Restore MySQL database to docker container

 
cat backup.sql | docker exec -i mysql_container-name 
/usr/bin/mysql 
-u root 
--password=root database_name 

Docker Container Stats command

To find out CPU, memory usage of each container.

 docker stats container_name 

docker-stat

See memory and usage for all docker containers

 docker ps -q | xargs  docker stats --no-stream 

 

 

Leave a comment