Skip to content

Commit 67282c3

Browse files
committed
feature: added support for running sample code using Docker and docker-compose
1 parent d682dd4 commit 67282c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1761
-7
lines changed

.gitignore

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ htmlcov/
6262
coverage.xml
6363
*.cover
6464

65-
# dotenv environment files
66-
.env.local
67-
.env.*
68-
.env
69-
!.env.example
70-
7165
# Ignore generated files and backups
7266
*.orig
7367
*.rej
@@ -82,3 +76,15 @@ public/
8276
# Ignore personal notes or drafts
8377
private-notes/
8478
drafts/
79+
80+
# Docker
81+
.docker-env
82+
.docker-cache
83+
docker-compose.override.yml
84+
85+
# Environment variables
86+
.env.local
87+
.env.*
88+
.env
89+
!.env.example
90+
docker/environments/databases/.env

Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.PHONY: build run-snippet help db-start db-stop db-run-snippet
2+
3+
help:
4+
@echo "Tech Notes Hub Docker Environment"
5+
@echo "================================="
6+
@echo ""
7+
@echo "Usage:"
8+
@echo " make build Build all Docker environments"
9+
@echo " make run-snippet FILE=path Run a specific code snippet"
10+
@echo " make db-start DB=type Start a specific database (mysql, postgres, mongodb, redis, sqlite)"
11+
@echo " make db-stop DB=type Stop a specific database"
12+
@echo " make db-run-snippet DB=type FILE=path Run a code snippet with database connection"
13+
@echo " make help Show this help message"
14+
15+
build:
16+
docker-compose build
17+
18+
run-snippet:
19+
@if [ -z "$(FILE)" ]; then \
20+
echo "Error: Please specify a file path. Example: make run-snippet FILE=snippets/algorithms/graph-traversal/graph_traversal.py"; \
21+
exit 1; \
22+
fi
23+
./docker/run-snippet.sh $(FILE)
24+
25+
db-start:
26+
@if [ -z "$(DB)" ]; then \
27+
echo "Error: Please specify a database type. Example: make db-start DB=mysql"; \
28+
exit 1; \
29+
fi
30+
docker-compose -f docker/environments/databases/docker-compose.yml up -d $(DB)
31+
32+
db-stop:
33+
@if [ -z "$(DB)" ]; then \
34+
echo "Error: Please specify a database type. Example: make db-stop DB=mysql"; \
35+
exit 1; \
36+
fi
37+
docker-compose -f docker/environments/databases/docker-compose.yml stop $(DB)
38+
39+
db-run-snippet:
40+
@if [ -z "$(DB)" ]; then \
41+
echo "Error: Please specify a database type. Example: make db-run-snippet DB=mysql FILE=path/to/file.py"; \
42+
exit 1; \
43+
fi
44+
@if [ -z "$(FILE)" ]; then \
45+
echo "Error: Please specify a file path. Example: make db-run-snippet DB=mysql FILE=path/to/file.py"; \
46+
exit 1; \
47+
fi
48+
./docker/run-db-snippet.sh $(DB) $(FILE)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ Simply browse the folders or use GitHub's search feature to find the topic or pa
5353

5454
**For a complete table of contents with all available notes and resources, check out the [SUMMARY.md](SUMMARY.md) file.**
5555

56+
### 🐳 Docker Environments
57+
58+
This repository includes Docker configurations for running code snippets in various programming languages. To run a code snippet:
59+
60+
```bash
61+
./docker/run-snippet.sh snippets/path/to/your/snippet.py
62+
```
63+
64+
For more information on Docker usage, see the [Docker README](docker/README.md).
65+
5666
## 🤝 Contribution
5767

5868
Contributions are highly welcome! If you want to:

README_vi.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ Mỗi ghi chú đều độc lập, bao gồm lý thuyết và mã ví dụ th
5454

5555
**Để xem danh mục đầy đủ với tất cả các ghi chú và tài nguyên có sẵn, hãy xem file [SUMMARY.md](SUMMARY.md).**
5656

57+
### 🐳 Môi Trường Docker
58+
59+
Kho lưu trữ này bao gồm cấu hình Docker để chạy đoạn mã trong nhiều ngôn ngữ lập trình khác nhau. Để chạy một đoạn mã:
60+
61+
```bash
62+
./docker/run-snippet.sh snippets/path/to/your/snippet.py
63+
```
64+
65+
Để biết thêm thông tin về cách sử dụng Docker, hãy xem [Docker README](docker/README_vi.md).
66+
5767
## 🤝 Đóng góp
5868

