DEV Community

Madhu Prakash Behara
Madhu Prakash Behara

Posted on

Day 3: Exploring Docker's Core: Images and Containers

Introduction

Hello, fellow Docker enthusiasts! Today, we're diving into the heart of Docker: Images and Containers. These concepts are akin to recipes and meals in a kitchen, essential for mastering Docker.

Section 1: What are Docker Images?

Docker images are the blueprints for Docker applications, akin to recipe cards in cooking.

  • Creating Images with a Dockerfile:
  # Example Dockerfile
  # Use an official Python runtime as a parent image
  FROM python:2.7-slim

  # Set the working directory to /app
  WORKDIR /app

  # Copy the current directory contents into the container at /app
  ADD . /app

  # Install any needed packages specified in requirements.txt
  RUN pip install --trusted-host pypi.python.org -r requirements.txt

  # Make port 80 available to the world outside this container
  EXPOSE 80

  # Define environment variable
  ENV NAME World

  # Run app.py when the container launches
  CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Docker Images and Containers: A Quick Guide

Section 1: Exploring Docker Images

What are Docker Images?
Snapshots of environments that contain everything needed to run an application.
Include code, libraries, dependencies, and configuration files.
Using Images from Docker Hub:
Access a vast repository of pre-built images at Docker Hub: https://hub.docker.com/
Download an image using:

docker pull [image_name]
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more

Section 2: Understanding Docker Containers

What are Docker Containers?
Running instances of Docker images.
Isolated environments where applications execute.
Running a Container:
Start a container with:

docker run -p 4000:80 [image_name]
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more

Section 3: Managing Docker Containers

Listing Running Containers:

docker ps
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more
Stopping a Container:

docker stop [container_id]
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more
Viewing Container Logs:

docker logs [container_id]
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more
Entering a Running Container:

docker exec -it [container_id] /bin/bash
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more

Section 4: Building Your First Docker Image

Creating a Dockerfile:
A text file with instructions to build an image.
Building the Image:

docker build -t [your_username]/[app_name] 
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more
Running a Container from Your Image:

docker run -p 4000:80 [your_username]/[app_name]
Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more

Conclusion

Docker images and containers are powerful tools for packaging, deploying, and managing applications efficiently. Experiment with them, share your experiences, and let's continue learning together!

Top comments (0)