-1

I have a docker container based on an image that, by default, does not run any command when started. I need to execute a command in that container.

Running docker run -it [container tag] runs, but there are two problems:

  1. It runs in interactive mode (as one would expect), while I need something that can run on a server, without human supervision.
  2. I need to perform a docker cp commands before executing the command I need to run.

Basically, what I have is:

  • docker create creates the container.
  • docker cp moves over the files I need.
  • docker start starts the container, but since it has no default command to run, it exits immediately.
  • docker exec refuses to run my command, because the container is stopped. No amount of docker start can fix this.

I cannot edit the image Dockerfile, so unfortunately, adding a while true; done (suggested here) as default command to run isn't possible.

I assumed a command that would chain start and exec back-to-back (and ensure the container doesn't close before the command is run, similar to how run is create-start-attach) would be already existing, but I can't seem to find anything neither in the documentation nor in other forum answers.

Invizio
  • 382
  • 1
  • 8
  • You need your container to run a command on start-up that makes it stay alive. It would help if we know what image it is you're running. – Hans Kilian Jan 24 '23 at 07:42
  • I would expect that Docker would enforce the presence of that command if its absence makes the container unusable. The image is a proprietary image, so I can't share it, but I suppose the fact that it does not have an entry point is all that's needed. FWIW I believe it's based on ubuntu. – Invizio Jan 24 '23 at 15:47
  • The purpose of a container is to encapsulate a process. Your image might not start a process by default, but you'd then have to start one on `docker run`. Having no process at all makes no sense. – Hans Kilian Jan 24 '23 at 15:50
  • There _is_ a process I'd like to run — _after_ having copied some extra files from the host. What I need is very close to `docker run`, but that does not allow for inserting extra steps before running the given process. – Invizio Jan 24 '23 at 16:02
  • Actually, your comment made me think of something that worked around the problem. I've posted my own answer. – Invizio Jan 24 '23 at 16:38

2 Answers2

1

You could just mount your dir where your file you need to copy is directly into the container then run the command you need. In this example I am just running an ls, but I could also run the script its self

sample file

#!/bin/bash
echo "running this on $(hostname)"
date
echo "ending....."
$ docker run -v $(pwd)/scripts:/myscripts ubuntu ls -lrt ./myscripts/stack.sh
-rwxrwxrwx 1 root root 70 Jan 24 05:43 ./myscripts/stack.sh

$ docker run -v $(pwd)/scripts:/myscripts ubuntu ./myscripts/stack.sh
running this on 63b65348373a
Tue Jan 24 05:44:22 UTC 2023
ending.....

In either case I am essentially mounting my script into the container image then running a command using the file I have mounted in.

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • It's probably the best option I have, although it has its drawbacks (constrained to a single `cp` and a single script, security concerns with the mounted volume accessing the host's FS, can't launch another process in the container to do other stuff afterwards...). It would really help to have some `--start-if-stopped` flag on `docker exec` to avoid those issues. But in the meantime, even though it probably won't do for production, at least I have enough to get something functioning, so thanks. I'll wait the mandatory few hours, and if no other answer is posted, I'll accept yours. – Invizio Jan 24 '23 at 06:02
0

@Hans Kilian's comment made me remember that the argument passed to docker run (after the image) is a replacement for the entry point. Therefore, if I have an image without an entry point by default, I can simply run:

docker run -d [image] sleep infinity

...and it will launch the container with a process that won't exit.

Afterwards, I am free to run as many docker cp and docker exec as I want.

Invizio
  • 382
  • 1
  • 8