0

I have the following script, which creates the docker image and container as expected.

echo "password" | sudo -S docker stop /nexct-approval-service-container | xargs sudo docker rm
echo "docker build ..."
echo "password" | sudo -S docker build . -t nexct-approval-service-image:latest
echo "docker run ..."
echo "password" | sudo -S docker run -t --network=host -p 8081:8081 --name nexct-approval-service-container nexct-approval-service-image

As you can see I am creating a container and image with the following names:

nexct-approval-service-container
nexct-approval-service-image

However, what I cannot get to work is the following line:

echo "password" | sudo -S docker stop /nexct-approval-service-container | xargs sudo docker rm

My intention is to remove the container and image if it exists before I create a new one. What I am finding however, when I do sudo docker images is that my list of images continues to grow.

Question

How can I build my images with the specific name, but stop a new one from being built each time?

More info:

As you can see, a number of images called <none> are left below after each build.

$ docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED              SIZE
nexct-approval-service-image   latest              7359e1ff2138        21 seconds ago       557MB
<none>                         <none>              c4f98256b401        About a minute ago   557MB
<none>                         <none>              908eebf0227a        2 minutes ago        557MB
<none>                         <none>              8b6b0cb01773        3 minutes ago        557MB
openjdk                        14                  cdc43cc23d2d        6 days ago           511MB
Richard
  • 8,193
  • 28
  • 107
  • 228
  • The later sudo doesn't see the password in your pipe chain. Why don't you just add your user to the docker group? – tkausl Jul 23 '20 at 12:35
  • Thanks for the reply. I am not experienced at docker. How do I add the user to the group? – Richard Jul 23 '20 at 12:36
  • Even if I add the password, it is still not clearing my legacy images: `echo "password" | sudo -S docker stop /nexct-approval-service-container | echo "password" | xargs sudo docker r`. I get the following warning: `Error: No such container: password`. So I have obviously done it wrong. – Richard Jul 23 '20 at 12:40
  • 1
    `usermod -aG docker $USER`, start a fresh terminal/ssh session and all docker commands should work without sudo. – tkausl Jul 23 '20 at 12:40
  • Thanks, now I can run the commander without `sudo`. But the it is still not removing the image. – Richard Jul 23 '20 at 12:52
  • 1
    Every time you `docker build` an image, the old one remains behind with the `` tags; you need to `docker rmi` images separately from `docker rm` containers. [Docker remove TAG images](https://stackoverflow.com/questions/33913020/docker-remove-none-tag-images) has some further discussion of cleaning these up (note that the top answers there predate some useful Docker commands that simplify the process). – David Maze Jul 23 '20 at 13:03
  • @DavidMaze, thank you. That's the solution. I now can remove the images. – Richard Jul 23 '20 at 13:08

0 Answers0