skip to Main Content

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


  1. 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 named Dockerfile (exactly that name, no extension, capital D) containing

    # Start our new image from this base
    FROM ubuntu
    
    # Install any OS-level packages we need
    RUN apt-get update 
     && DEBIAN_FRONTEND=noninteractive   # avoid post-installation questions
        apt-get install 
          --no-install-recommends        # don't install unneeded extra packages
          --assume-yes                   # (-y) skip an "are you sure" prompt
          curl
    

    Rather 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’s process.env, Ruby’s ENV, 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.

    # at the end of the Dockerfile
    CMD curl "$website"
    

    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 a docker run -e option.

    # Build the image:
    docker build 
      -t my/curl  # giving it a name
      .           # using the content in the current directory
    
    docker run 
      --rm        # deleting the container when done
      -e website=https://stackoverflow.com 
      my/curl     # with the same name as above
    

    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, hence docker 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 a curl command-line invocation, and I’d similarly avoid docker exec outside of debugging tasks.

    Login or Signup to reply.
  2. You can also use alpine/curl image to use curl command without needing to install anything.

    First start the container in detached mode with -d flag.
    Then run your script with exec sub command.

    
    docker run -d --name 1.4 alpine/curl sleep 600 
    docker exec -it 1.4 sh -c 'echo "Input website:"; read website; echo "Searching.."; sleep 1; curl http://$website;'
    
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search