Skip to main content

Command Palette

Search for a command to run...

Docker Images And Containers along with important Docker commands

Updated
4 min read

In the previous blog, we covered the fundamentals of Docker and its architecture. Now, let's dive into Docker images and containers and explore some basic Docker commands.

What is image?

  • A Docker Image is a read-only template used to create Docker containers. It contains everything needed to run an application, including the application code, runtime environment, system libraries, and dependencies.

  • Docker images act as the blueprint for containers. When you run an image, Docker creates a container from that image.

  • Images can be downloaded from Docker Hub or created using a Dockerfile.

What Does a Docker Image Contain?

A Docker image typically includes:

  • Application code

  • Runtime (e.g., Java, Python, Node.js)

  • Libraries and dependencies

  • Environment configurations

  • System tools

This ensures the application runs the same way in any environment.

What is Container?

A Docker Container is a lightweight, standalone, and executable environment that runs an application along with all the necessary dependencies, libraries, and configurations required for it to work.

Containers are created from Docker Images and provide an isolated environment where applications can run consistently across different systems.

Command to create a container:

docker run -itd --name myfirstcontainer -p 1234:80 nginx

The above command is used to create a basic image. Lets breakdown it to understand the command

The command docker run is a CLI command used to create and start a container using a Docker image. -it: Represents an interactive terminal. This is particularly useful when the container requires user input or when you want to interact with processes running inside the container. It allows users to interact with the bash shell inside the container. -d: Stands for detached mode in Docker. It runs a container in the background, freeing up your terminal for other tasks. It is used to retrieve logs from our containers. --name: Specifies the container name. -p: Used for publishing the port number to access applications running inside the container. In the command above, there are two ports: 1234: Represents the host port used to access the application over the internet. This number can be changed to any desired value. 80: The container port, which depends on the image. For example, if using an httpd image, the container port is 80. For a Jenkins image, it's 8080. For a Nexus image, it's 8081. For a Tomcat image, it's 8080. For a Sonar image, it's 9000. nginx: Is a web server image; you can replace it with your application image here.

Basic Docker commands:

  • To install docker in Linux : yum install docker -y

  • To see the docker version : docker --version

  • To start the docker service : service docker start

  • To check service is start or not : service docker status

  • To check the docker information : docker info

  • To see all images in local machine : docker images

  • To find images in docker hub : docker search image name

  • To download image from docker hub to local : docker pull image name

  • To give names of a container : docker run -itd --name janaki -p 1234:80 image

  • To start container : docker start container-name/id

  • To go inside the container : docker attach container-name

  • To see all the details inside container : cat /etc/os-release

  • To get outside of the container : exit

  • To see all containers : docker ps -a

  • To see only running containers : docker ps (ps: process status)

  • To see only exited containers: docker ps -q -f "state=exited"

  • To stop the container : docker stop container name

  • To delete container : docker rm container name

  • To stop all the containers : docker stop $(docker ps -a -q)

  • To delete all the stopped containers : docker rm $(docker ps -a -q)

  • To delete all images : docker rmi -f $(docker images -q)

In the next blog, we are going to explore about Dockefile which is very essential part to create the images and containers.

Conclusion

Creating containers is a fundamental skill in modern software development. By mastering the essentials and following a systematic approach, you can build applications that are portable, consistent, and efficient. Whether you’re a seasoned developer or new to DevOps, understanding container creation is a key step in optimizing your development workflow.

1 views