Docker Container Management
Tags: docker, linux
Published: 2023-03-03 01:41:59

## Introduction

> Docker container management involves tasks such as creating, starting, stopping, and removing containers, as well as monitoring and troubleshooting container performance and health


![Docker container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6ceemvd0i360uu7cswg.png)

---

## Some Basic Commands Hands-on

```bash
docker
```

👉 This will show us some options that we can use and also some of the management commands

```bash
docker search hello-world
```

👉 This will search for the 'hello-world' images from dockerhub


```bash
docker pull hello-world
```

👉 This will pull the latest hello-world image from the dockerhub


```bash
docker images
```

👉 This will show us pulled images

```bash
docker run --name hello1 -it hello-world
```
👉 This command creates and starts a new Docker container using the hello-world image 

> 💡 **'hello1'** is the container name and **'-it'** enables interactive mode but '-it' shouldn't be used as it is for developing or debugging only as it enables us to interact with the container's shell

In the above case, as the 'hello-world' image doesn't have any shell to be interacted with, it automatically pushes us back to our Linux terminal. To check any running container process

```bash
docker ps
```
👉 This will show us nothing as the 'hello1' container was stopped immediately

But we can see previous container using

```bash
docker ps -a
```

👉 This will show us 'hello1' container and at what time it exited

Now let's use the `-d` option to run a docker container

```bash
docker run --name hello2 -d hello-world
``` 
👉 This will show us the container ID and quit itself

> 💡 The 'hello-world' image is designed to automatically quit itself after its processing. `-d` option indicates the container to run in the background

If we want to delete these exited containers,

```bash
docker rm <first-four-digits-of-container-ID>

# OR

docker rm <container-name>
```

To remove the `hello-world` image

```bash
docker rmi hello-world
```

We can confirm this using `docker images` commmand

Next, we can try creating a container

```bash
docker create --name myWEB nginx:latest
```
👉 `create` command simply 'creates' the container and doesn't run it while the `run` command creates and run the container 

> 💡 `run` is a shortcut for the `docker create` and `docker start` commands combined


![Run and Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5931k93h0ttgtnhy3xz.png)



 We can also check the status using the `ps -a` that will show us the container status as 'Created'

```bash
docker start myWEB
```

👉 This will start the container and we can see the process using `docker ps`

What if we want to delete this container?

```bash
docker rm myWEB
Error response from daemon: You cannot remove a running container
```

We can either stop the container first and delete it or force delete it using

```bash
docker rm -f myWEB
```

Finally removing the nginx image

```bash
docker rmi nginx:latest
```

Another test we can do using an ubuntu image

```bash
docker run -it --name ubuntu_1 ubuntu:bionic
```

👉 This will allow us to connect to ubuntu shell automatically 

So what if we want to exit from the ubuntu image terminal while keeping the container running?

✨ We will use `CTRL + P + Q` 

We can confirm that our container is running even after returning to our own terminal using `docker ps`

To return to the container shell

```bash
docker attach <container-name>
```

We can also pause and unpause the container

```bash
docker pause ubuntu_1

docker unpause ubuntu_1
```

Stopping a container

```bash
docker stop ubuntu_1
```

Killing a container

```bash
docker kill ubuntu_1
```
👉 This is force stop

To remove all Docker containers that are currently stopped

```bash
docker rm $(docker ps -a -q)
```

Now what if we don't apply a custom name for our container

```bash
docker run --rm -it ubuntu:bionic
```

👉 The `--rm` option automatically removes the container when we 'exit' from the container 


We can also change the container name

```bash
docker rename <current-container-name> <new-custom-container-name>
```

To inspect a container or an image 

```bash
docker inspect <container-name> 

# OR

docker inspect <image-name>
```

---

✍ In this post I walked through some of the basic docker container commands that are beginner friendly