How I can check if get_theme_mod is in header or a template part

I tried if ( ! did_action( 'get_header' ) ) but doesn’t seem to work.

Try again, but this way:

add_filter( 'theme_mod_understrap_container_type', 'override_container_type' );
function override_container_type( $value ) {
    if ( did_action( 'get_header' ) && ! did_action( 'get_footer' ) ) {
        if ( get_field('container_type') !== 'inherit' ){
            return get_field('container_type');
        }
    }

    // For header.php and footer.php
    return $value;
}

So basically, here’s the conditional you need:

did_action( 'get_header' ) && ! did_action( 'get_footer' )

UPDATE

If you are using a child theme, how about these:

  • Add this to functions.php:
    function my_theme_container_type( $location = null ) {
        if ( 'main' === $location ) {
            if ( $value = get_field('container_type') ){
                return $value;
            }
        }
    
        return get_theme_mod( 'understrap_container_type' );
    }
    

  • Edit the single.php and page.php files, and change this:
    $container = get_theme_mod( 'understrap_container_type' );
    

    to

    $container = my_theme_container_type( 'main' );
    

  • And remove this (from functions.php):
    add_filter( 'theme_mod_understrap_container_type', 'override_container_type' );
    function override_container_type() {
        ...
    }
    

  • That may not answer the question, but I’d do that if I were you. =)