I’m trying to add a script to a docker run command , command i’m using is :
docker run -dit --name 1.4 ubuntu sh -c 'echo "Input website:"; read website; echo "Searching.."; sleep 1; curl http://$website;'
and then install curl , then enter a website as input and it should reply to me as per the course i’m studying , but running this exact command makes the container exit immediately
any guidance on why would that be ?
also how should i send the input to the container so it can use it afterwards , do i just attach to it after installing curl in the terminal ?
2
Answers
I’m going to recommend an extremely different workflow from what you suggest. Rather than manually installing software and trying to type arguments into the stdin of a shell script, you can build this into a reusable Docker image and provide its options as environment variables.
In comments you describe a workflow where you first start a container, then get a debugging shell inside of it, and then install
curl
. Unless you’re really truly debugging, this is a pretty unusual workflow: anything you install this way will get lost as soon as the container exits, and you’ll have to repeat this step every time you re-run the container. Instead, create a new empty directory, and inside that create a file namedDockerfile
(exactly that name, no extension, capitalD
) containingRather than try to read from the container’s input, you can take the URL as an environment variable. In most cases the best way to give the main command to a container is by specifying it in the Dockerfile. You can imagine running a larger script or program here as well, and it would take the some environment-variable setting (using Python’s
os.environ
, Node’sprocess.env
, Ruby’sENV
, etc.).In our case, let’s make the main container command be the single
curl
command that you’re trying to run. We haven’t specified the value of the environment variable yet, and that’s okay: this shell command isn’t evaluated until the container actually runs.Now let’s build and run it. When we do launch the container, we need to provide that
$website
environment variable value, which we can do with adocker run -e
option.So note that we’re starting the container in the foreground (no
-d
option) since we want to see its output and we expect it to exit promptly; we’re cleaning up the container when it’s done; we’re not trying to pass a full shell script as a command-line argument; and we are providing our options on the command line, so we don’t need to make the container’s stdin work (no-i
or-t
option).A Docker container is a wrapper around a single process. When that process exits, the container exits too. In this example, the thing you want the container to do is run a
curl
command; that’s not a long-running process, hencedocker run --rm
but not-d
. There’s not an "afterwards" here, if you need to query a different Web site then launch a new container. It’s very normal to destroy and recreate containers, especially since there are many options that can only be specified when you first start a container.With the image and container we’ve built here, in fact, it’s useful to think about them as analogous to the
/usr/bin/curl
binary on your host. You build it once into a reusable artifact (here the Docker image), and you run multiple instances of it (curl
commands or new Docker containers) giving options on the command line at startup time. You do not typically "get a shell" inside acurl
command-line invocation, and I’d similarly avoiddocker exec
outside of debugging tasks.You can also use
alpine/curl
image to usecurl
command without needing to install anything.First start the container in detached mode with
-d
flag.Then run your script with
exec
sub command.