NashTech Blog

Table of Contents
Docker Images and containers

In the ever-evolving world of software development, Docker has emerged as a revolutionary tool for building, sharing, and running applications. If you’re new to Docker, understanding the concepts of Docker Images and Containers is crucial. In this blog, we’ll break down these concepts in a straightforward manner, making it easy for beginners to grasp.

What is Docker?

Docker is an open-source platform designed to automate the deployment of applications inside lightweight, portable containers. Containers are an essential part of Docker, allowing applications to run consistently across different computing environments.

What Are Docker Images?

Docker Images are the blueprints for creating Docker Containers. Think of an image as a snapshot of a file system with everything needed to run an application, including the code, runtime, libraries, and dependencies.

Key Points about Docker Images:

  • Read-Only: Docker Images are read-only. Once an image is created, it cannot be altered.
  • Layered Architecture: Images are composed of layers, where each layer represents a change or addition. This layered approach makes Docker Images efficient and easy to share.
  • Version Control: Docker Images can be versioned, allowing developers to keep track of different versions and roll back if necessary.

How Docker Images Work:

When you build a Docker Image, you define it using a Dockerfile, a text file containing instructions for creating the image. For example, a basic Dockerfile might look like this:

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

# Set the working directory in the container
WORKDIR /app

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

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -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"]

In this Dockerfile:

  • FROM specifies the base image.
  • WORKDIR sets the working directory.
  • COPY copies files into the container.
  • RUN executes commands (like installing packages).
  • EXPOSE exposes a port.
  • ENV sets environment variables.
  • CMD specifies the command to run when the container starts.

What Are Docker Containers?

Docker Containers are the runtime instances of Docker Images. They are lightweight, portable, and isolated from the host system. Containers package up the application and all its dependencies, ensuring it runs consistently regardless of the environment.

Key Points about Docker Containers:

  1. Isolation: Containers run in isolated environments, which means the applications inside them are unaffected by the host system or other containers.
  2. Portability: Because containers include everything needed to run an application, they can be moved and executed across different systems seamlessly.
  3. Efficiency: Containers share the host system’s kernel, making them more resource-efficient compared to virtual machines.

How Docker Containers Work:

When you run a Docker Container, Docker uses the image as a template to create the container. The container starts up with the image’s file system and executes the application as defined in the CMD or ENTRYPOINT instructions.

For example, to run a container from an image named myapp, you would use the following command:

docker run -p 4000:80 myapp

In this command:

  • docker run creates and starts a new container.
  • -p 4000:80 maps port 80 in the container to port 4000 on the host system.
  • myapp is the name of the Docker Image.

Building and Managing Docker Images and Containers

Building Docker Images:

To build a Docker Image from a Dockerfile, use the docker build command. For example:

docker build -t myapp .

Here:

  • -t myapp tags the image with the name myapp.
  • . indicates the build context, which is the directory containing the Dockerfile.

Running Docker Containers:

To run a Docker Container from an image, use the docker run command. For example:

docker run -d -p 8080:80 myapp

In this command:

  • -d runs the container in detached mode (in the background).
  • -p 8080:80 maps port 80 in the container to port 8080 on the host.

Listing Docker Images and Containers:

To view the list of Docker Images, use:

docker images

To view the list of running containers, use:

docker ps

To view all containers (including stopped ones), use:

docker ps -a

Stopping and Removing Containers:

To stop a running container, use:

docker stop <container_id>

To remove a container, use:

docker rm <container_id>

To remove an image, use:

docker rmi <image_id>

Conclusion

Docker Images and Containers simplify the process of building, deploying, and running applications by providing a consistent environment across various platforms. Images act as the blueprints, while Containers are the running instances of those blueprints. With Docker, developers can create reproducible environments and streamline the development workflow.

As you become more familiar with Docker, you’ll find that these concepts become second nature. For now, understanding Docker Images and Containers is a great start to diving deeper into the world of containerisation and its benefits.

Picture of teeshajain73125e8884

teeshajain73125e8884

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top