Docker is an open-source platform that automates the process of building, deploying, and managing applications within lightweight, portable containers. Containers encapsulate an application and its dependencies, ensuring consistency across different environments and simplifying the deployment process.
- Containers: Lightweight, standalone, and executable software packages that include everything needed to run an application—code, runtime, libraries, and system tools.
- Images: Read-only blueprints for containers that include the application code, libraries, and environment variables. Built from a Dockerfile.
- Dockerfile: A text document with instructions to create a Docker image, defining the base image, application code, dependencies, and setup commands.
- Docker Engine: The core component that runs and manages Docker containers, consisting of a server, REST API, and command-line interface (CLI).
- Docker Compose: A tool for defining and running multi-container Docker applications using a YAML file to configure services, networks, and volumes.
- Portability: Consistent behavior across development, testing, and production environments.
- Isolation: Enhanced security and prevention of conflicts between applications.
- Efficiency: Lightweight containers with reduced resource overhead and improved startup times.
- Scalability: Easy horizontal scaling with multiple container instances.
- Development: Consistent environments across the software lifecycle.
- CI/CD: Automated and reproducible build, test, and deployment processes.
- Microservices: Manage microservices architectures with multiple containers.
- Legacy Application Modernization: Simplify migration and integration of legacy applications.
To initiate a Docker session, use the following command:
sudo service docker startTo check the installed Docker version, run:
docker -vList Docker Images:
docker imagesPull Docker Images from Docker Hub:
docker pull [Image-Name]List Running Containers:
docker psList All Containers (including stopped ones):
docker ps -aRun a New Container:
docker run [options] [Image-Name]Example of running with environment variables and detached mode:
docker run --env MYSQL_ROOT_PASSWORD=my-secret-pw -d --name mysql-container mysqlInteractive Mode and Detached Mode:
-
Run a container interactively and in detached mode:
docker run --name SampleName -it -d [Image-Name]
Accessing a Running Container:
-
To execute commands inside a running container:
docker exec -it [container-name/id] /bin/bash -
Stopping a Running Container:
docker stop [container-name/id]
Removing Containers:
-
Remove All Containers:
docker rm -a
-
Remove a Specific Container:
docker rm [container-id]
Remove a Docker Image:
docker rmi [image-name]To restart a container:
docker restart [container-name]Docker Login:
docker loginDocker Commit (Create a New Image from a Container):
docker commit [container-id] [new-image-name]Push Image to Docker Hub:
docker push [image-name]
To view logs of a container:
docker logs [container-name]To manage Docker volumes (persistent storage for containers):
docker volume [options]
Step 1: Create a file named Dockerfile with the following content:
FROM ubuntu
MAINTAINER hardik
RUN apt update
CMD ["echo", "this is my first image"]Step 2: Build the Docker image:
docker build -t myubuntuimage .Step 3: List Docker images to verify:
docker imagesStep 4: Run the created image:
docker run myubuntuimageStep 1: Create a file named Dockerfile with the following content:
FROM openjdk:11
WORKDIR /usr/src/myapp
COPY . /usr/src/myapp/
RUN javac Test.java
CMD ["java", "Test"]Create a file named Test.java with the following content:
import java.util.Properties;
public class Test {
public static void printSystemProperties() {
System.out.println("Printing system properties:");
Properties props = System.getProperties();
System.out.println(props);
}
public static void main(String[] args) {
System.out.println("Java program started..");
printSystemProperties();
}
}Step 2: Build the Docker image:
docker build -t myjavaimage .Step 3: Run the created Java image:
docker run --name javaProject2 myjavaimage