DEV Community

Harsh Pandhe
Harsh Pandhe

Posted on

3

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!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay