Skip to content

Instantly share code, notes, and snippets.

@cleanunicorn
Last active September 28, 2025 15:06
Show Gist options
  • Select an option

  • Save cleanunicorn/19be74cd3b25735473b1fe42094eac76 to your computer and use it in GitHub Desktop.

Select an option

Save cleanunicorn/19be74cd3b25735473b1fe42094eac76 to your computer and use it in GitHub Desktop.
Functions to drop you into a docker container

Docker Dev Container Functions

This gist includes three Zsh functions that help you manage Docker containers tied to your current project folder.

Usage

  • Reuse or Create and Attach to the Persistent Container:

    Run:

    dev_container

    This command checks if a container associated with your current folder exists:

    • If it exists, it starts and attaches you to it.
    • Otherwise, it creates the container and attaches you.
  • Create a New Container with a Unique Name:

    Run:

    new_container

    This command always creates a new container (with a timestamp appended) and attaches you to it.

  • Delete the Container Associated with the Current Folder:

    Run:

    del_container

    This command stops and removes the persistent container tied to your current project folder.

Installation

  1. Copy functions to your shell profile

    Open your ~/.zshrc file and add the following functions (or copy from the docker-dev-functions.sh file if you saved them there):

# Helper to generate a consistent container name from the current folder path
_get_container_name() {
    # Replace '/' with '_' to form a friendly name, and add a "dev" prefix
    # Sanitize to ensure only Docker-allowed characters: [a-zA-Z0-9][a-zA-Z0-9_.-]
    local sanitized_path=$(echo "$PWD" | tr '/' '_' | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_.-]//g' | tr -s '_')
    # Ensure it starts with an alphanumeric character
    sanitized_path=$(echo "$sanitized_path" | sed 's/^[^a-z0-9]*//')
    # If the result is empty or doesn't start with alphanumeric, prepend "dev"
    if [[ -z "$sanitized_path" ]] || [[ ! "$sanitized_path" =~ ^[a-z0-9] ]]; then
        sanitized_path="dev_${sanitized_path}"
    fi
    echo "dev${sanitized_path}"
}

# 1. Reusable container: If the container exists, start and attach. Otherwise, create it and attach.
dev_container() {
    local container_name="$(_get_container_name)"
    if docker ps -a --filter "name=^${container_name}$" --format '{{.Names}}' | grep -Eq "^${container_name}\$"; then
        echo "Container '${container_name}' exists. Starting and attaching..."
        docker start "${container_name}" >/dev/null
    else
        echo "Creating container '${container_name}'..."
        docker run -d --name "${container_name}" -v "$PWD":/workspace ubuntu tail -f /dev/null
    fi
    docker exec -it "${container_name}" bash
}

# 2. New container: Creates a new container with a generated name (appends a timestamp).
new_container() {
    local base=$(basename "$PWD")
    local container_name="${base}_$(date +%s)"
    echo "Creating new container '${container_name}'..."
    docker run -it --name "${container_name}" -v "$PWD":/workspace ubuntu bash
}

# 3. Delete container: Stops and removes the container associated with the current folder.
del_container() {
    local container_name="$(_get_container_name)"
    if docker ps -a --filter "name=^${container_name}$" --format '{{.Names}}' | grep -Eq "^${container_name}\$"; then
        echo "Stopping container '${container_name}'..."
        docker stop "${container_name}" >/dev/null
        echo "Removing container '${container_name}'..."
        docker rm "${container_name}" >/dev/null
        echo "Container '${container_name}' deleted."
    else
        echo "No container found for the current folder with name '${container_name}'."
    fi
}
  1. Reload your shell:

    Run the following command in your terminal to load the new functions:

    source ~/.zshrc

Enjoy managing your Docker development containers with these simple functions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment