Docker image and container

Category : Docker | Sub Category : Docker for Java Developers | By Prasad Bonam Last updated: 2023-11-21 10:29:26 Viewed : 235


Docker image and container:


Docker Image:

Docker images are read-only templates that contain instructions for creating a containerA Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools. Images serve as a blueprint for creating Docker containers. They are built from a set of instructions specified in a file called a Dockerfile. Key characteristics of Docker images include:

  1. Immutable: Once built, a Docker image is immutable. This means that its contents cannot be changed. If modifications are needed, a new image version is created.

  2. Layered File System: Docker images are built using a layered file system. Each instruction in a Dockerfile adds a new layer to the image. Layers are cached, allowing for efficient image building and sharing of common layers across different images.

  3. Tagging: Images can be tagged with a version or label, making it easy to manage and reference different versions of an application or service.

  4. Registry: Images can be stored in registries, such as Docker Hub or private registries, making it easy to share and distribute images across different environments.

  5. Reproducibility: Docker images ensure consistency and reproducibility across different environments, making it easier to manage dependencies and application configurations.

Docker Container:

A Docker container is a lightweight, runnable instance of a Docker image. It encapsulates the application and its dependencies, providing isolation and portability across different environments. Containers run on the host machines operating system and share the same kernel with the host. Key features of Docker containers include:

  1. Isolation: Containers provide process isolation, ensuring that an application and its dependencies run independently of the host and other containers. Each container has its own filesystem, network, and process space.

  2. Portability: Containers are portable and can run consistently across different environments, such as development, testing, and production, as long as the host has the Docker runtime installed.

  3. Resource Efficiency: Containers share the host operating systems kernel, which makes them lightweight and resource-efficient. They start quickly and consume fewer resources compared to virtual machines.

  4. Lifecycle Management: Containers can be started, stopped, paused, and removed easily. They are designed to be ephemeral, and their state can be managed through images and volumes.

  5. Networking: Containers can communicate with each other and the external world through the Docker networking model. Ports can be exposed to the host or linked to other containers.

In summary, a Docker image is a packaged and immutable snapshot of an application, while a Docker container is a runtime instance of that image. Containers provide a consistent and reproducible environment for running applications, making them a powerful tool for software development, deployment, and scaling.

lets go through examples to illustrate Docker images and containers.

Docker Image Example:

Suppose you have a simple web application that prints "Hello, Docker!" when accessed. You want to package this application into a Docker image. Here is a basic example:

  1. Create a Dockerfile (Dockerfile):

    Dockerfile
    # Use an official Python runtime as a base image FROM python:3.8-slim # Set the working directory to /app 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"]
  2. Create the web application (app.py):

    python
    from flask import Flask app = Flask(__name__) @app.route(`/`) def hello(): return `Hello, Docker! `
  3. Build the Docker image:

    bash
    docker build -t my-python-app:1.0 .

    This command builds a Docker image named my-python-app with the tag 1.0 using the provided Dockerfile and application files.

Docker Container Example:

Now, lets create and run a container based on the image we just built:

  1. Run the Docker container:

    bash
    docker run -p 4000:80 my-python-app:1.0

    This command starts a container based on the my-python-app:1.0 image. It maps port 4000 on the host to port 80 on the container.

  2. Access the application:

    Open a web browser and navigate to http://localhost:4000. You should see "Hello, Docker!" displayed on the webpage.

  3. Check running containers:

    bash
    docker ps

    This command lists the running containers. You should see your container in the list.

  4. Stop the container:

    bash
    docker stop <container_id>

    Replace <container_id> with the actual container ID from the docker ps command.

In summary, the Docker image (my-python-app:1.0) is a packaged and versioned snapshot of your web application and its dependencies. The Docker container is an instance of this image, running your application in an isolated environment. Containers are portable and can run consistently across different environments, providing a reliable and reproducible way to deploy and manage applications.


Search
Related Articles

Leave a Comment: