Chapter 2: Docker Containers and Images

Docker Containers and Images

Docker Containers

What is a Container?

Container is like a virtual machine which holds the entire packages that is needed to run the application.
Containers are more portable and lightweight.

What is Image in docker?

Docker images are the read only binary templates used to create docker containers.
Images become containers when they run on docker engine.

Basic information about Container and Image

Container ID:- 579c25a52e88
Image:- Ubuntu
Image Name:- upbeat_panini

Remember above information, It will help you to know, how the Container Id, Image type and Image name look likes.

Basic commands for Docker Containers

To list running containers

$ sudo docker container ps
or
$ sudo docker container ls

To List all containers

$ sudo docker container ps -a
or
$ sudo docker ps -a

To start docker container

$ sudo docker start <container-id>

To Stop docker container

$ sudo docker stop <container-id>

To check containers Logs

$ sudo docker logs -f <container-id>

To go inside container

$ sudo docker attach <container-id>

To Remove a stopped container

$ sudo docker rm <container-id>

To Remove a running container

$ sudo docker rm -f <container-id>

To Remove all Stopped containers

$ sudo docker rm $(docker ps -q -f “status=exited”)

To Remove all Running containers

$ sudo docker rm -f $(docker ps -aq)


Basic commands for Docker Images

To list Docker Images

$ sudo docker images ls
or
$ sudo docker images ps

To find images in Docker hub

$ sudo docker search <image-name>

To download the Docker images from Docker hub

$ sudo docker pull <image-name>

To build a Image from Dockerfile

$ sudo docker build -t <image-name>:<tag-name> .

To check the history of Image

$ sudo docker history <image-name>

To Export Docker Image

$ sudo docker (image-name) >(filename).rar

To Import Docker Images

$ sudo docker <filename.rar> <image-name>

To Push docker image in Docker hub

$ sudo docker push <registory-name>/<username>/<image-name>

To Remove docker Images

$ sudo docker rmi <image-name>

To Remove docker images forcefully

$ sudo docker rmi -f <image-name>

To Remove all docker images

$ docker rmi $(docker images -q)

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon