title: How to get container id of running container programmatically (on Docker)?
body: <p>I have an app in a docker setup. I would like to run a script on the host that would run some commands in an existing (running container).</p>
  
  <p>If I know the container id, say ... it's 50250e572090 ... then I can run the script like this</p>
  
  <p>For example ...</p>
  
  <pre><code>#!/usr/bin/env bash
  docker exec 50250e572090 example_command_1_here
  docker exec 50250e572090 example_command_2_here
  docker exec 50250e572090 example_command_3_here
  docker exec 50250e572090 example_command_4_here
  </code></pre>
  
  <p>It's working great! ... but the thing here is that I only know the image name ... not the container id. To find the container id ... I use <code>docker ps</code> ... where I get something like this ...</p>
  
  <pre><code>CONTAINER ID        IMAGE                              COMMAND             CREATED             STATUS              PORTS               NAMES
  50250e572090        aws_beanstalk/staging-app:latest   "/sbin/my_init"     29 hours ago        Up 29 hours         80/tcp, 443/tcp     drunk_bardeen 
  </code></pre>
  
  <p>It's output isn't something that I can use (pipe through). Which command can I run to get the container id as the output which can then be piped into the script? Or now that it's clear what I'm trying to achieve ... is there a better way? </p>
  
  <p>Ps: My context is that I'm on elastic beanstalk ... but I don't see how this changes anything. Might as well be on the local host ... the problem is the same.</p>