Container Lifecycle Investigation
Time: 30-45 minutes
Type: Independent exploration
Deliverable: This completed worksheet with your answers
Instructions
You've run containers in Lab 1 and Lab 2. Now it's time to really understand how the container lifecycle works.
Rules:
- Answer each question by running commands and observing the results
- Write your answers in your own words (no copy-paste from docs)
- Include the commands you ran to discover the answer
- Mark any surprises or "aha!" moments with ๐ก
You'll learn more from being wrong and figuring out why than from being right immediately.
Part 1: Understanding Container States
Question 1.1: Stop vs Start vs Restart
Start an nginx container, then stop it, then start it again.
Question: Did you use the same container or create a new one? How can you tell?
Commands I ran:
# Your commands hereMy answer:
๐ก Aha moment (if any):
Question 1.2: Stop vs Kill
Question: What's the difference between docker stop and docker kill?
Experiment:
- Run two nginx containers
docker stoponedocker killthe other- Check their exit codes
Commands I ran:
# Your commands hereMy answer:
Exit codes I observed:
Question 1.3: Created But Not Running
Question: Create a container but don't start it (hint: docker create). What state is it in? Can you exec into it? Why or why not?
Commands I ran:
# Your commands hereMy answer:
Question 1.4: Paused Containers
Question: Pause a running nginx container. Can you still see it in docker ps? What happens if you try to curl it? What about docker exec?
Commands I ran:
# Your commands hereMy answer:
What worked / what didn't:
Part 2: Data Lifecycle
Question 2.1: Survived Stop?
Experiment:
- Start nginx
- Exec in and create a file:
echo "test" > /tmp/myfile.txt - Stop the container
- Start it again
- Exec in and check if the file exists
Question: Is the file still there after stop/start? Why or why not?
Commands I ran:
# Your commands hereMy answer:
Question 2.2: Survived Remove?
Experiment:
- Start nginx
- Exec in and create a file:
echo "test" > /tmp/myfile.txt - Stop the container
- Remove the container (
docker rm) - Start a new container from the same image
- Check if the file exists
Question: Is the file still there after rm + new container? Why or why not?
Commands I ran:
# Your commands hereMy answer:
๐ก What does this tell you about the difference between containers and images?
Question 2.3: Crashed Container Logs
Experiment:
Run a container that crashes immediately:
docker run --name crasher nginx /bin/falseQuestions:
- What state is the container in?
- Can you still get its logs?
- Can you restart it?
Commands I ran:
# Your commands hereMy answers:
Part 3: Lifecycle Commands
Question 3.1: Restart Running Container
Question: What happens if you docker restart a container that's already running? Does it get a new container ID?
Commands I ran:
# Your commands hereMy answer:
Question 3.2: Renaming Containers
Questions:
- Can you rename a running container?
- Can you rename a stopped container?
- If you start a container without
--name, can you still rename it?
Commands I ran:
# Your commands hereMy answers:
Question 3.3: The --rm Flag Mystery
Experiment:
- Start a container with the
--rmflag - Stop it
- Try to find it with
docker ps -a
Question: Where did it go? When is --rm useful?
Commands I ran:
# Your commands hereMy answer:
Part 4: The Image-Container Relationship
Question 4.1: Deleting Images
Experiment:
- Run 2 containers from nginx (let them keep running)
- Try to delete the nginx image
Question: What happens? Why?
Commands I ran:
# Your commands hereMy answer:
Now stop and remove all containers, then try deleting the image. What happens?
Question 4.2: Container Independence
Experiment:
- Run 3 nginx containers from the same image
- Exec into container 1 and create
/tmp/file1.txt - Exec into container 2 and create
/tmp/file2.txt - Exec into container 3 and check what files exist in
/tmp/
Question: Can container 3 see file1.txt and file2.txt? Why or why not?
Commands I ran:
# Your commands hereMy answer:
๐ก What does this tell you about container isolation?
Question 4.3: Image Immutability
Experiment:
- Run nginx
- Exec in and modify
/usr/share/nginx/html/index.html - Stop and remove the container
- Run a new nginx container
- Check if your modifications are still there
Question: Are your changes in the new container? What happened to them?
Commands I ran:
# Your commands hereMy answer:
How would you make permanent changes to an image?
Part 5: Real-World Debugging Scenario
Question 5.1: The Crashing Container
Scenario: You deployed a container in production. It crashes every 30 seconds (exits with code 1). You need to debug it, but it keeps restarting before you can exec in.
Experiment:
Simulate this with:
docker run -d --name broken --restart always nginx /bin/falseWatch it crash and restart. Now try to exec into it.
Question: How do you keep it alive long enough to investigate? (Try at least 2 different approaches)
Commands I tried:
# Your experiments hereMy solution(s):
๐ก Which approach would you use in production and why?
Question 5.2: Container Resource Detective
Experiment:
- Run an nginx container
- Use
docker statsto see its resource usage - Use
docker inspectto see its configuration
Questions:
- How much memory is it using right now?
- What's its IP address?
- What ports are exposed?
- What's its restart policy?
Commands I ran:
# Your commands hereMy answers:
Part 6: Lifecycle Troubleshooting
Question 6.1: Port Already in Use
Experiment:
- Start nginx on port 8080
- Try to start another nginx on port 8080
Question: What error do you get? How would you find what's using port 8080 if you didn't know?
Commands I ran:
# Your commands hereMy answer:
How to fix it:
Question 6.2: Container Name Conflicts
Experiment:
- Run a container named "test"
- Stop it (don't remove)
- Try to run another container named "test"
Question: What happens? How do you fix it?
Commands I ran:
# Your commands hereMy answer:
Question 6.3: Finding Lost Containers
Scenario: You ran several containers yesterday during testing. Some are stopped, some are running. You can't remember their names.
Questions:
- How do you find ALL containers (running and stopped)?
- How do you find just the stopped ones?
- How do you remove all stopped containers at once?
Commands I ran:
# Your commands hereMy answers:
Final Reflection
What surprised you most about container lifecycle?
My answer:
What's one thing you'll do differently now when working with containers?
My answer:
If you had to explain the difference between an image and a container to someone new, how would you explain it?
My answer:
Cleanup
Before submitting, clean up your test containers:
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker system prune -fSubmission
Save this file as YOURNAME-lifecycle-investigation.md and submit according to your instructor's directions.
Bonus Challenges (Optional)
If you finish early:
-
The Zombie Hunter: What's the difference between
docker psanddocker ps -a? Why would stopped containers stick around? -
The Network Detective: Two containers running on the same host. Can they talk to each other? How?
-
The Volume Mystery: Create a container with
-v /data. Put a file in /data. Remove the container. Where did the file go?