Amanda Ferrari
Amanda Ferrari

Reputation: 946

Avoid containers to shutdown if machine is rebooted

I have a OSX, and I would like to know if is possible to persist a container between OS reboots. I'm currently using my machine to host my code and using it to install platforms or languages like Node.js and Golang. I would like to create my environment inside a container, and also leave my code inside it, but without losing the container if my machine reboots. Is it possible? I didn't find anything related.

Upvotes: 0

Views: 183

Answers (1)

Adiii
Adiii

Reputation: 59936

  1. Your container never killed if your system reboot except you start container with --rm which will remove on stop.
  2. Your container will restart automatically if you start container with docker run -dit --restart always my_container
  3. As per " also leave my codes inside it" this question is concern there is two solution to avoid loss of data or code and any other configuration.

You lose data because

It is possible to store data within the writable layer of a container, but there are some downsides:

The data doesn’t persist when that container is no longer running, and it can be difficult to get the data out of the container if another process needs it.

https://docs.docker.com/storage/

So here is the solution.

Docker offers three different ways to mount data into a container from the Docker host: volumes, bind mounts, or tmpfs volumes. When in doubt, volumes are almost always the right choice. Keep reading for more information about each mechanism for mounting data into containers.

https://docs.docker.com/storage/#good-use-cases-for-tmpfs-mounts

Here how you can persist the nodejs code and golang code

docker run -v /nodejs-data-host:/nodejs-container -v /go-data-host:/godata-container -dit  your_image

As per packages|runtimes (nodejs and go) is the concern they persist if your container killed or stop because they store in docker image.

Upvotes: 3

Related Questions