Stop and remove all docker containers

How can I stop and remove all docker containers to create a clean slate with my Docker containers? Lots of times I feel it is easier to start from scratch, but I have a bunch of containers that I am not sure what their states are, then when I run docker rm it won't let me because the docker container could still be in use.

144934 次浏览

Here's a good gist I use for this kind of thing: From this link that people seem to not like (https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430)

delete volumes

$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm

Docker introduced new namespaces and commands which everyone should finally learn and not stick to the old habits. Here is the documentation, and here are some examples:

Deleting no longer needed containers (stopped)

docker container prune

delete networks

$ docker network ls
$ docker network ls | grep "bridge"
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')

remove docker images

Deleting no longer needed images

which means, that it only deletes images, which are not tagged and are not pointed on by "latest" - so no real images you can regularly use are deleted

docker image prune

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')

remove docker containers

Delete all volumes, which are not used by any existing container

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")

( even stopped containers do claim volumes ). This usually cleans up dangling anon-volumes of containers have been deleted long time ago. It should never delete named volumes since the containers of those should exists / be running.

Essentially you want to kill all your running containers, remove every image, uninstall docker, reinstall the version you want and that should be about as clean a slate as it gets.

Same for unused networks

docker network prune

The one liner:

docker rm -f $(docker ps -a -q)

And finally, if you want to get rid if all the trash - to ensure nothing happens to your production, be sure all stacks are running and then run

docker system prune

or

docker ps -aq | xargs docker rm -f
ld suggest are in following order,

then when I run docker rm it won't let me because the docker container could still be in use.

The steps I would suggest are in following order,
1. Try to remove

docker rm <container-id>
1. Try to remove

docker rm <container-id>

2. rm doesn't work, use stop

docker stop <container-id>

2. rm doesn't work, use stop

docker stop <container-id>

3. stop doesn't work? try kill

docker kill <container-id>

3. stop doesn't work? try kill

docker kill <container-id>

4. stop worked but still container is there? try prune to remove all the stopped container forcefully

docker container prune -f

Stop all the containers

docker stop $(docker ps -a -q)

Remove all the containers

docker rm $(docker ps -a -q)

We can achieve this with one liner (also removes running containers)

docker container rm $(docker container ls -aq) -f

What it does

Find more command here

docker container ls -aq lists container's ids only

bly removes all container's ids

Alternatively, you can run only the the commands to remove the containers with rm --force, but keep in mind that this will send SIGKILL, so running stopfirst and then rm is advisable (stop sends SIGTERM, unless it times out, in which case it sends SIGKILL).

If you only want to do the running ones, remove -a.

then when I run docker rm it won't let me because the docker container could still be in use.

To remove all Docker images:

docker rmi $(docker images -aq)

The steps I would suggest are in following order,
d still be in use.

1. Try to remove

docker rm <container-id>

The steps I would suggest are in following order,

2. rm doesn't work, use stop

docker stop <container-id>
1. Try to remove

docker rm <container-id>

3. stop doesn't work? try kill

docker kill <container-id>

2. rm doesn't work, use stop

docker stop <container-id>

3. stop doesn't work? try kill

docker kill <container-id>

4. stop worked but still container is there? try prune to remove all the stopped container forcefully

docker container prune -f

docker container ls -aq lists container's ids only

docker container rm $(..) -f forcibly removes all container's ids

hey do not. It's funny because only the tests from the lazy loading module are failing...

I'm developing a github repository (with angular 7 and angular-cli), and I have some tests with Karma and Jasmine working in the master branch.

Here is the code and the error:

import {async, TestBed} from '@angular/core/testing';
import {APP_BASE_HREF} from '@angular/common';
import {AppModule} from '../../app.module';
import {HeroDetailComponent} from './hero-detail.component';


describe('HeroDetailComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [AppModule
],
providers: [
{provide: APP_BASE_HREF, useValue: '/'}
],
}).compileComponents();
}));


it('should create hero detail component', (() => {
const fixture = TestBed.createComponent(HeroDetailComponent);
const component = fixture.debugElement.componentInstance;
expect(component).toBeTruthy();
}));
});

Now I'm trying to add lazy loading feature, the thing is, that the tests that before passed, now they do not. It's funny because only the tests from the lazy loading module are failing...

The error is this:

