Introduction
Docker container management involves tasks such as creating, starting, stopping, and removing containers, as well as monitoring and troubleshooting container performance and health
Some Basic Commands Hands-on
docker
👉 This will show us some options that we can use and also some of the management commands
docker search hello-world
👉 This will search for the 'hello-world' images from dockerhub
docker pull hello-world
👉 This will pull the latest hello-world image from the dockerhub
docker images
👉 This will show us pulled images
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
docker ps
👉 This will show us nothing as the 'hello1' container was stopped immediately
But we can see previous container using
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
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,
docker rm <first-four-digits-of-container-ID>
# OR
docker rm <container-name>
To remove the hello-world
image
docker rmi hello-world
We can confirm this using docker images
commmand
Next, we can try creating a container
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 thedocker create
anddocker start
commands combined
We can also check the status using the ps -a
that will show us the container status as 'Created'
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?
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
docker rm -f myWEB
Finally removing the nginx image
docker rmi nginx:latest
Another test we can do using an ubuntu image
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
docker attach <container-name>
We can also pause and unpause the container
docker pause ubuntu_1
docker unpause ubuntu_1
Stopping a container
docker stop ubuntu_1
Killing a container
docker kill ubuntu_1
👉 This is force stop
To remove all Docker containers that are currently stopped
docker rm $(docker ps -a -q)
Now what if we don't apply a custom name for our container
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
docker rename <current-container-name> <new-custom-container-name>
To inspect a container or an image
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
Top comments (2)
Now do docker-compose
Hey thanks for the comment, I did write a post on Docker Compose as well! You can check that out over here