This guide helps you run code snippets with connections to databases in Docker environments.
- MySQL (8.0)
- PostgreSQL (15)
- MongoDB (6)
- Redis (7)
- SQLite (3.x)
# Using Docker Compose directly
docker-compose -f docker/environments/databases/docker-compose.yml up -d mysql
# Or using the Makefile
make db-start DB=mysqlValid values for DB are: mysql, postgres, mongodb, redis, sqlite
# Using the script directly
./docker/run-db-snippet.sh mysql snippets/databases/mysql_example.py
# Or using the Makefile
make db-run-snippet DB=mysql FILE=snippets/databases/mysql_example.py# Using Docker Compose directly
docker-compose -f docker/environments/databases/docker-compose.yml stop mysql
# Or using the Makefile
make db-stop DB=mysqlWhen running a code snippet with a database, the following environment variables will be automatically passed to the container:
DB_HOST: Database hostnameDB_PORT: Database portDB_USER: Username for connectionDB_PASS: Password for connectionDB_NAME: Database nameDB_CONN_STR: Full connection string
- MySQL:
mysql://user:password@tech-notes-mysql:3306/tech_notes - PostgreSQL:
postgresql://user:password@tech-notes-postgres:5432/tech_notes - MongoDB:
mongodb://user:password@tech-notes-mongodb:27017/tech_notes - Redis:
redis://tech-notes-redis:6379 - SQLite:
sqlite:///data/tech_notes.db
To change the default connection information, you can create a .env file in the docker/environments/databases/ directory:
-
Copy the
.env.examplefile to.env:cp docker/environments/databases/.env.example docker/environments/databases/.env
-
Edit the
.envfile to change the connection information (usernames, passwords, database names, etc.) -
When running the
run-db-snippet.shcommand ormake db-run-snippet, the environment variables from the.envfile will be automatically used.
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.
import os
import mysql.connector
# Get connection info from environment variables
db_host = os.environ.get('DB_HOST', 'localhost')
db_port = os.environ.get('DB_PORT', '3306')
db_user = os.environ.get('DB_USER', 'user')
db_pass = os.environ.get('DB_PASS', 'password')
db_name = os.environ.get('DB_NAME', 'tech_notes')
# Connect to the database
conn = mysql.connector.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_pass,
database=db_name
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
for user in users:
print(user)
conn.close()const { MongoClient } = require('mongodb');
// Get connection string from environment variable
const uri = process.env.DB_CONN_STR || 'mongodb://user:password@localhost:27017/tech_notes';
async function main() {
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db('tech_notes');
const users = database.collection('users');
const query = {};
const cursor = users.find(query);
if ((await cursor.count()) === 0) {
console.log("No documents found!");
}
await cursor.forEach(user => {
console.log(user);
});
} finally {
await client.close();
}
}
main().catch(console.error);Each database has been configured with the following sample data:
userstable/collection with 3 userspoststable/collection with 4 posts linked to users
You can see the details of the sample data in the init files in the respective directory of each database.