5969
Mọi đóng góp đều rất hoan nghênh! Nếu bạn muốn:

docker-compose.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
version: '1.0'
2+
3+
services:
4+
# Python environment
5+
python:
6+
build:
7+
context: .
8+
dockerfile: docker/environments/python/Dockerfile
9+
volumes:
10+
- ./snippets:/app/snippets
11+
working_dir: /app
12+
command: ["--version"]
13+
14+
# JavaScript/Node.js environment
15+
javascript:
16+
build:
17+
context: .
18+
dockerfile: docker/environments/javascript/Dockerfile
19+
volumes:
20+
- ./snippets:/app/snippets
21+
working_dir: /app
22+
command: ["--version"]
23+
24+
# Java environment
25+
java:
26+
build:
27+
context: .
28+
dockerfile: docker/environments/java/Dockerfile
29+
volumes:
30+
- ./snippets:/app/snippets
31+
working_dir: /app
32+
command: ["/bin/bash", "-c", "java -version"]
33+
34+
# C/C++ environment
35+
cpp:
36+
build:
37+
context: .
38+
dockerfile: docker/environments/cpp/Dockerfile
39+
volumes:
40+
- ./snippets:/app/snippets
41+
working_dir: /app
42+
command: ["/bin/bash", "-c", "g++ --version"]
43+
44+
# Go environment
45+
go:
46+
build:
47+
context: .
48+
dockerfile: docker/environments/go/Dockerfile
49+
volumes:
50+
- ./snippets:/app/snippets
51+
working_dir: /app
52+
command: ["version"]
53+
54+
# Rust environment
55+
rust:
56+
build:
57+
context: .
58+
dockerfile: docker/environments/rust/Dockerfile
59+
volumes:
60+
- ./snippets:/app/snippets
61+
working_dir: /app
62+
command: ["/bin/bash", "-c", "rustc --version"]
63+
64+
# PHP environment
65+
php:
66+
build:
67+
context: .
68+
dockerfile: docker/environments/php/Dockerfile
69+
volumes:
70+
- ./snippets:/app/snippets
71+
working_dir: /app
72+
command: ["--version"]
73+
74+
# C# environment
75+
csharp:
76+
build:
77+
context: .
78+
dockerfile: docker/environments/csharp/Dockerfile
79+
volumes:
80+
- ./snippets:/app/snippets
81+
working_dir: /app
82+
command: ["/bin/bash", "-c", "dotnet --version"]
83+
84+
# Ruby environment
85+
ruby:
86+
build:
87+
context: .
88+
dockerfile: docker/environments/ruby/Dockerfile
89+
volumes:
90+
- ./snippets:/app/snippets
91+
working_dir: /app
92+
command: ["--version"]
93+
94+
# Shell environment for Linux and DevOps scripts
95+
shell:
96+
build:
97+
context: .
98+
dockerfile: docker/environments/shell/Dockerfile
99+
volumes:
100+
- ./snippets:/app/snippets
101+
working_dir: /app
102+
command: ["-c", "echo 'Shell environment ready'"]
103+
104+
# Databricks/PySpark environment
105+
databricks:
106+
build:
107+
context: .
108+
dockerfile: docker/environments/databricks/Dockerfile
109+
volumes:
110+
- ./snippets:/app/snippets
111+
working_dir: /app
112+
command: ["--version"]