Chrome 58.0.3029 (Mac OS X 10.12.6) HeroDetailComponent should create hero detail component FAILED
Error: Illegal state: Could not load the summary for directive HeroDetailComponent.
at syntaxError Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:1690:22)
at CompileMetadataResolver.getDirectiveSummary Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:15272:1)
at JitCompiler.getComponentFactory Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:26733:26)
at TestingCompilerImpl.getComponentFactory Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler/testing.es5.js:484:1)
at TestBed.createComponent Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/core/@angular/core/testing.es5.js:874:1)
at Function.TestBed.createComponent Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/core/@angular/core/testing.es5.js:652:1)
at UserContext.it Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/src/app/heroes/hero-detail/hero-detail.component.spec.ts:18:29)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.onInvoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/proxy.js:79:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/zone.js:390:1)

Here is the code and the error:

import {async, TestBed} from '@angular/core/testing';
import {APP_BASE_HREF} from '@angular/common';
import {AppModule} from '../../app.module';
import {HeroDetailComponent} from './hero-detail.component';


describe('HeroDetailComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [AppModule
],
providers: [
{provide: APP_BASE_HREF, useValue: '/'}
],
}).compileComponents();
}));


it('should create hero detail component', (() => {
const fixture = TestBed.createComponent(HeroDetailComponent);
const component = fixture.debugElement.componentInstance;
expect(component).toBeTruthy();
}));
});

You can see the entire project, for more details if you need it.

The error is this:

Chrome 58.0.3029 (Mac OS X 10.12.6) HeroDetailComponent should create hero detail component FAILED
Error: Illegal state: Could not load the summary for directive HeroDetailComponent.
at syntaxError Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:1690:22)
at CompileMetadataResolver.getDirectiveSummary Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:15272:1)
at JitCompiler.getComponentFactory Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:26733:26)
at TestingCompilerImpl.getComponentFactory Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler/testing.es5.js:484:1)
at TestBed.createComponent Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/core/@angular/core/testing.es5.js:874:1)
at Function.TestBed.createComponent Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/core/@angular/core/testing.es5.js:652:1)
at UserContext.it Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/src/app/heroes/hero-detail/hero-detail.component.spec.ts:18:29)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.onInvoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/proxy.js:79:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/zone.js:390:1)

You can see the entire project, for more details if you need it.

UPDATE: added declaration like this:

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
AppModule
],
declarations: [HeroDetailComponent],
providers: [
{provide: APP_BASE_HREF, useValue: '/'}
],
}).compileComponents();
}));

Now, new errors appears:

The pipe 'translate' could not be found ("<h1 class="section-title">\{\{[ERROR ->]'heroDetail' | translate}}</h1>
<md-progress-spinner *ngIf="!hero"
class="progre"): ng:///DynamicTestModule/HeroDetailComponent.html@0:28
Can't bind to 'color' since it isn't a known property of 'md-progress-spinner'.

UPDATE: added declaration like this:

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
AppModule
],
declarations: [HeroDetailComponent],
providers: [
{provide: APP_BASE_HREF, useValue: '/'}
],
}).compileComponents();
}));

And more... it's like all directives and components from angular material, and the pipe translate from ngx-translate/core do not appear to be included...

My coworker and I had this issue but the fix was way different than anything else on the internet.

Alternatively, you can run only the the commands to remove the containers with rm --force, but keep in mind that this will send SIGKILL, so running stopfirst and then rm is advisable (stop sends SIGTERM, unless it times out, in which case it sends SIGKILL).

We are using Visual Studio Code and the folder names are case insensitive. Because of that, we asked everyone to use a lowercase naming convention but eventually an uppercase name got into source control. We renamed it, in a roundabout way, and everything was fine.

A month later, my coworker started getting a specific unit test to break with this error message. Only his computer was breaking on that test. We literally commented out all the code that could possible be effecting the test and we still got the error. Finally, I globally searched for the class and we realized that the folder name had reverted back to the uppercase name. We renamed it back to a lowercase name, with no pending changes recognized might I add..., and the test worked.

stop containers:

docker stop container1_id container2_id containerz_id

Let that be a lesson to follow style guides. :)

delete all image:

docker system prune --all

For those who are still having issues with this - I read a separate github issue that discussed changes the Angular team made to the beforeEach callback function.

One command line for cleaning all containers

docker system prune -f ; docker volume prune -f ;docker rm -f -v $(docker ps -q -a)