toti toto
toti toto

Reputation: 87

Docker - why cp doesn't copy correctly via bash?

I'm trying to run an ordinary bash script to run a tool from a container and extract its output to the host machine. This is what i've got so far (stripped of the script itself ofcourse):

docker build -t amass https://github.com/OWASP/Amass.git
docker run -d --name amass_dock amass
docker exec -it amass_dock sh -c "./bin/amass --passive -d example.com -o out.txt"
docker cp amass_dock:/out.txt .

This gives out the error: "Cannot exec in a stopped state". What is the correct way to perform this? my goal is to eventually run the docker program and take output to host machine.

Upvotes: 0

Views: 635

Answers (2)

hek2mgl
hek2mgl

Reputation: 158250

The simplest would be to read the manual :), and run it like this:

docker build -t amass https://github.com/OWASP/Amass.git
# Note that out.txt will be created on your host, not within the container
docker run amass --passive -d example.com > out.txt
cat out.txt

PS: For convenience you might want to place a wrapper script on your host system like this:

#!/bin/bash
# /usr/local/bin/amass
docker run amass "$@"

Make it executable:

chmod +x /usr/local/bin/amass

Now you can run amass, and use it in scripts, as if it would be installed on your host system:

amass --passive -d example.com

Upvotes: 2

David Maze
David Maze

Reputation: 159975

If the main goal of your program is to read and write local files, consider running it not in Docker. That completely avoids the container-lifecycle and filesystem-mapping issues you're running into.

sudo apt install snapd
sudo systemctl start snapd
sudo snap install amass
./bin/amass --passive -d example.com -o out.txt

Otherwise, Docker containers have their own separate filesystems, and need to explicitly be given access to host-system files. See the Docker documentation on bind mounts. You might run this program like

sudo docker build -t amass https://github.com/OWASP/Amass.git
sudo docker run --rm -v $PWD:/data \
  amass --passive -d example.com -o /data/out.txt
cat out.txt

Note that you can specify any host directory in the docker run -v options, even system directories like /etc and /bin, and for that reason I've explicitly called out the steps that require root-equivalent permissions (membership in a docker group is equivalent to having root). Also note that without Docker you can run the tool as an ordinary user, but to run the Docker container you must effectively be root.

If your problem is that the container is exiting ("...in a stopped state") your very first step should be to look at docker logs and run the container in the foreground without the -d option to understand why. While docker exec is a useful debugging tool it wasn't designed to be the primary way to interact with a container.

Upvotes: 0

Related Questions