Docker
Tags: docker, webdev, devops
Published: 2024-04-10 19:41:19

# Docker

## **Use Docker without sudo**

For this, we need to add the user to the docker mod.

Below is the terminal command:

```bash
sudo usermod -a -G docker userName
#or
sudo usermod -a -G docker $USER
```

Now we need to logout and login again to see effects.

**Pull and run a public docker image from the docker hub.**

Here’s the command

version is optional

```bash
docker run image:version
```

### Docker advantages

1 Its light

2 Its fast 

3 It allows to have different configuration layer

i.e. We can separate it form the actual OS configs.

4 We also get the benefit of running the same image 

with different versions simultaneously on the same computer.

## Basic Commands

### **docker pull imageName:version**

This gets the public image from docker hub.

The version is optional

### docker images

It shows the list of all images 

```bash
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
redis                    latest    e10bd12f0b2d   7 days ago      138MB
```

### **docker run imageName:version**

Here things get interesting.

If you mentioned the version while pulling, you must do so now too.

Else docker will pull the latest version of it and then run it.

<aside>
💡 Well, you can stop it by just doing a `Ctrl+C` that's it.

</aside>

### docker ps

lists down all the currently running images.

<aside>
💡 We can also use `Ctrl+C` to stop the image

</aside>

### docker run -d imageName

To run the image in detached mode i.e. now `Ctrl+C` won’t work on it.

It runs in detached mode

example

```bash
docker run -d -p3000:3000 --name node-app example-node
```

### Now to stop a detached image

We need to 

<aside>
💡 `docker ps`
Now we have the id 
We can stop it simply using
`docker stop id`

</aside>

### docker ps -a

This prints the whole history.

From here we can start a container with its id.

by using the command

```bash
docker start id
```

### docker run -p<hostPort>:containerPort image:version

Now the command above looks a bit big.

Let me break it down.

Let’s first have an example

```bash
docker run -p9000:6379 -d redis
```

Result

```bash
docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                       NAMES
5aeaf8ee3849   redis     "docker-entrypoint.s…"   14 seconds ago   Up 11 seconds   0.0.0.0:9000->6379/tcp, :::9000->6379/tcp   xenodochial_curran
```

This helps us bind a host port to the container port.

This is really helpful when we want to run 2 versions of the same image.

We can bind them to 2 different ports.

### docker logs id

This helps us see the logs of a container.

This helps us solve bugs and errors.

<aside>
💡 we can also use names instead of id here

</aside>

## Naming the docker containers

### docker run —name custom-name image:version

```bash
docker run -p9000:6379 -d --name redis_latest redis
docker run -d -p9001:6379 --name redis_old redis:6.2
```

## docker rm container_id_or_name

This command removes the container

ex

```bash
docker rm redis_old redis_latest
```

### Remove all containers

<aside>
💡

```
docker ps -q | xargs docker stop
docker ps -q | xargs docker rm
```

</aside>

### To get the interactive terminal of a container

```bash
docker exec -it container_id_or_name /bin/bash
```

To exit the terminal simply Enter `exit` 

Here `-it` stands for interactive mode.


## Docker Volume 

Think of them as special folders that you can use to store data outside of your container. This is really helpful because it means that even if your container gets deleted or crashes, your data will still be safe and sound.

Example 

```sh
docker run -p 27017:27017 -v atulDB:/data/db --name my-mongodb-container mongo:latest
```