docker/DATABASE_GUIDE.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Database Usage Guide with Docker
2+
3+
This guide helps you run code snippets with connections to databases in Docker environments.
4+
5+
## Supported Databases
6+
7+
- **MySQL** (8.0)
8+
- **PostgreSQL** (15)
9+
- **MongoDB** (6)
10+
- **Redis** (7)
11+
- **SQLite** (3.x)
12+
13+
## How to Use
14+
15+
### 1. Start a Specific Database
16+
17+
```bash
18+
# Using Docker Compose directly
19+
docker-compose -f docker/environments/databases/docker-compose.yml up -d mysql
20+
21+
# Or using the Makefile
22+
make db-start DB=mysql
23+
```
24+
25+
Valid values for DB are: `mysql`, `postgres`, `mongodb`, `redis`, `sqlite`
26+
27+
### 2. Run a Code Snippet with Database Connection
28+
29+
```bash
30+
# Using the script directly
31+
./docker/run-db-snippet.sh mysql snippets/databases/mysql_example.py
32+
33+
# Or using the Makefile
34+
make db-run-snippet DB=mysql FILE=snippets/databases/mysql_example.py
35+
```
36+
37+
### 3. Stop a Database When Not in Use
38+
39+
```bash
40+
# Using Docker Compose directly
41+
docker-compose -f docker/environments/databases/docker-compose.yml stop mysql
42+
43+
# Or using the Makefile
44+
make db-stop DB=mysql
45+
```
46+
47+
## Database Connection Information
48+
49+
When running a code snippet with a database, the following environment variables will be automatically passed to the container:
50+
51+
- `DB_HOST`: Database hostname
52+
- `DB_PORT`: Database port
53+
- `DB_USER`: Username for connection
54+
- `DB_PASS`: Password for connection
55+
- `DB_NAME`: Database name
56+
- `DB_CONN_STR`: Full connection string
57+
58+
### Default Connection Strings
59+
60+
- **MySQL**: `mysql://user:password@tech-notes-mysql:3306/tech_notes`
61+
- **PostgreSQL**: `postgresql://user:password@tech-notes-postgres:5432/tech_notes`
62+
- **MongoDB**: `mongodb://user:password@tech-notes-mongodb:27017/tech_notes`
63+
- **Redis**: `redis://tech-notes-redis:6379`
64+
- **SQLite**: `sqlite:///data/tech_notes.db`
65+
66+
### Environment Variables Configuration
67+
68+
To change the default connection information, you can create a `.env` file in the `docker/environments/databases/` directory:
69+
70+
1. Copy the `.env.example` file to `.env`:
71+
```bash
72+
cp docker/environments/databases/.env.example docker/environments/databases/.env
73+
```
74+
75+
2. Edit the `.env` file to change the connection information (usernames, passwords, database names, etc.)
76+
77+
3. When running the `run-db-snippet.sh` command or `make db-run-snippet`, the environment variables from the `.env` file will be automatically used.
78+
79+
Note: The `.env` file has been added to `.gitignore` to prevent it from being tracked by Git, ensuring sensitive information is not pushed to the repository.
80+
81+
## Example Connection Code
82+
83+
### Python with MySQL
84+
85+
```python
86+
import os
87+
import mysql.connector
88+
89+
# Get connection info from environment variables
90+
db_host = os.environ.get('DB_HOST', 'localhost')
91+
db_port = os.environ.get('DB_PORT', '3306')
92+
db_user = os.environ.get('DB_USER', 'user')
93+
db_pass = os.environ.get('DB_PASS', 'password')
94+
db_name = os.environ.get('DB_NAME', 'tech_notes')
95+
96+
# Connect to the database
97+
conn = mysql.connector.connect(
98+
host=db_host,
99+
port=db_port,
100+
user=db_user,
101+
password=db_pass,
102+
database=db_name
103+
)
104+
105+
cursor = conn.cursor()
106+
cursor.execute("SELECT * FROM users")
107+
users = cursor.fetchall()
108+
109+
for user in users:
110+
print(user)
111+
112+
conn.close()
113+
```
114+
115+
### JavaScript with MongoDB
116+
117+
```javascript
118+
const { MongoClient } = require('mongodb');
119+
120+
// Get connection string from environment variable
121+
const uri = process.env.DB_CONN_STR || 'mongodb://user:password@localhost:27017/tech_notes';
122+
123+
async function main() {
124+
const client = new MongoClient(uri);
125+
126+
try {
127+
await client.connect();
128+
const database = client.db('tech_notes');
129+
const users = database.collection('users');
130+
131+
const query = {};
132+
const cursor = users.find(query);
133+
134+
if ((await cursor.count()) === 0) {
135+
console.log("No documents found!");
136+
}
137+
138+
await cursor.forEach(user => {
139+
console.log(user);
140+
});
141+
142+
} finally {
143+
await client.close();
144+
}
145+
}
146+
147+
main().catch(console.error);
148+
```
149+
150+
## Sample Data
151+
152+
Each database has been configured with the following sample data:
153+
154+
- `users` table/collection with 3 users
155+
- `posts` table/collection with 4 posts linked to users
156+
157+
You can see the details of the sample data in the init files in the respective directory of each database.
158+

0 commit comments

Comments
 (0)