Part III : Docker Useful Commands and Tips

This is third part of docker tutorials,I have already shared docker tutorial Introduction of Docker and How to install Docker on Linux/Windows.This docker tutorial describe some useful common docker command like docker push,docker pull, docker run command and how to docker build using command etc.

Some Common Usage Docker Command

Here I will list down some useful and common usage docker command to build and run docker images using command line.

How to show docker containers

You get all listed containers using below docker command,
docker ps -a

How to show docker images

You can get all images using below docker command,
docker images

How to pull docker image

You can pull an image or a repository from a registry using below docker command,
docker pull image_name/repository_name

image_name/repository_name : You can replace this variable with your repository name or docker image name.

How to run docker image

You can run a docker image or a repository from a registry using below docker command,
docker run image_name/repository_name

image_name/repository_name : You can replace this variable with your repository name or docker image name.

How to Delete docker Container

You can delete container using below docker command,
docker rm conatiner_id

conatiner_id : You can replace this varaible with your containers id.

How to Delete docker Image

You can delete docker image using below command.
docker rmi -f 13d839aa8178

13d839aa8178: image id, You will get from image list.

How to Delete multiple docker Images

You can delete multiple docker images using below docker command,
docker rm $(docker ps -a -q -f status=exited)

How to Connect docker Container

You can connect docker container using below command.
docker exec -it 13d839aa8178 bash

13d839aa8178: container name or uuid.

How to create Docker Image

You can create your own docker image by following below steps.
Step 1: Create a directory and cd into this directory using below command and .
$mkdir mydockerbuild
$cd mydockerbuild

Step 2: Create a new Dockerfile using below command.
$vi Dockerfile

Step 3: Add a FROM statement by copying the following line into the file
FROM docker/whalesay:latest

Step 4: Below command tells the image the final command to run after its environment is set up
CMD /usr/games/fortune -a | cowsay

How to build a Docker Image using DockerFile

$docker build -t docker-whale

How to tag and push image

You can create tag for your docker image and push image into dockerhub using below command.You need to login into dockerhub to push and pull docker image.

Step 1: Create tag for your docker image using below command.
$docker tag 7d9495d03763 phpflow/docker-whale:latest

Step 2: push docker image using below command.
$docker push phpflow/docker-whale

Conclusion :

I have shared basic commands of docker.I have created new images using base docker image.We have also push the newly created docker image into docker hub and pull the image from the repository.