Docker Lifecycle: From Pulling Images to Terminating Containers

Sandhya BabuSandhya Babu
4 min read

Imagine Docker containers as magical boxes that can be created, used, paused, and discarded at will. These containers are crucial for deploying and managing applications efficiently. But how does this magic happen behind the scenes?

In this guide, we'll explore the various stages a Docker container goes through, each crucial for its effective management and operation

Docker Container Lifecycle Management

Hands-on Practice:

1. Checking and Pulling an Image

docker pull httpd
Explanation*:* Docker will check for the HTTP image cache on the local machine, if not found, it will automatically pull the image from the Docker hub.

docker image ls
Explanation*:* Lists all images in the local repository.

docker image history httpd
Explanation*:* Displays the history of the httpd image, including layers and changes.

2. Creating and Managing Containers

docker container create httpd
Explanation*:* Creates a new container from the httpd image but does not start it.

docker container ls -a
Explanation*:* Lists all containers, including those that are not running.

Starting and Stopping Containers

docker container start <replace container id/name>
Explanation*:* Starts a container using its ID or name.

docker container ls
Explanation*:* Lists all running containers. This is the modern equivalent of docker ps.

docker container stop <replace container id/name>
Explanation*:* Stops a running container using its ID or name.

docker container ls -a
Explanation*:* Lists all containers, including those that are stopped. This is the modern equivalent of docker ps -a

3. Pausing and Unpausing Containers

docker container pause <replace container id/name>
Explanation*:* Pauses a running container, which stops its processes without removing it.

docker container unpause <replace container id/name>
Explanation*:* Unpauses a previously paused container, resuming its processes.

4. Accessing and Modifying Containers

docker exec -it <replace container id/name> bash
Explanation: Opens a new shell inside the running container for interaction.

cd htdocs
Explanation: Changes the directory to htdocs within the container.

apt update && apt install wget -y
Explanation: Updates package information and installs the wget tool inside the container.

rm index.html
Explanation: Removes the index.html file inside the container.

wget https://s3.ap-south-1.amazonaws.com/files.cloudthat.training/devops/docker-essentials/index.html
Explanation
: Downloads a file from the specified URL into the container.

exit
Explanation: Exits the shell session inside the container.

5. Committing Changes and Running Containers

docker commit <replace container id/name> myhttpd:version1
Explanation: Creates a new image (myhttpd:version1) from the modified container.

docker image ls
Explanation: Lists all images, including the newly created myhttpd:version1.

docker run -d -p 8080:80 myhttpd:version1
Explanation: Runs the myhttpd:version1 image in detached mode with port mapping from host port 8080 to container port 80.

curl <public IP>:8080
Explanation: Checks the running container's service by making a request to the public IP and port 8080

The web page "Welcome to CloudThat Training" is hosted from the new index.html file we downloaded using wget.

6. Monitoring and Resource Management

docker logs <replace container id/name>
Explanation: Shows logs for the specified container, providing output and errors.

docker stats <replace container id/name>
Explanation: Displays resource usage statistics for the specified container.

7. Stopping and Removing Containers and Images

docker stop <replace container id/name>
Explanation: Stops a running container.

docker container rm <replace container id/name>
Explanation: Removes a stopped container from the system.

docker image ls
Explanation: Lists all images, including those available after removing containers.

docker image rm <replace image id/name>
Explanation: Removes the specified image from the local repository.

docker image ls
Explanation: Lists all images to confirm the removal of the specified image.

docker image ls -a
Explanation: Lists all images, including intermediate and dangling images.

Wrapping Up the Docker Lifecycle

Understanding the Docker lifecycle is key to effective container management. We've covered how to pull images, create and manage containers, and clean up by stopping and removing them. With these commands, you’re equipped to streamline your Docker workflows and maintain control over your containerized applications. Keep experimenting and mastering Docker to enhance your development and deployment processes.

1
Subscribe to my newsletter

Read articles from Sandhya Babu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandhya Babu
Sandhya Babu

🌟 Aspiring DevOps Engineer | Cloud & DevOps Enthusiast🌟 Hello! I’m Sandhya Babu, deeply passionate about DevOps and cloud technologies. Currently in my exploring phase, I’m learning something new every day, from tools like Jenkins, Docker, and Kubernetes to the concepts that drive modern tech infrastructures. I have hands-on experience with several Proof of Concept (POC) projects, where I've applied my skills in real-world scenarios. I love writing blogs about what I've learned and sharing my experiences with others, hoping to inspire and connect with fellow learners. With certifications in Azure DevOps and AWS SAA-C03, I’m actively seeking opportunities to apply my knowledge, contribute to exciting projects, and continue growing in the tech industry. Let’s connect and explore the world of DevOps together!