https://github.com/jdmary/docker-sample
A comprehensive Docker sample project featuring practical labs and in-depth guides to master containerization efficiently.
https://github.com/jdmary/docker-sample
docker dockerfile dockerfile-examples
Last synced: 3 months ago
JSON representation
A comprehensive Docker sample project featuring practical labs and in-depth guides to master containerization efficiently.
- Host: GitHub
- URL: https://github.com/jdmary/docker-sample
- Owner: JdMary
- Created: 2025-02-09T12:10:00.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-02-26T20:34:35.000Z (11 months ago)
- Last Synced: 2025-08-18T19:55:03.974Z (5 months ago)
- Topics: docker, dockerfile, dockerfile-examples
- Language: Dockerfile
- Homepage:
- Size: 4.35 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# docker-sample
## Diffrent methods of image creation:
1. Create an image from Scratch
2. Create an image from a Dockerfile
3. Create an image from a Container
### Creating a Docker Image FROM Scratch
Building Docker images **FROM scratch** means starting from an **empty base**, without any preinstalled OS, ensuring **lightweight, secure, and fully controlled** containerization.
#### Why use FROM scratch?
- **Minimal size**
- **Enhanced security**
- **Full control over dependencies**
#### Example: Ultra-Lightweight Docker Image
```dockerfile
FROM scratch
COPY hello /
CMD ["/hello"]
```
### Dockerfile: The Preferred Method for Building a Docker Image
A **Dockerfile** is a script that defines step by step how to build a Docker image in an **automated, efficient, and reproducible** way. It acts as a **recipe**, ensuring consistency across deployments.
#### Building an Image: `docker build`
The essential command to create a Docker image:
```
docker build -t image_name path
```
1. Define the base image
```
FROM ubuntu:20.04
```
2. Set up the environment
```
LABEL maintainer="Jrad Mariem"
ENV APP_ENV=production
WORKDIR /app
```
3. Define the startup behaviour
* The CMD command is the default command that will be executed but if an argument is entered when running the container it would ignore the CMD Statement
* The Entypoint is a base command which prioritary comparing it with CMD .
> [!NOTE]
> **`ENTRYPOINT` vs. `--entrypoint` in Docker**
> - `ENTRYPOINT` (Dockerfile) sets a fixed command that runs by default.
> ```dockerfile
> ENTRYPOINT ["ping", "-c", "4"]
> ```
> Running `docker run my-image google.com` executes: `ping -c 4 google.com`.
> - `--entrypoint` (CLI) temporarily overrides `ENTRYPOINT`.
> ```sh
> docker run --entrypoint ls my-image
> ```
> Runs `ls` instead of `ping`.
> - **Use `ENTRYPOINT`** for default behavior, **`--entrypoint`** for runtime overrides.
```
CMD ["echo", "Hello, World!"]
ENTRYPOINT ["echo"]
```
4. Run commands during build which creates a temporary container and executes the command and from it an image will be created
```
RUN apt-get update && apt-get install -y python3
```
### Create a Docker Image from a Container
Use `docker commit` to save a container’s current state as a new image.
#### Syntax
```
docker commit [container_id] [image_name:tag]
```
# Docker Swarm Setup
This README contains essential commands for setting up and managing a Docker Swarm cluster.
## 1. **Initialize Swarm**
To initialize a new Swarm on a manager node, run the following command:
```bash
docker swarm init