# Docker basic commands in linux

Installation of docker:

```bash
yum install docker -y
```

To start docker service:

```bash
service docker start
```

#shows all the docker images in our docker server:

```bash
docker images
```

#shows all the docker containers in our docker server:

```bash
docker ps -a
```

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

```bash
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:

```bash
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:

```bash
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:

```bash
docker exec -it <container_name> /bin/bash
```

Stop docker container:

```bash
docker stop <container_name>
```

create another container from same image:

```bash
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:

```bash
vi Dockerfile
```

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

```bash
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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684341040900/35fb4eab-b276-4b5c-9777-270ab9d71640.png align="center")

---

Command to delete all stopped containers at once:

```bash
docker container prune
```

Command to delete all images:

```bash
docker image prune -a
```

Command to remove a docker container:

```bash
docker rm <container_name>
```

Command to remove a docker image:

```bash
docker rmi <image_name>
```
