Skip to content

Instantly share code, notes, and snippets.

@bfolkens
Last active April 30, 2018 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfolkens/d401e265bfdecaf34507f50916ec90be to your computer and use it in GitHub Desktop.
Save bfolkens/d401e265bfdecaf34507f50916ec90be to your computer and use it in GitHub Desktop.
nvidia_docker2 example user-data script
#!/bin/bash
CLUSTER_NAME=[your-cluster-name]
echo ECS_CLUSTER=${CLUSTER_NAME} >> /etc/ecs/ecs.config
#
# Install NVIDIA CUDA & friends
# (remove section if using an image with drivers pre-installed)
#
set -x
exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1
echo BEGIN
date '+%Y-%m-%d %H:%M:%S'
# Create and mount extra volume
mkfs.ext4 /dev/xvdb
mkfs.ext4 /dev/xvdc
mkdir -p /var/lib/docker
mkdir -p /vol/data
cat <<-EOF >> /etc/fstab
/dev/xvdb /var/lib/docker ext4 noatime 0 0
/dev/xvdc /vol/data ext4 noatime 0 0
EOF
sudo systemctl daemon-reload
sudo mount -a
# Install CUDA prereqs
apt-get -qq update -y
apt-get -qq install -y build-essential
apt-get -qq install -y linux-headers-$(uname -r)
cuda_distro=ubuntu1604
cuda_version=8.0.61-1
cuda_architecture=amd64
wget -q http://developer.download.nvidia.com/compute/cuda/repos/${cuda_distro}/x86_64/cuda-repo-${cuda_distro}_${cuda_version}_${cuda_architecture}.deb
sudo dpkg -i cuda-repo-${cuda_distro}_${cuda_version}_${cuda_architecture}.deb
rm cuda-repo-${cuda_distro}_${cuda_version}_${cuda_architecture}.deb
apt-get -qq update
apt-get -qq install -y cuda
# Reset running kern modules
rmmod nvidia_modeset
rmmod nvidia_uvm
rmmod nvidia
modprobe nvidia
#
# Install ECS & Docker
# (remove section if using an "ECS-optimized" image)
#
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
apt-get -qq update
apt-get -qq install -y docker.io
sh -c "echo 'net.ipv4.conf.all.route_localnet = 1' >> /etc/sysctl.conf"
sysctl -p /etc/sysctl.conf
iptables -t nat -A PREROUTING -p tcp -d 169.254.170.2 --dport 80 -j DNAT --to-destination 127.0.0.1:51679
iptables -t nat -A OUTPUT -d 169.254.170.2 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 51679
mkdir -p /etc/iptables && sudo sh -c 'iptables-save > /etc/iptables/rules.v4'
mkdir -p /etc/ecs && sudo touch /etc/ecs/ecs.config
cat <<-EOF > /etc/ecs/ecs.config
ECS_CLUSTER=${CLUSTER_NAME}
ECS_DISABLE_PRIVILEGED=false
ECS_AVAILABLE_LOGGING_DRIVERS=["syslog"]
ECS_LOGFILE=/log/ecs-agent.log
EOF
#
# Install nvidia-docker2
#
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
apt-get update
apt-get -qq install -y nvidia-docker2
cat <<"EOF" > /etc/docker/daemon.json
{
"default-runtime": "nvidia",
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
pkill -SIGHUP dockerd
#
# Start ecs-agent at the very end
# (remove section if using an "ECS-optimized" image)
#
docker run \
--name ecs-agent \
--detach=true \
--restart=on-failure:10 \
--volume=/var/run:/var/run \
--volume=/var/log/ecs/:/log \
--volume=/var/lib/ecs/data:/data \
--volume=/etc/ecs:/etc/ecs \
--net=host \
--env-file=/etc/ecs/ecs.config \
amazon/amazon-ecs-agent:latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment