DEV Community

box
box

Posted on

Using cURL Inside a Docker Container

Environment

  • Mac

    • CPU: Apple M4
    • OS: Sequoia 15.1.1
  • Zsh

    • Version: 5.9
  • Docker Desktop

Conclusion

  1. Open a terminal.

  2. Move to the dir containing the files used by cURL, if necessary.

  3. Start the container with cURL installed and enter it.

    docker run --rm -it -v "$PWD:/work" -w /work curlimages/curl:latest sh
    
  4. Use cURL until satisfied.

  5. Exit the container.

    exit
    
  6. Remove the Docker Image, if necessary.

    docker rmi curlimages/curl:latest
    

Structure

Start the container with cURL installed and enter it.

--rm

This option is used to automatically remove the container (and its associated anonymous volumes) after it stops running. Since a container created to execute cURL does not need to be retained after execution, this option ensures the system is kept clean.

-it

This option consists of -i and -t. -i option enables STDIN within a container, and -t option attaches a pseudo-TTY to the container.

If we run the command without -i option (i.e. with only -t option), a pseudo-TTY will be allocated and the shell will start, but no commands can be accepted and we cannot continue the operation because STDIN is disabled. To forcefully exit from a container in this state, we need to send three consecutive SIGINT signals by pressing cmd + . same times. And this exiting does not trigger the automatic container removal provided by --rm option, so we need to trigger it by stopping the container or remove the container directly.

If we run the command without -t option (i.e. with only -i option), we can run commands using STDIN, but operations that rely on interpreting ANSI escape sequences cannot be performed because a pseudo-TTY is not attached. For example, we will be unable to move the cursor using keyboard, and output results will not be colorized.

-v "$PWD:/work"

This option is used to mount a dir from the host machine into a container.

-v <host-dir-path>:<container-dir-path>
Enter fullscreen mode Exit fullscreen mode

$PWD is an env variable supported by POSIX-compliant shells, representing the Present Working Directory on the host machine. In this article, the command is executed in Zsh, which also supports this env variable.

The purpose of using this option is to allow the container to access files located on the host machine. For instance, when transferring files with the -T option in cURL, it ensures that host files can be referenced directly from the container.

-w /work

By specifying this option, we can set the dir, mounted from the host dir, as the current dir.

curlimages/curl:latest

official docker image for curl

sh

sh command is executed in a container when explicitly specified in docker run command.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Enter fullscreen mode Exit fullscreen mode

By specifying sh in the [COMMAND] section, a shell is launched immediately after the container starts, allowing us to execute subsequent commands interactively.

Test

For example, If we want to test sending HTTP requests, we can use the service httpbin.

Top comments (0)