Home > Enterprise >  Iterating bash commands inside docker container
Iterating bash commands inside docker container

Time:08-06

I am a beginner to Docker and was trying to optimize the following bash shell code. The below code continuously has to create a docker container and remove it. What I would like is a one-time creation of the docker container; then based on the if-else condition, the corresponding command gets iteratively executed in while loop and finally, the docker container gets deleted. I have tried to play around with docker entrypoint commands but all of them seem to insist on a single line command with docker run. Some help will be deeply appreciated! Thanks!

#!/bin/bash
FILE=$1
flag=1
while read line; do
    #echo $line
    #echo $flag

    if [ $flag -gt 0 ] && [ $flag -lt 23189 ]; then
        docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Core.txt; dnf repoquery --srpm $line"
        ((  flag))

    elif [ $flag -gt 23188 ] && [ $flag -lt 46379 ]; then
        docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Old.txt; dnf info $line"
        ((  flag))

    elif [ $flag -gt 46378 ] && [ $flag -lt 69571 ]; then
        docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Graphics.txt"
        ((  flag))
    fi
done<"$1"

CodePudding user response:

Create container in detached mode:

docker run -d -t --rm --name my_container -v $(pwd):/eric -w /eric fedora

Run your commands in this container:

docker exec my_container sh -c "echo test"

CodePudding user response:

You may wrap those commands in a single script and put it in your pwd mapped to '/eric' volume. And then execute them something like:

docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "/eric/dnf-info.sh 1 '$line'"

Script dnf-info.sh may be:

#! /bin/sh
case $1 in
  1)
    dnf info "$2" | tee -a manifest-fed:Core.txt; dnf repoquery --srpm "$2"
    ;;
  2)
    dnf info "$2" | tee -a manifest-fed:Old.txt; dnf info "$2"
    ;;
  3)
    dnf info "$2" | tee -a manifest-fed:Graphics.txt
    ;;
esac
  • Related