Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can I persist docker container?#7097

Unanswered
Apr 12, 2023· 4 comments· 8 replies
Discussion options

In my use case, we are not familiar with changing template each time, so I would like to keep the container there when stopping, and restart the container when starting, instead creating a new one.
I tried to addlifecycle { ignore_changes = all } to container, but the workspace cannot restart after a stop.

You must be logged in to vote

Replies: 4 comments 8 replies

Comment options

You should look at mounting volumes to persist your workspace directories. See this template for example.

https://github.com/matifali/coder-templates/blob/main/deeplearning/main.tf

You must be logged in to vote
3 replies
@am009
Comment options

I know that the usual way is to mount volumes to persist workspace, and we are using it. but I still want the possibility to keep the container, so that I can use it like an VM, and use the most basic template for everything, although it might cost more storage.

@matifali
Comment options

Try settingcount = 1 fordocker_container resource.
This will prevent coder from deleting the container.

@am009
Comment options

I tried it right now, but it doesn't seem to work. When I stop the workspace, the container doesn't stop at all, and I can still connect to it. When I restart the workspace, the container is still recreated. (My test file under root disapperred, and the container ID changed.)

Comment options

I addedlifecycle { ignore_changes = all } (docs) for docker_container resource. Then I stop the workspace, the code server seems to be stopped (I cannot connect to it.), the container still doesn't stop. Then I manually stop the container. Then restart the workspace, the container was started as expected (not recreated).

BUT, if I don't stop the container after stopping the workspace, and start the workspace, wired things happen, unable to connect (the ui keeps loading,ps -aux in container shows two code-server instances).

Probably, coder should stop all container when stopping a workspace? By the way, I see nothing related to stopping a container interraform docs. Probably terraform simply doesn't support stopping a container? Can anyone tell me is that correct?

You must be logged in to vote
4 replies
@matifali
Comment options

I am not sure, but there are some options relating to stopping a container here,https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container
if you search forstop keyword.

@am009
Comment options

I got a feeling that the stop in the doc is only used for (before) destroy.

@matifali
Comment options

https://discuss.hashicorp.com/t/docker-provider-restart-stopped-containers-instead-of-recreating/7365
Something similar to what you are looking for. But no responses.

@am009
Comment options

Thanks. Probably this is the case.

Comment options

You cannot persist a container on workspace stop. Even if you somehow prevent terraform from destroying it it will be recreated/replaced on the next workspace restart.
For persistence you should mount volumes to the directories you need.

You must be logged in to vote
0 replies
Comment options

I hit this same issue as well. Let me provide a little more usecase info to justify the need.

We are using the Docker Container template w/a-few tweeks. We like that, it's fast! As opposed to DevContainer (EnvBuilder).

We have a case where we use a mostly complete image, but we want to add some addtl things (like vscode extensions and a few other things).
We'd prefer not modify the image we are using because other folks use the same image in a fashion that doesn't relate to the coder experience.
So, the semantics around workspace lifecycle are odd for us, as we spend time "peppering" the container on workspace start up and all that "peppering" is lost on a workspace stop.
The end-user goes to start their auto-stopped workspace and their food tastes bland (no pepper!), well not really, but they have to wait for the pepper to be applied to the new container. Not that long but, still we want fastness :) .

Here's the work-around that might-be-awful or might-not-be (we'll see I guess)

add this to the bottom of your Container template

resource "null_resource" "docker_control" {  triggers = {    always_run = timestamp()  }  provisioner "local-exec" {    command = <<-EOT      if [ "${data.coder_workspace.me.transition}" = "start" ]; then        docker start ${docker_container.workspace[0].name}      elif [ "${data.coder_workspace.me.transition}" = "stop" ]; then        docker stop ${docker_container.workspace[0].name}      fi    EOT  }  depends_on = [docker_container.workspace, data.coder_workspace.me]}

now, you must also change the count variable on your "workspace" resource

resource "docker_container" "workspace" {  count = 1

The full life cycle as tested is this...

  1. You create the ws, container starts as expected. (confirmed withdocker ps -a)
  2. You stop the ws, container w/count=1 remains, null resource kicks in and issues a stop (also confirmed w/docker ps -a) NOTE: coder Ui is confused, cuz it thinks there is a container, but really the null_resource kicks in and is stopping the container (see above )
  3. You start the ws, container w/count=1 is still there-but-stopped, now your null_resource kicks in and starts the container. Coder UI is now happy cuz it finds what it expects, the container running.

I fear what happens if a stop doesn't actually stop? Maybe that's not a thing, but does seem like there is some potential for state inversion sync problem.

We'll see I guess.
I would like to see this as a feature though, yes I know this is partly a Terraform thing and partly a provider:docker thing, but I suspect there's something doable here?

You must be logged in to vote
1 reply
@NENightElves
Comment options

Hi there. I tried your method and found it does not work. In the full life cycle you tested, the step 1 and 2 worked as expected. However, the step 3, the Coder seems to receate the workspace and then execute null_resource. This leads to the recreation of the container, and thus the data is destroied.
Maybe it should be added some other configuration such aslifecycle andignore_changes, but I do not know how to do. Do you have some ideas? Thanks.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
4 participants
@am009@NENightElves@matifali@alfred-stokespace

[8]ページ先頭

©2009-2025 Movatter.jp