About Docker
Index
Docker Overview
Docker is a platform for developing, shipping, and running applications inside containers. Containers are lightweight, standalone, executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, system libraries, and settings.
Benefits of using Docker:
- Consistency: Ensures your application runs the same way in any environment (development, testing, production).
- Portability: Easily move applications between different infrastructures.
- Isolation: Containers isolate applications from each other and the host system, improving security and stability.
- Resource Efficiency: Containers share the host OS kernel, making them lightweight and efficient.
Core Concepts:
- Images: Read-only templates that contain the instructions for creating a container.
- Containers: Runnable instances of an image.
- Dockerfile: A text file that contains the instructions for building a Docker image.
- Docker Hub: A public registry for sharing Docker images.
Install docker & docker compose
Here’s how to install Docker and Docker Compose on different operating systems:
Ubuntu:
- Update the package index:
sudo apt update
- Install Docker:
sudo apt install docker.io
- Verify the installation:
sudo docker run hello-world
- Install Docker Compose:
sudo apt install docker-compose
- Verify the installation:
docker-compose --version
macOS:
- Download Docker Desktop from https://www.docker.com/products/docker-desktop
- Install Docker Desktop.
- Docker Compose is included with Docker Desktop.
Windows:
- Download Docker Desktop from https://www.docker.com/products/docker-desktop
- Install Docker Desktop.
- Docker Compose is included with Docker Desktop.
For more detailed instructions, refer to the official Docker documentation: https://docs.docker.com/get-docker/
Get Started
Let’s run a simple “Hello World” container:
Create a file named
hello.txt
with the following content:1
Hello, world!
Build a Docker image from a Dockerfile:
Create a file named
Dockerfile
(without any extension) with the following content:1
2
3FROM alpine:latest
COPY hello.txt /
CMD ["cat", "/hello.txt"]Build the image:
1
docker build -t hello-world .
Run the container:
1
docker run hello-world
This will print “Hello, world!” to the console.
This example demonstrates the basic workflow of building and running a Docker container.
Static page with docker
Here’s how to create a Dockerfile for a static website:
Create a directory for your static website:
1
2mkdir my-static-website
cd my-static-websiteCreate an
index.html
file:1
2
3
4
5
6
7
8
9
<html>
<head>
<title>My Static Website</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>Create a
Dockerfile
:1
2FROM nginx:latest
COPY index.html /usr/share/nginx/html/Build the image:
1
docker build -t my-static-website .
Run the container:
1
docker run -d -p 80:80 my-static-website
Now you can access your website at
http://localhost
.
Springboot with docker
Here’s how to dockerize a Spring Boot application:
Create a Spring Boot application (if you don’t have one already).
Create a
Dockerfile
:1
2
3
4
5FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]Build the Spring Boot application:
1
mvn clean install
Build the Docker image:
1
docker build -t my-springboot-app .
Run the container:
1
docker run -d -p 8080:8080 my-springboot-app
Now you can access your Spring Boot application at
http://localhost:8080
.