How to retrieve an image from a post and display it before excerpt of a post? [duplicate]

if it is the “Featured Image” of your post, you can use:

<?php 
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) { the_post_thumbnail(); } 
?>

check: the_post_thumbnail function

else if you want to get the first image inside your post, you can use somthing like that:

function get_first_post_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  //Defines a default image
  if(empty($first_img)){ 
    $first_img = "/images/default.jpg";
  }
  return $first_img;
} 

Then place <?php echo get_first_post_image(); ?>