What is the best way to update a Docker image without destroying container configurations? reddit / selfhosted ---------------------------------------------------------------------- Significant-Neat7754 · 2023-11-05 16:06 I have Docker container installed for things like home assistant. Now, if I pull a new, updated Docker image for home assistant, will the new container, which I will have to generate after deleting the old one, keep using the old/saved config as long as I point it to the same config directory? Or do I have to set everything up all over again? What is the best practice regarding this? Thanks for helping. ---------------------------------------------------------------------- thekrautboy · 2023-11-05 16:14 If it was set up correctly, then all important persistent data should be stored on the host. So restarting, updating, destroying the container should keep your data. Refer to the instructions of the exact image you use. `docker compose pull` to check for and if available pull a new image `docker compose down` to shut down the current container ` docker compose up -d` to start a new container using the compose config and the new pulled image. This assumes you are using Docker compose, which you should. And that you dont pin the image used to a specific version tag. Refer to the Docker documentation and ask /r/Docker ---------------------------------------------------------------------- redditfatbloke · 2023-11-05 16:19 Click on the container to update. Stop the container. Click recreate, confirm you want to repull the new image, restart the container. ---------------------------------------------------------------------- flaming_m0e · 2023-11-05 16:19 `docker compose down` isn't required actually. If there was an updated image when you `pull`, then doing `docker compose up -d` will restart only the affected containers with the updated image. It will also handle dependencies if you use `depends_on` and restart those necessary items as well. I have a big Home Assistant stack that I've been managing this way for 7 years. There are containers within the stack that aren't dependencies that don't need to be shut down every time I update the others. ---------------------------------------------------------------------- Significant-Neat7754 · 2023-11-05 16:20 Thanks. I'll check those out. I'm using Docker and not Docker compose. ---------------------------------------------------------------------- thekrautboy · 2023-11-05 16:21 True but i find the small extra step makes it clearer for beginners :) ---------------------------------------------------------------------- Significant-Neat7754 · 2023-11-05 16:21 That seems very convenient. Does Docker have the same features? I haven't used Docker compose to set it up. Maybe I should have used Docker compose, in retrospect. ---------------------------------------------------------------------- thekrautboy · 2023-11-05 16:22 > I'm using Docker and not Docker compose. I hope the same rules apply for Docker too. As basics yes. But again, you should really use Compose instead. ---------------------------------------------------------------------- thekrautboy · 2023-11-05 16:23 Where exactly are you clicking? Are you just assuming that OP uses Portainer? Or Docker Desktop? ---------------------------------------------------------------------- flaming_m0e · 2023-11-05 16:23 > Does Docker have the same features? Compose is just a "recipe book" for Docker. It's not a separate runtime. >Maybe I should have used Docker compose, in retrospect. You still can. You just convert your `run` command to a compose file and start it up. This isn't an either/or situation. Compose is part of Docker. ---------------------------------------------------------------------- JustSummGuy · 2023-11-05 16:29 Install watchtower ---------------------------------------------------------------------- pigers1986 · 2023-11-05 16:34 Assuming you have some volumes/binds for that docker (and they are setup properly) than you should stop container, pull new one and start new one with the same runtime parameters ```docker run ... ``` (maybe there will name conflict .. than append 2 to new container). If I were you, I'd convert that docker run to docker compose - so you will not have such problems in future! https://www.composerize.com/ (example with zigbee2mqtt and related stuff https://pastebin.com/4Kx1RsLn) ---------------------------------------------------------------------- clintkev251 · 2023-11-05 16:38 Work on moving everything over to docker compose. I really wouldn't recommend spinning up containers using `docker run` outside of testing or for containers that are intended to be short lived. As you've discovered it's a pain to manage updates and configuration changes for containers that were created using `docker run`. Compose will allow you to just put all your configurations into a file so you don't have to dig through your history every time you want to make an update ---------------------------------------------------------------------- CatTypedThisName · 2023-11-05 16:38 A containers filesystem is ephemeral, meaning that all changes made to the container are discarded once it's rebooted. This is where volumes come in. Make sure you create a directory on your host where you want your data to persist. Then when you spin up your container, tell it to use a volume which points to this directory. For example, lets say you have \`/mnt/data\` on your host and you want it to be mounted in \`/mnt/homeassistant\`. Depending on how you're booting the container you would just tell Docker to mount this as a volume. If you're doing it on the CLI it's like this: \`docker run -v /mnt/homeasisstant:/mnt/data\` Docker compose is nice because you can have a file representation of the command you need to run which allows you to store and keep your configuration long term (in something like git). Here's more info on docker volumes: [https://docs.docker.com/storage/v olumes/](https://docs.docker.com/storage/volumes/) Please feel free to ask more Q's, I work as a staff eng at a big ecommerce shop, will happily share any notes I have. Good luck and don't give up! ---------------------------------------------------------------------- Cool-Respect-7545 · 2023-11-05 16:57 As was previously suggested, I highly recommend switching to docker compose. Below is an example. Note the “volumes” node which persists your configuration.yaml, automations.yaml, etc. on the host at /opt/apps/ha/config. I run a weekly python script (pertinent commands at bottom) that updates all of my container images. homeassistant: container_name: ha image: "ghcr.io/home-assistant/home-assistant:stable" ports: - "8123:8123" volumes: - /opt/apps/ha/config:/config - /etc/localtime:/etc/localtime:ro devices: # zigbee USB device - /dev/ttyUSB0:/dev/ttyUSB0 restart: unless-stopped privileged: true networks: - rodham-net Weekly update commands: exec_cmd("sudo apt update") exec_cmd("sudo apt full-upgrade -y") exec_cmd("sudo apt autoremove -y") exec_cmd("sudo docker-compose -f /opt/admin/docker-compose.yaml pull") exec_cmd("sudo docker-compose -f /opt/admin/docker-compose.yaml up -d") exec_cmd("sudo docker image prune -af") exec_cmd("sudo docker volume prune -f") ---------------------------------------------------------------------- redditfatbloke · 2023-11-05 16:58 Sorry yes, this uses portainer. Apologies I wasnt more clear. It is a very quick and easy fix for the problem, and portainer is a common way to manager docker containers. Not sure why I getting downvoted, it was a genuine attempt to help. ---------------------------------------------------------------------- thekrautboy · 2023-11-05 17:06 > Not sure why I getting downvoted, it was a genuine attempt to help. Probably because you assumed OP was using a specific additional tool without mentioning it, likely confusing OP by saying to click on things when the topic is about a commandline tool? ---------------------------------------------------------------------- indomitus1 · 2023-11-05 17:23 Watchtower. Especially if you run quite a few containers. I would be impossible for me otherwise to keep uo ---------------------------------------------------------------------- thekrautboy · 2023-11-05 17:24 Tiny hint: You can pull before stopping/restarting, depending on the size of the image and download speed, this can reduce downtime of the service. ---------------------------------------------------------------------- thekrautboy · 2023-11-05 17:27 Sorry but without proper formatting this is useless. ---------------------------------------------------------------------- Ursa_Solaris · 2023-11-05 17:57 I **strongly** recommend learning Docker Compose. It's just a better way to build and run a Docker container using a persistent config file written in a clean human-readable language (YAML) instead of a long messy command. It completely solves your issue, and it's just a much saner way to maintain containers. Most projects will offer a `docker- compose.yml` example these days, and all you really have to do in most cases is edit the variables. It is especially valuable for projects with multiple components, because you can define them all in one file, usually called a "stack", and control them as a single unit. At work, one of our stacks has 10 separate containers in it. Trying to control these individually would be a nightmare. And the best part? A `docker-compose.yml` file is self-documenting. Everything you need to know about the container is written write there; the ports, the variables, the volumes, everything. Just trust me, if you want to get into Docker, you really need to learn this. It's easy and it pays dividends by orders of magnitude. ---------------------------------------------------------------------- pigers1986 · 2023-11-05 21:11 Hhint is good (im already running it in compose so no problem here). ---------------------------------------------------------------------- tech2but1 · 2023-11-05 22:21 This is another subject that is confusing when lots of people say "docker isn't for persistent storage, all your data should be saved elsewhere" but then doing an update essentially destroys your container but your data stays. From what I can gather there are 2 docker factions, those that say it is a development platform, and therefore everything is temporary. But then this isn't true as there are many apps released on docker only (with little to no guidance on using docker (somewhat understandably)) that are very much not temporary, quite the opposite. ---------------------------------------------------------------------- sjveivdn · 2023-11-06 01:53 You should start using compose ---------------------------------------------------------------------- RushTfe · 2023-11-06 02:03 I recommend you to use docker compose. You can translate your docker command to a docker compose, which end up being much more convenient, plus you can put your compose projects on git. ---------------------------------------------------------------------- gromhelmu · 2023-11-06 07:05 > This assumes you are using Docker compose, which you should. And that you dont pin the image used to a specific version tag. You should _always_ PIN your image to a specific version tag in production. If you want to update, then 1. edit the `docker- compose.yml`, 2. Change the version tag to the new version, 3. use `docker compose pull` to get the new version. ---------------------------------------------------------------------- thekrautboy · 2023-11-06 08:02 Imo that practice depends entirely on the specific type of software being used in that image, not as a general rule. Also some projects dont even provide version-tagged images and there is only *latest* to use. ---------------------------------------------------------------------- TBT_TBT · 2023-11-06 09:18 On databases yes, on all other containers imho no. I run very well with :latest and Watchtower on non-database containers. ---------------------------------------------------------------------- gromhelmu · 2023-11-06 09:26 Watchtower is a specific case. OP recommenda `latest` without Watchtower, which is why I posted this warning. If you know the side- effects of `latest` - go ahead, but for anyone starting, I recommend sticking with something that is not changing spontaneously. Even with Watchtower, you may want to pin major versions and only upate minor ones automatically. E.g. for nextcloud, I use `image: nextcloud:27-apache`, which updates minor versions automatically with Watchtower, but waits for my presence in case of major ones (27->28). ---------------------------------------------------------------------- TBT_TBT · 2023-11-06 09:28 That is certainly good advice 👍 and good practice. ---------------------------------------------------------------------- gromhelmu · 2023-11-06 09:28 I think as a general rule, having pins should certainly be the default. Exceptions exist and if you know the consequences, go ahead and skip this default rule. Projects that don't provide version-tagged images are usually not production-ready, imho. ---------------------------------------------------------------------- FishSpoof · 2023-11-06 11:52 Docker compose pull Then Docker compose up -d ---------------------------------------------------------------------- PaulEngineer-89 · 2023-11-06 16:42 Containers are indeed a fantastic development tool but that’s not the only use. Docker in particular provides a consistent environment to server applications allowing deployment in an almost unlimited number of environments. It also functions as an “appliance” eliminating the most difficult installation hassles, as well as security and network features that are clearly anything but developer features. Basically given a choice between traditional service installation on a server (never mind VM) compared to a light weight container, the container wins. Granted LXC/KVM exists but it is far more heavy weight.