Skip to content

Instantly share code, notes, and snippets.

@nealeu
Last active October 17, 2024 08:18
Show Gist options
  • Save nealeu/2eaa543b5f0d26d85109b0b582098464 to your computer and use it in GitHub Desktop.
Save nealeu/2eaa543b5f0d26d85109b0b582098464 to your computer and use it in GitHub Desktop.
Run MySQL in docker with latency to simulate network
services:
mysql:
# instead of image: mysql:5.7-debian
build: ./docker/mysql-with-traffic-control
cap_add:
- NET_ADMIN
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: flyingcars
MYSQL_USER: zephod
MYSQL_PASSWORD: child
MYSQL_ROOT_PASSWORD: not-so-secret!
# comment out to have no latency added. 20ms seems sufficient to unearth perf issues
LATENCY_MS: 20
command:
#
- "--default-authentication-plugin=mysql_native_password"
- "--lower_case_table_names=1"
- "--character-set-server=utf8"
- "--collation-server=utf8_general_ci"
volumes:
- "./mysql-db:/var/lib/mysql"
# Dockerfile - saved in ./docker/mysql-with-traffic-control
FROM mysql:5.7-debian
# Add MySQL APT repository key
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C
# Install iproute2 for the `tc` command (NOTE ; instead of & because update still complains about key)
RUN apt-get update; apt-get install -y iproute2 && rm -rf /var/lib/apt/lists/*
# Copy the latency script into the container
COPY start_with_latency.sh /usr/local/bin/start_with_latency.sh
RUN chmod +x /usr/local/bin/start_with_latency.sh
# Override the entrypoint to include the latency script
ENTRYPOINT ["/usr/local/bin/start_with_latency.sh"]
CMD ["mysqld"]
#!/bin/bash
# Start MySQL in the background
docker-entrypoint.sh "$@" &
# Add latency if LATENCY_MS is set and greater than 0
if [ "${LATENCY_MS:-0}" -gt 0 ]; then
tc qdisc add dev eth0 root netem delay "${LATENCY_MS}ms"
echo "Added ${LATENCY_MS}ms of latency to eth0"
fi
# Wait for MySQL to exit
wait -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment