[Docker] Delete only the volume associated with a specific container

I want to delete only a specific volume

With $ docker volume prune, all unused volumes will be deleted, and even if you look at $ docker volume ls, you can't tell which volume you want to delete.

Set the container name with the volume you want to delete

$ CONTAINER_NAME=hoge

When you don't know the container name

$ docker ps
CONTAINER ID        IMAGE           COMMAND                  CREATED             STATUS              PORTS                    NAMES
1fd9075c0f5d       my_project_app   "docker-entrypoint.s…"   59 minutes ago      Up 59 minutes       0.0.0.0:8080->8080/tcp   app

The value of NAMES in the last column is the container name

One-liner delete command

$ docker inspect $(docker ps -f name=${CONTAINER_NAME} -q) | jq -r '.[].Mounts[] | select(.Type == "volume").Name' | xargs docker volume rm

Overview

--Get container ID from container name --Get volume name with a combination of docker inspect and jq --Delete with docker volume rm {volume name}

Simpler case

If you want to delete volume at the same time as deleting the container

docker-compose down -v $CONTAINER_NAME

Only when using docker-compose, but this is easier

Recommended Posts

[Docker] Delete only the volume associated with a specific container
How to start a Docker container with a volume mounted in a batch file
[Docker] How to see the contents of Volumes. Start a container with root privileges.
Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
How to delete child elements associated with a parent element at the same time
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
Create a Docker image with the Oracle JDK installed (yum
Wait for the container service to start with docker healthcheck
Run PHP-FPM with OPcache enabled in a Read Only container
Get only the ID of the container specified by docker ps
Time is wrong with the application launched on the Docker container
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
I tried deploying a Docker container on Lambda with Serverless Framework
Create a Vue3 environment with Docker!
Deploy a Docker application with Greengrass
Build a Node.js environment with Docker
Run PureScript on a Docker container
[Linux] Start Apache container with Docker
Build WebRTC Janus with Docker container
Make a daily build of the TOPPERS kernel with Gitlab and Docker
Starting with installing Docker on EC2 and running Yellowfin in a container
Run x11 apps in a Docker container (supports network access from the container)
How to limit IP address only to a specific directory with laravel + nginx
I tried running the route search engine (OSRM) easily with a container
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Create a jar file with the command
Build a PureScript development environment with Docker
The story of updating SonarQube's Docker Container
Create a MySQL environment with Docker from 0-> 1
Run a DMN with the Camunda DMN Engine
[docker] [nginx] Make a simple ALB with nginx
Build a Wordpress development environment with Docker
[Docker] Start the container as soon as possible
[Docker] Start container, start bash in container, delete image
Up to the point of launching a Docker container built using RedHat Quarkus
Monitor the Docker container and SystemD process on the same host with Zabbix on Ubuntu.
A quick note on using jshell with the official Docker image of the JDK
[Part 1] Creating a Docker container that delivers Markdown in HTML with Apache / Pandoc
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC