Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a longer command from docker

I am using Docker to write a file on the fly and run it. The command looks like this so far (just to test the idea first):

docker run dockerfile/python cat <<EOF >hi.txt && tail hi.txt
> hi there
> EOF

For some reason this does not echo anything.

If I run this command without a HEREDOC then it does output the result. For example the following works:

docker run dockerfile/python cat > hi.txt && ls
hi.txt

How do I output the result of a multi line run command/HEREDOC.

like image 727
Johnston Avatar asked Dec 09 '14 01:12

Johnston


People also ask

How do you make a container run forever?

The easiest way to keep the container running is to change its entry point to a command that will continue running forever. Let's use tail -f /dev/null . Rechecking the running container via docker ps -a we can see that our container has been up and running for several seconds and hasn't stopped yet.

How do I run a command in docker container?

To use the docker exec command, you will need a running Docker container. If you don't already have a container, start a test container with the following docker run command: docker run -d --name container-name alpine watch "date >> /var/log/date. log"

How long will a docker container run for?

1. Overview. In this tutorial, we'll explore ways to keep Docker containers running indefinitely. By default, containers run only as long as their default command executes but a common use case it´s to run them indefinitely for debugging and troubleshooting purposes.

How do I get the full docker command?

docker ps --no-trunc will display the full command along with the other details of the running containers.


1 Answers

I was fiddling with crossbuild* and was wondering about how to use here documents to pipe commands to a Docker container. Here's the solution.

$ docker run --rm --interactive --volume $(pwd):/workdir --env CROSS_TRIPLE=x86_64-apple-darwin multiarch/crossbuild /bin/bash -s <<EOF
mkdir build && cd build
cmake ..
make
EOF

Quick rundown of what's happening.

  • --rm tells Docker to remove the container when it finished execution, otherwise it would show up in the output docker ps -a (not mandatory to use of course)
  • --interactive, -i is needed, otherwise /bin/bash would not run in an interactive environment and would not accept the here document from stdin as its input
  • about the -s flag passed to /bin/bash

    If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input.

  • --volume $(pwd):/workdir, just -v will mount the current working directory on the host to /workdir in the container
  • --env CROSS_TRIPLE=x86_64-apple-darwin, or simple -e tells the crossbuild container about the target platform and architecture (the container's entry point is /usr/bin/crossbuild, which is a shell script and based on the environment variable it's symlink the right toolchain components to the right places for the cross compilation to work)
  • multiarch/crossbuild the name of the Docker container to run (available in Docker Hub)

The commands can be fed to Docker like this as well.

$ cat a.sh
mkdir build && cd build
cmake ..
make
$ docker run --rm -i -v $(pwd):/workdir -e CROSS_TRIPLE=x86_64-apple-darwin multiarch/crossbuild /bin/bash -s < a.sh

Hoped this helps.

Update

Actually it seems you don't even need to use /bin/bash -s, it can be ommited, at least for the crossbuild container, YMMV.

*Linux based container used to produce multi-arch binaries: Linux, Windows and OS X, very cool.

like image 182
Kohányi Róbert Avatar answered Sep 30 '22 14:09

Kohányi Róbert