Containers have a "scratch space" that allows you to store files only temporarily, meaning as long as the container is running. But once the container stopped the data is gone.
Volumes are a way to persist information even after a container got destroyed. A volume is a special type of directory in a container (a mount or alias) which maps back to a folder on the host. That can be done in one of two ways: Using named volumes or bind mounts.
Volumes can be shared and reused among containers.
The following command will create a volume that mounts the container's
/var/www folder to a default folder location at the host system:
# mount host's default location to container's /var/www docker run -p 8080:3000 -v /var/www node
docker inspect <container> to look up the paths that are mounted from the host to the container (Listed under "Mounts").
Another way to create a Volume is to give your volume a name like
todo-db (named volume):
# create named volume docker volume create todo-db # run container using named volume (works even without creating named volume before) docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
# run container with volume that has host's current working directory pointing to container's /var/www docker run -p 8080:3000 -v $(pwd):/var/www node
To remove a container including its volume run
docker rm -v mycontainer.
To run a command in the docker container you first have to specify a working directory with working directory argument (-w), followed by the image name and the command:
docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ node:12-alpine \ sh -c "yarn install && yarn run dev"
docker volume inspect todo-db