Skip to content

Instantly share code, notes, and snippets.

@mdeweerd
Last active August 22, 2023 17:18
Show Gist options
  • Save mdeweerd/4688d833d41d992a644f4e176386ae2e to your computer and use it in GitHub Desktop.
Save mdeweerd/4688d833d41d992a644f4e176386ae2e to your computer and use it in GitHub Desktop.
Scripts for Docker (on Windows)

Scripts

  • optimizeDocker.ps1: Powershell Script to optimize Docker Data File. Gets location from configuration file.
  • moveDocker.cmd: Script to move Docker Data File (out of default directory). Use as moveDockerData.cmd D:\DockerData. Permissions of the target directory should be set to allow access to the wsl process.

Note: The shebang is for cygwin.

License

You can republish/modify these scripts as long as you reference this Gist URL in the scripts: https://gist.github.com/4688d833d41d992a644f4e176386ae2e

---
files: ^(.*\.(py|json|md|sh|yaml|cfg|txt))$
exclude: ^(\.[^/]*cache/.*|.*/_user.py)$
repos:
- repo: https://github.com/executablebooks/mdformat
# Do this before other tools "fixing" the line endings
rev: 0.7.16
hooks:
- id: mdformat
name: Format Markdown
entry: mdformat # Executable to run, with fixed options
language: python
types: [markdown]
args: [--wrap, '75', --number]
additional_dependencies:
- mdformat-toc
- mdformat-beautysh
# -mdformat-shfmt
# -mdformat-tables
- mdformat-config
- mdformat-black
- mdformat-web
- mdformat-gfm
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
# - id: no-commit-to-branch
# args: [--branch, main]
- id: check-yaml
args: [--unsafe]
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-json
- id: mixed-line-ending
- id: check-builtin-literals
- id: check-ast
- id: check-merge-conflict
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: check-docstring-first
- id: fix-byte-order-marker
- id: check-case-conflict
# - id: check-toml
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.32.0
hooks:
- id: yamllint
args:
- --no-warnings
- -d
- '{extends: relaxed, rules: {line-length: {max: 90}}}'
- repo: https://github.com/lovesegfault/beautysh.git
rev: v6.2.1
hooks:
- id: beautysh
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
hooks:
- id: codespell
args:
# - --builtin=clear,rare,informal,usage,code,names,en-GB_to_en-US
- --builtin=clear,rare,informal,usage,code,names
- --ignore-words-list=hass,master
- --skip="./.*"
- --quiet-level=2
#!/usr/bin/env powershell
# License:
# Free to modify and reuse at your own risks.
# Keep link to original location:
# https://gist.github.com/4688d833d41d992a644f4e176386ae2e
# Set the distribution name to search for
$DISTRIBUTION_NAME = "docker-desktop-data"
# Use PowerShell to query the registry and find the distribution information
$basePath = ""
Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss\*" | ForEach-Object {
if ($_.DistributionName -eq $DISTRIBUTION_NAME) {
$basePath = $_.BasePath
}
}
# Remove "\\?" prefix from the base path if present
$basePath = $basePath -replace "^\\\\\?\\", ""
# Remove trailing backslash if present
$basePath = $basePath -replace "\\$", ""
# Print data location
Write-Host "Current Docker Data Location: $basePath"
# Rest of your optimization script...
# Make sure to use $basePath in your script for diskpart
# Generate a unique temporary filename
$tempScript = [System.IO.Path]::GetTempFileName()
# Create script content
$scriptContent = @"
select vdisk file="$basePath\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
"@
# Write script content to temp file
$scriptContent | Set-Content -Path $tempScript
wsl --shutdown
# Run diskpart script
diskpart /s "$tempScript"
# Clean up temp file
Remove-Item -Path $tempScript -Force
Read-Host "Press enter to exit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment