# 📚 How to update your Docker containers safely

## Purpose

Learn how to update your Docker containers safely, minimizing downtime and preventing accidental data loss.

---

## 1. Check Running Containers

List all currently running containers to know what services are active:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div>```bash
docker ps
```

---

## 2. Pull the Latest Images

For each image you want to update, pull the latest version from the registry:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-1"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div>```bash
docker pull imagename:tag
```

*Replace `imagename:tag` with your actual image name and tag.*

---

## 3. Stop and Remove Old Containers

Stop the container without deleting its data volumes:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-2"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div>```bash
docker stop containername
docker rm containername
```

*Replace `containername` with your actual container name.*

---

## 4. Start New Containers with Updated Images

Use your original `docker run` command or your `docker-compose` file to launch the updated containers.

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-3"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div>```bash
docker run -d --name containername imagename:tag
```

---

## 5. Verify Everything is Working

After starting the new containers, check that everything is healthy:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-4"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div>```bash
docker ps
docker logs containername
```

---

## ✅ Summary

- **Always backup your data volumes** before updating if possible.
- **Use versioned tags** (e.g., `nginx:1.25`) instead of `latest` to avoid unexpected changes.
- **Use compose files** to automate container recreation safely.