Volumes#
By default, Docker containers don’t retain any state from past containers. For example, if I:
- Start a container from an image
- Make some changes to the filesystem (like installing a new package) in that container
- Stop the container
- Start a new container from the same image
- The new container does not have the changes I made in step 2.
However, if I restart the stopped container, it will have the changes I made. This is only worth mentioning because sometimes developers think that killing an old container and starting a new one is the same as restarting a process - but that’s not true… it’s more like resetting the state of the entire machine to the original image.
All this said, Docker does have ways to support “persistent state” through storage volumes. They’re basically a filesystem that lives outside of the container, but can be accessed by the container.
Create a new empty volume called ghost-vol:
docker volume create ghost-volMake sure it worked:
docker volume lsInspect the volume to see where it is on your local machine:
docker volume inspect ghost-volDocker hosts an official image for Ghost on Docker Hub.
Pull the Ghost image from Docker Hub:
docker pull ghostRun the Ghost image in a new container:
docker run -d -e NODE_ENV=development -e url=http://localhost:3001 -p 3001:2368 -v ghost-vol:/var/lib/ghost ghost-druns the image in detached mode to avoid blocking the terminal.-e NODE_ENV=developmentsets an environment variable within the container. This tells Ghost to run in “development” mode (rather than “production”, for instance)-e url=http://localhost:3001sets another environment variable, this one tells Ghost that we want to be able to access Ghost via a URL on our host machine.- We’ve used
-pbefore.-p 3001:2368does some port-forwarding between the container and our host machine. -v ghost-vol:/var/lib/ghostmounts theghost-volvolume that we created before to the/var/lib/ghostpath in the container. Ghost will use the/var/lib/ghostdirectory to persist stateful data (files) between runs.
- Navigate to
http://localhost:3001/in your browser, you should see your new Ghost CMS!
Remove docker volume#
Use docker ps -a to see all containers, even those that aren’t running.
Stop the running Ghost container
Remove the ghost container. Use docker --help to find the right command.
Remove the ghost-vol volume. Use docker volume --help to find the right command.
Now that it’s gone, let’s see what happens if we try to start the Ghost container back up and attach it to a volume that doesn’t exist.
docker run -d -e NODE_ENV=development -e url=http://localhost:3001 -p 3001:2368 -v ghost-vol:/var/lib/ghost ghostNavigate to http://localhost:3001/ in your browser, and you should see a fresh CMS. That’s weird, why no errors?
Run:
docker volume lsThe ghost-vol is back from the dead!?! It turns out the -v ghost-vol:/var/lib/ghost flag binds to a “ghost-vol” volume if it exists, otherwise, it creates it automatically!
So, we now have a fresh installation. Our post that was on the old volume is gone, but this new volume will persist if we don’t delete it.
Bu çıktı, docker ps -a listesidir.
İçinde:
- İlk satır (62a79dd0b7e4) → aktif çalışan container (
Up 11 minutes). - Diğerleri → durdurulmuş (Exited).
Tümünü - çalışan ve durdurulmuş dahil - sadece ID olarak bastırmak için:
docker ps -aqSilmek istersek:
- Sadece durdurlumus state’de olanlar:
docker ps -aq -f status=exited | xargs docker rm- Hepsi :
docker rm -f $(docker ps -aq)Bu ikinci komut tüm container’ları durdurur ve siler.
Var olan bir volume üzerine compose ile devam etmek için var-olan-volume-uzerine-docker-compose sayfasına bak.