Logo

dev-resources.site

for different kinds of informations.

Day 03: Docker Images and Containers: Building, Pulling, and Running Docker Containers

Published at
1/14/2025
Categories
webdev
docker
beginners
containerapps
Author
harshpandhe
Author
11 person written this
harshpandhe
open
Day 03: Docker Images and Containers: Building, Pulling, and Running Docker Containers

With Docker installed, you’re ready to dive into the core of containerization—working with Docker images and containers. This article will guide you through building, pulling, and running containers, helping you understand their practical use in development and deployment.


Key Concepts: Docker Images vs. Containers

Before we proceed, let’s clarify two important terms:

I. Docker Images:

  • A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software: code, runtime, libraries, and environment settings.
  • Images are immutable and serve as blueprints for creating containers.

II. Docker Containers:

  • A container is a runtime instance of a Docker image.
  • It is isolated and portable, running the application as specified in the image.

Think of an image as a recipe and a container as the dish prepared from that recipe.


Pulling a Docker Image

Docker Hub hosts thousands of pre-built images. To use an image, you first need to pull it. Here’s how:

Pulling the NGINX Image

Example: Pulling the NGINX Image

I. Open your terminal.
II. Run the following command:

   docker pull nginx
Enter fullscreen mode Exit fullscreen mode

III. Docker will fetch the latest version of the NGINX image from Docker Hub.

Pulled Image

To list all downloaded images, use:

docker images
Enter fullscreen mode Exit fullscreen mode

List of Images


Running a Container

Once you have an image, you can create and run a container from it.

Example: Running an NGINX Container

I. Use the following command:

   docker run --name my-nginx -d -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode
  • --name my-nginx: Names the container "my-nginx."
  • -d: Runs the container in detached mode.
  • -p 8080:80: Maps port 8080 on your host to port 80 in the container.

II. Open your web browser and navigate to http://localhost:8080. You should see the default NGINX welcome page.

NGINX Welcome Page

To view running containers, use:

docker ps
Enter fullscreen mode Exit fullscreen mode

docker ps


Building Your Own Docker Image

Sometimes, you’ll want to create a custom image tailored to your application. To do this, you’ll use a Dockerfile.

Example: Building a Simple Web Server Image

Docker File Structure

I. Create a Dockerfile:

   # Use an official Python runtime as a parent image
   FROM python:3.9-slim

   # Set the working directory in the container
   WORKDIR /app

   # Copy the current directory contents into the container
   COPY . /app

   # Install dependencies
   RUN pip install --no-cache-dir flask

   # Make port 5000 available to the world outside the container
   EXPOSE 5000

   # Define the command to run the application
   CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

II. Place a simple Flask app (app.py) in the same directory:

   from flask import Flask

   app = Flask(__name__)

   @app.route('/')
   def hello():
       return "Hello, Docker!"

   if __name__ == "__main__":
       app.run(host="0.0.0.0", port=5000)
Enter fullscreen mode Exit fullscreen mode

III. Build the image:

   docker build -t my-flask-app .
Enter fullscreen mode Exit fullscreen mode

Running the Container

VI. Run the container:

   docker run -d -p 5000:5000 my-flask-app
Enter fullscreen mode Exit fullscreen mode

Docker Desktop

Visit http://localhost:5000 to see your application in action.

Hello, Docker!


Managing Containers

Stopping and Removing Containers

  • Stop a container:
  docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Remove a container:
  docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

Removing Images

  • Remove an image:
  docker rmi <image_id>
Enter fullscreen mode Exit fullscreen mode

Stopping a Container


Conclusion

In this article, you’ve learned how to pull images, run containers, and even build your own custom image. These skills form the foundation of working with Docker.

In the next article, we’ll explore Docker Compose, a powerful tool for managing multi-container applications. Stay tuned!

docker Article's
30 articles in total
Favicon
Building bun-tastic: A Fast, High-Performance Static Site Server (OSS)
Favicon
Day 04: Docker Compose: Managing multi-container applications
Favicon
Infraestrutura para análise de dados com Jupyter, Cassandra, Pyspark e Docker
Favicon
Building RelaxTube: A Scalable Video Transcoding and Streaming Application
Favicon
Docker vs kubernetes
Favicon
Advanced Docker Concepts and Features
Favicon
Building and Deploying Your First Java App with Docker in Just 5 Minutes
Favicon
Realtime troubleshooting based questions docker compose
Favicon
common Docker Compose interview questions.
Favicon
[Boost]
Favicon
Docker scenario
Favicon
TOP 10 TYPES OF DOCKER COMMANDS
Favicon
how to inspect a docker container
Favicon
How to run a Nginx-web server
Favicon
Docker article Best
Favicon
Load Balancing Node.js Applications with Nginx Upstream Configuration
Favicon
Top 20 Docker Commands with Examples and Outputs
Favicon
🌟 Deploying a Live Project Without a Dockerfile Using Buildpacks 🌟
Favicon
A Conversation with Docker CTO Justin Cormack and Flux CEO Ron Efrani: The Future of Developer Environments
Favicon
Understanding Node.js Alpine Versions: A Lightweight Choice for Your Projects
Favicon
Day 03: Docker Images and Containers: Building, Pulling, and Running Docker Containers
Favicon
How would you optimize the performance of Docker containers, particularly in resource-constrained environments ?
Favicon
Docker in development: Episode 4
Favicon
Building a Local S3 Environment with MinIO: AWS SDK for Java V2 Migration Guide
Favicon
Node.js Meets PostgreSQL and MongoDB in Docker: Docker Diaries
Favicon
How to Use Docker in Your Data Science Projects: A Complete Guide
Favicon
Bringing Together Containers & SQL
Favicon
🚀 Are Your Docker Containers Secure? 🛡️ In this article, learn 10 actionable strategies to protect your Docker containers like a pro
Favicon
Running PostgreSQL, MongoDB, and NestJS concurrently with Docker Compose
Favicon
Creando un notebook con Jupyter y Kotlin

Featured ones: