Skip to content

Instantly share code, notes, and snippets.

@cleanunicorn
Created September 12, 2025 12:10
Show Gist options
  • Save cleanunicorn/19be74cd3b25735473b1fe42094eac76 to your computer and use it in GitHub Desktop.
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
        echo "dev$(echo "$PWD" | tr '/' '_')"
    }
    
    # 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
    }
  2. 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