This document explains how to perform a speed test inside a Docker container using the speedtest-cli
utility. It walks through the setup and execution steps to diagnose network performance issues, check bandwidth usage, or evaluate a container’s network connectivity.
Why your internet speed might vary
Docker containers run in isolated environments with their own networking setup. By default, containers use one of Docker’s networking modes, such as bridge mode, which introduces an additional abstraction layer between the container and the host machine’s network. This abstraction can lead to:
- Overhead from Network Virtualization: The bridge network mode requires packets to be routed through virtual network interfaces and potentially NAT (Network Address Translation), which can slightly degrade performance compared to native host speeds.
- Resource Limitations: Containers might have limited access to system resources, such as CPU and memory, which can affect the performance of networking operations, especially under load.
- Shared Bandwidth: Multiple containers running on the same host share the host's network bandwidth. If other containers or processes are consuming significant bandwidth, the speeds seen in individual containers may appear reduced.
- Testing Environment: Tools used to measure internet speed inside containers may not account for the additional overhead introduced by the container environment, leading to lower reported speeds.
- Docker Bug: Some users intermittently experience a bug within the Docker community. This is an on-going issue that we are investigating solutions for.
Running the Test
1️⃣ Prerequisites
Before you begin, ensure the following requirements are met:
- Docker installed and running on the host machine.
- A functioning Docker container with network access.
- Terminal or Command Line admin access.
Make sure the user running the Docker commands has the necessary permissions to access Docker resources. This may require root or admin access, or the user should be part of the docker
group.
2️⃣ Launching a Docker Container with Network Access
To run a speed test, the first step is to launch a Docker container with network access.
- Download the Docker image (Python 3.9 slim image in this case):
docker pull python:3.9-slim
- The command downloads a docker image from the Docker registry. Python is the name of the image, followed by its version.
3️⃣ Run the Docker container:
This command starts the container and opens an interactive terminal (/bin/bash) inside the container. You’ll be able to install and run the speed test from here.
Run the Docker container:
docker run -it --name speedtest-container python:3.9-slim /bin/bash
4️⃣ Install the Speedtest Utility
Once inside the container, the next step is to install the speedtest-cli
utility, which is used to run the speed test.
- Install
speedtest-cli
using pip:
pip install speedtest-cli
5️⃣ Run the Speed Test
Now that the utility is installed, you can run the speed test to evaluate your network performance. Enter the below in your command line interface (CLI).
speedtest-cli
This command will initiate a speed test and display analytics such as ping, download speed, and upload speed.
If you continue facing issues, make sure the container is not restricted by firewall settings or network policies on your host machine.
6️⃣ Troubleshooting: Unable to Run Speed Test
If you encounter issues running the speed test, here are a few troubleshooting steps to ensure network connectivity is not the problem.
Check network access: Try pinging a reliable external site (e.g.,
google.com
) from inside the container:
ping google.com
If you are unable to ping the site successfully, it means there is an issue with the container’s network access, and the speed test won't work.
Network Flag: If the connection is blocked, try adding the
--network host
flag to give the container more direct access to the host’s network. For example:
docker run -it --network host --name speedtest-container python:3.9-slim /bin/bash
- Verify the container's network access: You can also verify the network settings using:
docker network ls
- This will list all available networks and show if the container has access to the correct network.
Container Network Isolation: Docker containers are isolated from the host machine’s network by default. When testing network performance, the container may not always have the same network privileges as the host machine. If you're encountering issues with the speed test or other network-related commands, be sure the container has appropriate network access by either configuring Docker’s network settings or using the --network host
flag.
7️⃣ Exiting the Container
Once you’ve completed the speed test and gathered the necessary data, you can exit the container.
- Exit the container by typing:
exit
- Alternatively, you can press
CTRL+D
.
If the speedtest-cli
utility isn’t working as expected, you can try other network speed testing tools. One popular alternative is fast-cli
, which provides similar functionality.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article