Mastering the Art of Creating Containers

Mastering the Art of Creating Containers

In today’s tech-driven world, containers have become the backbone of modern software development. But what exactly are containers, and how do you create one? Let’s break it down into clear, actionable steps.

What is a Container?

A container is a lightweight, standalone, and executable software package that includes everything your application needs to run—code, runtime, libraries, and dependencies. Containers ensure that your application works consistently across various environments, from development to production.

Command to create a container:

docker run -itd --name myfirst-container -p 1234:80 nginx

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

docker run is a CLI command used to create and start the container using docker image

-it : represents interactive terminal, This is particularly useful when the container needs input from the user or when you want to interact with processes running inside the container. and this will helps user to interact with bash shell inside the container.

-d : stands for detached mode in Docker. It runs a container in the background, freeing up your terminal so that you can continue using it for other tasks. It is used to get the logs from our containers.

--name flag represents the container-name

-p : used for publishing the port number to access the application which are running inside the container. In the above command we have 2 ports

  • 1234 : this represents the host port which is used to access the application through internet. This could be anything like we can give any number instead of 1234.

  • 80 : this is container port which depends on image.

    • For Example if we use httpd image then container port is 80

    • if we use jenkins image then container port is 8080

    • for nexus image the container port is 8081

    • for tomcat image the container port is 8080

    • for sonar image the container port is 9000

nginx : is a webserver image, we can keep our application image here.

When to use ITD

ScenarioFlags to UseWhy?
Running a background service-dKeeps the container running without blocking the terminal. Ex: database and app continers
Debugging or manual interaction-itAllows real-time interaction with the container's shell.
Long-running service with interaction-itdBackground service that you may want to interact with later.
Quick commands or batch jobsNo -i, -t, or -dRuns the container, executes the command, and exits.

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 mustafa -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.

Give me your heart 💖

If you found this blog helpful for your interviews or in learning Docker troubleshooting, please hit a heart for 10 times and drop a comment! Your support motivates me to create more content on DevOps and related topics. ❤️