Skip to content

Instantly share code, notes, and snippets.

@mbreese
Last active June 12, 2023 21:28
Show Gist options
  • Save mbreese/e6d4b57867ca440341b5 to your computer and use it in GitHub Desktop.
Save mbreese/e6d4b57867ca440341b5 to your computer and use it in GitHub Desktop.
Running docker with a HEREDOC to script the commands to run inside the container.
docker run -v /Users/mbreese/tmp:/tmp1 -w /tmp1 -i centos:7 /bin/bash -s <<EOF
date > foo
echo 'foo' >> foo
cat /etc/redhat-release >> foo
whoami >> foo
EOF
@sexygeoff
Copy link

sexygeoff commented Nov 28, 2016

Thanks, just what I needed!

@crazycodr
Copy link

Great work
Not too sure how to use this with an if, should it look like this?

if docker-compose exec container bash -s << 'EOF'; then
# script to run
EOF
some commands
fi

@rockandska
Copy link

Alternative without the use of "bash -s"

$ cat <<"EOF" | docker run --rm -i --name ${DOCKER_CONTAINER_NAME} ${IMAGE_NAME}
  date > foo
  echo 'foo' >> foo
  cat /etc/redhat-release >> foo
  whoami >> foo
EOF

@mqu
Copy link

mqu commented Jun 19, 2020

with docker-compose and heredoc syntax

cat <<"EOF" | docker-compose exec -T app /bin/bash -
  cat /etc/os-release
EOF

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