How to Install and Use cURL in Docker Containers
Posted January 19, 2024
When running cURL commands inside the Docker container, you first need to ensure cURL is installed. You create your custom image but at the same time run the command needed to get cURL ready before running your Docker container. This way, your image had a reference to the curl
In this guide, you will learn how to install cURL to Docker with Dockerfile and Docker Compose.
In summary, you will learn:
- How to get cURL to run inside of the Docker Container.
- How to use the installed cURL and run the command in your container.
- Answer the question, how do I download a curl file to a docker container?
- How not to install cURL with Dockerfile
Step One: Installing cURL with Dockerfile and Ubuntu
To install and use cURL in Docker containers I’ll assume you’re using a Linux-based Docker container, but the general approach is similar for other operating systems.
Go ahead and create your Docker file as follows:
# Your desired Linux distribution
FROM ubuntu:latest
# Install cURL
RUN apt-get update && apt-get install -y curl
# Set the working directory
WORKDIR /app
# Your additional configuration
CMD ["bash"]
The key point here is:
# Install cURL
RUN apt-get update && apt-get install -y curl
It is the same as:
RUN apt-get update \
&& apt-get install -y curl
In your image, you need to update the package list before attempting to install cURL within the container.
Step Two: Installing cURL with Dockerfile and Linux Alpine
Alpine is known for its small size. If your base image is Alpine based modify the Dockerfile accordingly as follows:
# Alpine Linux
FROM alpine:latest
# Install cURL
RUN apk --no-cache add curl
# Set the working directory
WORKDIR /app
# Command to run when the container starts
CMD ["sh"]
On Alpine, you will need to install cURL using the apk
package manager. Remember to add the --no-cache
flag is used to avoid storing the package index locally.
Step Three: Running a cURL Container
Once your cURL is ready (based on what your Dockerfile looks like), you will build the image using the Docker build command:
docker build -t curl_image .
Now run this image in a docker container:
docker run -it curl_image
Step Four: Testing cURL Within the Docker Container
For test reasons, I’m using it
for running the container in interactive mode with a pseudo-TTY. This allows you to interact with the container. However, in normal cases, you will need to enter into your container using the following to terminal into the running container:
docker ps
docker exec -it your-container-name /bin/sh
In my case, I will run the following command to test if cURL is installed:
curl --version
At the same time, I can send an actual cURL command within this container:
curl https://reqres.in/api/users
Step Five: No Need to Install cURL for Debugging Reasons
It’s not a must for you to install cURL permanently in your container. At this point. I will assume you have your container running on Docker. Now you will docker exec
to access your container as follows;
#list available container
docker ps
# Go into your container terminal
docker exec -it container-name /bin/sh
# Example
docker exec -it a256e2433cae /bin/sh
If you are an Alpine image, you will use apk
(use apt-get
on Debian on Ubuntu) to install cURL with:
apk add curl
You then ran the curl
command to verify that cURL is installed and available inside this Docker container.
This approach is good for debugging and ad-hoc changes within a running container. Use Dockerfile to include cURL during the image build process if you need cURL permanently running in your container.
Step Six: cURL and Docker Compose
On the Docker Compose file, you can include the cURL installation as follows:
version: '3.8'
services:
my-service:
image: nginx:latest
ports:
- "8899:8080"
command: sh -c "apk add --no-cache curl && tail -f /dev/null"
This is a very basic example. It’s not realistic when creating a container.
What you need to do is add your cURL using Dockerfile. Then you will add a build
context within Docker Compose as follows:
version: '3.8'
services:
api-test:
restart: always
build: . # path to Dockerfile
# Other Docker compose configurations
Step Seven: Using cURL with depend_on and Docker Compose Container
Consider the following example of using cURL with Docker compose:
version: '3.9'
services:
web-client:
image: curlimages/curl:latest
command: curl http://api-server:8000/
depends_on:
- api-server
api-server:
image: 'node:18'
ports:
- '8000:8000'
volumes:
- './api:/app'
command: 'npm start'
You will note the cURL will run using the web-client
service. However, it has a depends_on
parameter pointing to api-server
. In this case:
depends_on
Control startup and shutdown order on a container.- The
api-server
running Node.js must be running before attempting to the cURL command with Docker. curl http://api-server:8000/
is not supposed to becurl http://localhost:8000/
. cURL must point to the service running the URL being tested
Conclusion
I hope these steps with the Docker container and cURL got your solution solved.