DEV Community

Cover image for Docker Container Management
Waji
Waji

Posted on

Docker Container Management

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


Some Basic Commands Hands-on

docker
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will show us some options that we can use and also some of the management commands

docker search hello-world
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will search for the 'hello-world' images from dockerhub

docker pull hello-world
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will pull the latest hello-world image from the dockerhub

docker images
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will show us pulled images

docker run --name hello1 -it hello-world
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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

docker ps
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will show us nothing as the 'hello1' container was stopped immediately

But we can see previous container using

docker ps -a
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will show us 'hello1' container and at what time it exited

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

docker run --name hello2 -d hello-world
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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,

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

# OR

docker rm <container-name>
Enter fullscreen mode Exit fullscreen mode

To remove the hello-world image

docker rmi hello-world
Enter fullscreen mode Exit fullscreen mode

We can confirm this using docker images commmand

Next, we can try creating a container

docker create --name myWEB nginx:latest
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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

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

docker start myWEB
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This will start the container and we can see the process using docker ps

What if we want to delete this container?

docker rm myWEB
Error response from daemon: You cannot remove a running container
Enter fullscreen mode Exit fullscreen mode

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

docker rm -f myWEB
Enter fullscreen mode Exit fullscreen mode

Finally removing the nginx image

docker rmi nginx:latest
Enter fullscreen mode Exit fullscreen mode

Another test we can do using an ubuntu image

docker run -it --name ubuntu_1 ubuntu:bionic
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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

docker attach <container-name>
Enter fullscreen mode Exit fullscreen mode

We can also pause and unpause the container

docker pause ubuntu_1

docker unpause ubuntu_1
Enter fullscreen mode Exit fullscreen mode

Stopping a container

docker stop ubuntu_1
Enter fullscreen mode Exit fullscreen mode

Killing a container

docker kill ubuntu_1
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This is force stop

To remove all Docker containers that are currently stopped

docker rm $(docker ps -a -q)
Enter fullscreen mode Exit fullscreen mode

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

docker run --rm -it ubuntu:bionic
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ The --rm option automatically removes the container when we 'exit' from the container

We can also change the container name

docker rename <current-container-name> <new-custom-container-name>
Enter fullscreen mode Exit fullscreen mode

To inspect a container or an image

docker inspect <container-name> 

# OR

docker inspect <image-name>
Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

Collapse
 
bherbruck profile image
bherbruck

Now do docker-compose

Collapse
 
waji97 profile image
Waji

Hey thanks for the comment, I did write a post on Docker Compose as well! You can check that out over here