Docker basic commands in linux

Docker basic commands in linux

Installation of docker:

yum install docker -y

To start docker service:

service docker start

#shows all the docker images in our docker server:

docker images

#shows all the docker containers in our docker server:

docker ps -a

#shows all the running docker containers in our docker server:

docker ps

With help of Docker image we can execute docker run command to create a docker container and it would be created in start state, if required we can stop or start the container again

Docker image we can get in two ways :

1)Docker hub (use docker pull)

to pull a docker image named tomcat:

docker pull <image_name>

After pulling/creating an image, we need to create a container out of it(container will start running as soon as we create it until we stop it) Use below command:

docker run -d --name <container_name> -p 8081:8080 <image_name> #this container would be exposed to outside network in 8081 port

login to this container(nothing but a server) by below command:

docker exec -it <container_name> /bin/bash

Stop docker container:

docker stop <container_name>

create another container from same image:

docker run -d --name <container_name2> -p 8082:8080 <image_name> #this container would be exposed to outside network in 8082 port

2)Create your own docker file:

vi Dockerfile

To create a image from docker file, use docker build command

docker build -t <image_name> .

If multiple manual steps are being performed while/after creating a single container, we would need to follow the same steps everytime we create new containers,

Hence, all those steps can be added in a single Dockerfile, create image with it and then start using this image to create n no of containers which would be customized


Command to delete all stopped containers at once:

docker container prune

Command to delete all images:

docker image prune -a

Command to remove a docker container:

docker rm <container_name>

Command to remove a docker image:

docker rmi <image_name>