Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script: Remove hello world docker container without knowing ID

I'm running hello world docker container in a shell script:

#!/bin/bash
sudo docker run hello-world

I'm doing this to verify if the installation of docker was correct. After this I would like to remove this container again. But as I don't know the ID of this new container I can't remove it:

sudo docker rm hello-world

... is failing.

like image 800
user3142695 Avatar asked Jan 18 '17 22:01

user3142695


People also ask

How do I get rid of Hello World docker?

If you haven't already, open a terminal and ssh into the machine where you run your manager node. For example, the tutorial uses a machine named manager1 . Run docker service rm helloworld to remove the helloworld service. Even though the service no longer exists, the task containers take a few seconds to clean up.

How do I remove a docker image ID?

To remove the image, you first need to list all the images to get the Image IDs, Image name and other details. By running simple command docker images -a or docker images . After that you make sure which image want to remove, to do that executing this simple command docker rmi <your-image-id> .


2 Answers

In the docs of docker: Explore the Application it's described how you can do this.

When you docker run hello-world, the container gets created with a random name that you can check with the following command:

docker container ls --all

Under the NAMES column of the output of the command you can check the generated name (you can see below on my example image. peaceful_morse in my case).

Then you can use this name as a parameter when you call the docker remove command:

docker rm peaceful_morse

Image with all the steps:

enter image description here

like image 192
Miguel Jesus Avatar answered Oct 18 '22 23:10

Miguel Jesus


Give the container a name:

sudo docker run --name hello-world-container hello-world

Then you can remove it by name:

sudo docker rm hello-world-container
like image 23
BMitch Avatar answered Oct 18 '22 23:10

BMitch