DEV Community

Cover image for Lifecycle of a Container
Arif Hossain
Arif Hossain

Posted on

2

Lifecycle of a Container

The Lifecycle of a Container

This document outlines the lifecycle of a Docker container, from creation to deletion, using the example of a container named "percy."

1. Creating and Starting a Container
To create and start a Docker container, use the docker run command. This example names the container "percy" and starts an interactive bash shell within an Ubuntu container:

docker run --name percy -it ubuntu:latest /bin/bash

Image description

Once the command is executed, we are placed inside the container shell:

root@ubuntu:/#

2. Writing Data to the Container
Within the container, we can write data to the filesystem. The following commands navigate to the /tmp directory, create a new file, and verify its contents:

cd /tmp

ls -l

Now, create a file named newfile in this directory:

echo "This is the file about container lifecycle" > newfile
Enter fullscreen mode Exit fullscreen mode

ls -l

View the data of the file using:

cat newfile

3. Stopping the Container
Press Ctrl-PQ to exit the container without killing it. To stop the container, use the docker stop command:

docker stop percy

Stopping a container is like putting it on vacation—it stops running but retains its data and configuration.

4. Listing Containers
To verify the container's status, list all running containers:

docker ps

Since the container is stopped, it won't appear in the list. To show all containers, including stopped ones, use:

docker ps -a

We should see an output similar to this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b92ace4338e7 ubuntu:latest "/bin/bash" 12 minutes ago Exited (137) 26 seconds ago percy

5. Restarting the Container
Restart the stopped container with the docker start command:

docker start percy

Check the running containers again:

docker ps

Now, the container should be running again and we should see an output similar to this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b92ace4338e7 ubuntu:latest "/bin/bash" 14 minutes ago Up 4 seconds percy

6. Verifying Data Persistence
Reattach to the container to verify the data persistence:

docker exec -it percy bash

Image description
Check the file created earlier:

cd /tmp
ls -l
cat newfile

Image description

we should see an output similar to this:

This is the file about container lifecycle
This demonstrates that the data persisted even after the container was stopped and restarted.

7. Deleting the Container
Press Ctrl-PQ to exit the container without killing it. To delete the container, first stop it if it’s running:

docker stop percy

Then remove the container:

docker rm percy

Verify the container has been deleted:

docker ps -a

No containers should be listed.

Conclusion
This document outlined the lifecycle of a Docker container through the example of a container named "percy". We covered how to create, start, stop, restart, and delete a container, as well as how to verify data persistence within the container. By following these steps, we can effectively manage Docker containers and understand their lifecycle.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay