Docker Images and How to create

Docker Images and How to create

A Docker image is a lightweight, standalone, and immutable file that contains the source code, libraries, dependencies, tools, and other files needed to run an application. Essentially, a Docker image serves as a template for creating Docker containers. When you execute a Docker image, it becomes a container that can be run on any system that supports Docker.

Docker Images

Key Characteristics of Docker Images

1. Layered Structure

   Docker images are built in layers, where each layer represents a set of changes or instructions in the Dockerfile (the script used to build the image). For example, one layer might contain the base operating system, another might include installed libraries, and yet another might have the application code.

The layered structure makes images efficient. If multiple images share the same base layers, Docker can reuse those layers rather than duplicating them, saving space and improving performance.

2. Immutable

  Once created, a Docker image is immutable, meaning it cannot be changed. Any modifications require creating a new image by adding new layers on top of the existing ones.

3. Portability

   Docker images are portable and can be shared across different environments. You can build an image on one machine, push it to a Docker registry (like Docker Hub), and pull it down on another machine to create a container. This ensures consistency across development, testing, and production environments.

4. Versioning

   Docker images can be versioned using tags. Tags allow you to specify different versions of the same image, such as `my-app:1.0`, `my-app:latest`, etc. This helps manage different versions of an application easily.

How to Create a Docker Container from an Image

Creating a Docker container from an image is straightforward. You use the `docker run` command, which pulls the image from a local repository (or Docker Hub if not available locally) and creates a container from it.

Steps to Create a Docker Container:

Example
1. Pull or Build a Docker Image:
   - You can either pull an existing image from Docker Hub or build your own image using a Dockerfile.
   - To pull an existing image:
     ```sh
     docker pull ubuntu
     ```
   - To build your own image from a Dockerfile:
     ```sh
     docker build -t my-app .
     ```	

Example
2. Run the Docker Container:
   - Use the `docker run` command to create and start a container from the image:
     ```sh
     docker run -d --name my-container my-app
     ```
   - The `-d` flag runs the container in detached mode (in the background), and `--name` assigns a name to the container.

Example
3. Check Running Containers:
   - To see the list of running containers, use:
     ```sh
     docker ps
     ```

4. Access the Container:
   - You can use the `docker exec` command to run commands inside a running container:
     ```sh
     docker exec -it my-container /bin/bash
     ```

5. Stop and Remove the Container:
   - To stop and remove the container:
     ```sh
     docker stop my-container
     docker rm my-container
     ```

Example
Java Example using Docker Image and Container
Let's demonstrate creating a Docker image for a Java application and then creating a container from that image.

1. Java Application:

```java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Docker World!");
    }
}
```

Example
2. Create a Dockerfile:

```Dockerfile
 Use an official OpenJDK runtime as the base image
FROM openjdk:17-jdk-alpine

 Set the working directory inside the container
WORKDIR /app

 Copy the compiled Java class to the container
COPY HelloWorld.class /app/HelloWorld.class

 Specify the command to run the application
CMD ["java", "HelloWorld"]
```
Example
3. Build the Docker Image:

```sh	
docker build -t my-java-app .
```

This command creates a Docker image named `my-java-app`.

Example
4. Create and Run the Docker Container:

```sh
docker run --name my-java-container my-java-app
```

This command creates and starts a container named `my-java-container` from the `my-java-app` image. The application will run, and you'll see the output:

```sh
Hello, Docker World!
```

Conclusion:

Docker images are the building blocks of Docker containers. They encapsulate all the necessary components to run an application in a consistent and portable manner. Once you have an image, creating a container is as simple as running a single command.

Homepage

Readmore