DEV Community

Cover image for ๐Ÿš€ Understanding the Docker Lifecycle with a Simple App
Latchu@DevOps
Latchu@DevOps

Posted on

๐Ÿš€ Understanding the Docker Lifecycle with a Simple App

Docker simplifies how we build, ship, and run applications by following a clear lifecycle.
Letโ€™s break it down into three main stages with an easy-to-follow example using Ubuntu and a small Python app.


1๏ธโƒฃ Create the Dockerfile

A Dockerfile is like a recipe for your application. It tells Docker:

  • Which base image to start from
  • What dependencies to install
  • What command to run when the container starts

Example:

# Start with an official Ubuntu image
FROM ubuntu:22.04

# Install Python
RUN apt-get update && apt-get install -y python3

# Copy our app to the container
COPY app.py /app/app.py

# Set working directory
WORKDIR /app

# Run the app when container starts
CMD ["python3", "app.py"]

Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Build the Docker Image

The Dockerfile is just instructions. To actually create something runnable, we build an image:

Using the Dockerfile, a Docker image is created through the docker build command. A Docker image is a lightweight, standalone, and executable package that includes everything needed to run an application, such as the runtime environment, system tools, libraries, and dependencies.

docker build -t ubuntu-simple-app .

Enter fullscreen mode Exit fullscreen mode

What happens here?

  • Docker takes the Ubuntu image
  • Installs Python
  • Copies app.py into it
  • Creates a ready-to-use image named ubuntu-simple-app

3๏ธโƒฃ Run the Docker Container

Once the image is built, a Docker container is created and executed using the docker run command. A container is an isolated environment where the application runs consistently across different systems. Containers leverage the lightweight nature of Docker images, ensuring minimal overhead and fast deployment.

docker run --name my-simple-app ubuntu-simple-app
Enter fullscreen mode Exit fullscreen mode

Example app (app.py):

print("Hello from inside my Ubuntu-based Docker container!")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello from inside my Ubuntu-based Docker container!
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Quick Recap

  1. Dockerfile โ†’ Your recipe for building the image
  2. Build Image โ†’ Turn recipe into a runnable package
  3. Run Container โ†’ Launch the app in an isolated environment

By following this lifecycle, you ensure your app runs consistently anywhere โ€” your laptop, a server, or the cloud.


๐Ÿ’ก Pro Tip:

You can share your image with others via Docker Hub:

docker tag ubuntu-simple-app your-dockerhub-username/ubuntu-simple-app
docker push your-dockerhub-username/ubuntu-simple-app
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vidakhoshpey22 profile image
Vida Khoshpey

Even though it's so hard for me but so cool ๐Ÿ˜Ž

Collapse
 
latchudevops profile image
Latchu@DevOps

It is very essential to learn when Cloud and DevOps comes