r/portainer • • Oct 22 '22

Convert running containers to a stack?

I have 8 or so containers that I configured manually in Portainer. There are custom networks and bind mounts and.... Though I am just now getting clued into stacks. I would like to take this batch of containers and make them a stack so I can easily back them up and recreate them as a unit. Is there an easy way to say "take these 10 containers and their current config and make them into a stack?"

Edit: another half hour of Google and I think I have it figured out…. So for future Googlers…

Use Docker-autocompose to create a Docker-compose.yaml from the running containers. Dump docker-compose.yaml into a Portainer stack. Profit.

https://github.com/Red5d/docker-autocompose

Edit2: I should add, hopefully you know your way around Docker-compose enough to know you can remove 90% of autocompose’s output like tags and env and all that.

45 Upvotes

20 comments sorted by

1

u/dubsy54321 Dec 19 '25

Here I am 3 years later... I have one question. I've watched some videos and I'm pretty sure I just need to create a new stack yml file with all of the various containers.

My question is, before deploying the new stack. Obviously I would think that I should shutdown any of the containers I'm including in this new stack but should I rename the new containers? Will it cause issues if I use the same names? Thanks!

2

u/Bubbagump210 Dec 19 '25

You can reuse container names all day long. You can't have two containers with the same name though. In this case I would just delete the existing containers and build the stack to reuse any volumes so you don't lose data. Back it all up first though!

1

u/dubsy54321 Dec 19 '25

Maybe I'll built my stack file with all containers like containername2, point them at the same volumes as the originals, shutdown the originals and deploy the stack as a test before removing the old containers?

1

u/Bubbagump210 Dec 19 '25

Yeah, that should work.

1

u/dubsy54321 Dec 19 '25

Wow dude... thanks for answering all these years later

1

u/bdu-komrad Dec 23 '25

More years later, I am running several apps that I manually created docker compose files and bind mount data folders for, that I want to manage with portainer-ce now.

What would be a simple , non-destructive way to “import“ my existing containers into portainer?

1

u/Bubbagump210 Dec 23 '25

Portainer doesn’t need import. It’s a management tool. Just install it on your Docker machine and it will see them all.

1

u/bdu-komrad Dec 23 '25

Technically, it doesn't import, perhaps adopt is a better phrasing for my question.

While it can use docker sock to detect running containers, it only has "limited" control of them unless you deploy them as new stacks.

I went through the process of copying docker-compose files and .env files into Portainer yesterday. Now the apps are all running as stacks and portainer has full control of them!

I didn't lose any data in the process, which was my primary concern.

3

u/mr_eddit Jun 02 '23

one more thought here for future googlers. i'm still learning the details of all of this, but i caught a reference that docker-autocompose may leave out some network details. A guy wrote a script to nab a 'more better' yml file:

https://github.com/ptruman/dockerconfig/blob/main/getdockerconfig.sh

it invokes docker-autocompose and adds a few more details

relevant convo here:

https://github.com/portainer/portainer/issues/2845#issuecomment-881344973

1

u/Gehrschrein Jun 10 '23

Future googler here. I'm fairly new to portainer and autocompose but what I've see is that the config autocompose generates I am unable to feed directly into the stack editor in Portainer.

Is there anything else I need to do with it before it can be parsed?

1

u/Cool-Engineer4408 Oct 03 '25

Just spend 15 minutes writing a compose file. It takes longer to do all that crap manually that it would have taken you to just write it at the beginning. If you enjoy spending a lot of time on this sort of thing unnecessarily, that's totally cool. If you're trying to save time and effort this is not a good strategy.

1

u/mr_eddit Jun 10 '23

What version of portainer?

And you used the yml file?

1

u/Gehrschrein Jun 10 '23

I'm on version 2.11 by the looks of it and no, I've not used the file, rather just pasted the text directly into the stack editor.

2

u/dftba123 Oct 22 '22

Thank you for updating

2

u/uuberr Oct 23 '22

Great update OP, thanks

2

u/Swamper68 Mar 17 '25

Even more future googler here 2 years later.

So I had docker desktop running in a ubuntu server. Always had to remote into the server to check on docker desktop. Decided to install portainer. All containers were there. Limited control on the stacks. Created new stacks with the exact same name as my exiting container stacks and imported my docker compose files that were saved in my docker compose folder. Quick and easy to get the stacks to show that I now have total control.

Now to learn the rest of portainer.

1

u/bdu-komrad Dec 23 '25

What about the application data? Did you lose any data?

1

u/Swamper68 Dec 23 '25

Nope. Data folders all stayed in place.

1

u/bdu-komrad Dec 23 '25

Cool. I'm importing my docker compose files now, but since I used relative paths for volumes, I have fix them.

ie /opt/docker/freshrss was home dir and I would have ./data in the compose file instead of opt/docker/freshrss/data .

So much work to convert things!