0

Good evening,

i just try to stop some docker containers defined in an array. But only the first one is stopped but four times.

CONTAINERTOSTOP=(webgrabplus Nextcloud ddclient MariaDB-10.6)

for constopping in "${CONTAINERTOSTOP[@]}"; do                      
docker container stop $CONTAINERTOSTOP done

My script only stops the webgrabplus container but for four times so i think the loop works.

Where is my fault here ?

1
  • docker container stop "$constopping"; done Commented Jun 24, 2022 at 14:21

2 Answers 2

4

You are stopping $CONTAINERTOSTOP. You want $constopping.

You are also either missing a ; or a newline after the docker command.

for constopping in "${CONTAINERTOSTOP[@]}"
do
    docker container stop "$constopping"
done

(Of course, @ufopilot's answer is even better in the specific case, but I let my answer stand for the "if you want a loop..." case.)

Sign up to request clarification or add additional context in comments.

2 Comments

and since $CONTAINERTOSTOP == ${CONTAINERTOSTOP[0]} == webgrabplus the stop is issued against webgrabplus 4 times
Better to quote "$constopping"
2

To stop just using (without loop):

CONTAINERTOSTOP=(webgrabplus Nextcloud ddclient MariaDB-10.6)
docker stop "${CONTAINERTOSTOP[@]}"

1 Comment

Thank you, sometimes you cant find the forest for the trees.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.