Community discussions

MikroTik App
 
User avatar
EvertonSSJ4
just joined
Topic Author
Posts: 5
Joined: Thu Aug 18, 2022 1:46 am

Script VS Container

Sun Dec 04, 2022 9:51 pm

Hello,

I'm trying to create an script to delete the current Container image and download a new one, but the command /container remove numbers=0 don't work (this proccess is after the command /container/stop number=0 and delay 15s).

Executing /container remove number=0 via MikroTik terminal, works without problem... I haved tryed only the command /container remove number=0 in script (to see if it will work only with this process) and no good, so the problem is with this command and now I'm thinking:
It's a problem with scripts on version 7.6?
It's because the image is in USB drive (but via terminal the process works...)?
User problem (it's admin), or it's because DNSMASQ_USER is seted with root user?

P.S: All process described here were made with Winbox and RouterOS version 7.6
/container/stop number=0
/delay 15s
/container remove numbers=0
/delay 300s
/container/add remote-image=pihole/pihole:latest interface=Docker root-dir=disk1/pihole mounts=dnsmasq_pihole,etc_pihole envlist=pihole_envs
/delay 300s
/container/start number=0
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script VS Container

Mon Dec 05, 2022 1:09 am

simply you can not use index numbers on commands,
are only for terminal

you must use container name
 
User avatar
EvertonSSJ4
just joined
Topic Author
Posts: 5
Joined: Thu Aug 18, 2022 1:46 am

Re: Script VS Container

Mon Dec 05, 2022 1:42 am

Thanks for your time answering me.

About your explanation, is something like this?:
/container remove name="71211403-2816-4401-bfc2-d7956679e20f"
I have tried this way and with number or numbers (in place of name), but without result.
Last edited by BartoszP on Mon Dec 05, 2022 3:09 am, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script VS Container

Mon Dec 05, 2022 2:50 am

If you have only one, you can use find to remove them "all":
/container stop [find]
/container remove [find]
Note: you need to stop it before removing one.

[find] takes "what to filter for", but if you provide find nothing, it means everything. If you want to remove just one by name use:

[find name="71211403-2816-4401-bfc2-d7956679e20f"]

Or you can get cleaver and do it by "tag" (e.g. the source docker name):

[find tag="pihole/pihole:latest"]
 
User avatar
EvertonSSJ4
just joined
Topic Author
Posts: 5
Joined: Thu Aug 18, 2022 1:46 am

Re: Script VS Container

Mon Dec 05, 2022 3:12 am

If you have only one, you can use find to remove them "all":
/container stop [find]
/container remove [find]
Note: you need to stop it before removing one.

[find] takes "what to filter for", but if you provide find nothing, it means everything. If you want to remove just one by name use:

[find name="71211403-2816-4401-bfc2-d7956679e20f"]

Or you can get cleaver and do it by "tag" (e.g. the source docker name):

[find tag="pihole/pihole:latest"]
Thank you so much, works like a charm.
With "find + tag" ([find tag="pihole/pihole:latest"]) seen's more objective (even for me running only one container).
Thanks again.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script VS Container

Mon Dec 05, 2022 7:00 pm

Great!

To get more complex, you can wait for the container to stop. Since the remove will fail if the container is not stopped, but the "stop" just requests the container to shutdown. It may not do so right aways, so the remove can fail. Also use a variable to store the "tag" you want to use, and a "~" which mean regex (or partial string matching, so just "pihole" will match on "pihole:latest" or any "sub-tag") for an even better example in case it wasn't "pihole" you want to do this with.
:global mytag "pihole"
/container stop [find tag~$mytag]
:do { :delay 1s } while=([/container get [find tag~$mytag] status ]!="stopped")
/container remove [find tag~$mytag]
e.g. the :do waits for the stop, which be important if you want to do this as part of a larger script. Interactively at Terminal, you can just retry the remove if it fails.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script VS Container

Mon Dec 05, 2022 7:12 pm

And for fun, an alternative style is the following, where you can use a "command block" so that the stuff inside the { } assumes the /container part:
:global mytag "pihole"
/container {
  stop [find tag~$mytag]
  :do { :delay 1s } while=([get [find tag~$mytag] status ]!="stopped")
  remove [find tag~$mytag]
}
I normally wrapped things in functions. A function avoids needing a global and cut-and-paste - especially if you need to do it multiple times/interactively:
:global "remove-container-by-tag" do={
  :local mytag $1
  /container {
    stop [find tag~$mytag]
    :do { :delay 1s } while=([get [find tag~$mytag] status ]!="stopped")
    remove [find tag~$mytag]
  }
}
The "function" above does nothing – just just defines a new command-like thing to call to remove a container by tag. So if that add at top of script (or via scheduler at startup), you can then just use:
$"remove-container-by-tag" pihole
 
User avatar
EvertonSSJ4
just joined
Topic Author
Posts: 5
Joined: Thu Aug 18, 2022 1:46 am

Re: Script VS Container

Mon Dec 05, 2022 7:44 pm

@Amm0

Great!
I have put an daley (300s) to wait the stop command, but with this possibilities you told, I'll try other ways and with that, refine the script. Thanks a lot for your time and good will.

Who is online

Users browsing this forum: No registered users and 21 guests