If you're new to Docker or just starting to explore containerization, mastering a few basic commands is the first step toward becoming a Docker pro!
Docker simplifies deployment of applications by running container.
Here are some simple Docker commands to help you get started:
Installation of Docker on Linux Server
$ sudo apt-get install docker.io
Find out which version of Docker is installed on your machine
$ docker –version
To check the status of Docker service
$ service docker status
To stop Docker service
$ service docker stop
To start Docker service
$ service docker start
To see all the images in Docker locally
$ docker images
To pull image from Docker hub
$docker pull <img-name>:<img-tag>
To remove a Docker Image from your local repo
$ docker image rm <img-name or img-id>
Or
$ docker rmi <img-name or img-id>
To run a container in interactive mode
$ docker run -it –-name <cont-name or cont-id> <img-name or img-id>
To list the running Containers
$ docker ps
To list All Containers (including stopped ones)
$ docker ps -a
To stop a running Container
$ docker stop <cont-name or cont-id>
To start a Container
$ docker start <cont-name or cont-id>
To remove a running Container
$ docker rm <cont-name or cont-id>
Note: It is important to stop a container before removing.
To remove all images at once
$ docker rmi $(docker images -q)
Here, $(docker images -q) will list the IDs of all the Docker images
and $ docker rmi will remove images based on the IDs passed to it.
To remove all containers at once
$ docker rm $(docker ps -aq)
Here, $(docker ps -aq) will list the IDs of all the Docker containers
and $ docker rm will remove the containers based on the IDs passed to it.
To view Logs from a Container
$ docker logs <cont-name or cont-id>
To go inside the container (container’s main process)
$ docker attach <cont-name or cont-id>
To execute command inside the running container
$ docker exec -it <cont-name or cont-id> /bin/bash
To exit a container
$ exit
By getting familiar with these basic commands, you're on your way to mastering Docker and containerized workflows.