blob: b2b0b1ae03faeb23242ba0693542758ad9e71a8b [file] [log] [blame]
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script to copy a file/directory located inside a container as a tarball.
# Usage:
# ./container_file_export.sh l.gcr.io/google/python:latest /opt/python3.6 py.tar.gz
# In order to use this script, you need to have docker installed, see
# https://docs.docker.com/engine/installation/
#
# Noted: this script is only meant to be used directly by the corresponding
# container_file_export skylark rule.
#!/usr/bin/env bash
set -e
main() {
if [ "$#" -ne 3 ]; then
echo "Illegal number of parameters: ./container_cp.sh <DOCKER_IMAGE> <SOURCE_PATH> <TARGET_TARALL_FILE>"
fi
IMAGE=$1
SOURCE=$2
TARGET=$3
container_name="data-container-$RANDOM"
docker run -t -d --name ${container_name} $IMAGE sleep infinity
docker exec -e GZIP=-n ${container_name} tar -czf /tmp/data.tar.gz --mtime='1970-01-01' $SOURCE
docker cp ${container_name}:/tmp/data.tar.gz $TARGET
docker rm -f ${container_name}
}
main $@