How to run, delete a Docker image
Tags: docker, devops, dockerimage, softwaredevelopment
Published: 2021-08-02 12:27:24

---
title: How to run, delete a Docker image
published: true
date: 2021-08-02 11:43:08 UTC
tags: docker,devops,dockerimage,softwaredevelopment
canonical_url: https://dvirsegal.medium.com/how-to-run-delete-a-docker-image-edae8607a44
---

![](https://cdn-images-1.medium.com/max/800/0*bRvhcFgijNKRVD9L.gif)<figcaption><a href="https://www.google.com/url?sa=i&amp;url=https%3A%2F%2Fdribbble.com%2Fshots%2F3122897-Nyan-Whale&amp;psig=AOvVaw08hekuqutjB1tjQ-JTIISu&amp;ust=1627277148699000&amp;source=images&amp;cd=vfe&amp;ved=0CAsQjRxqFwoTCPCNzPy9_fECFQAAAAAdAAAAABAJ">src</a></figcaption>

#### MY HUMBLE EFFORT TO SIMPLIFY DOCKER PART II

#### Solving the “It works on my machine” syndrome

I covered the basics of creating and building Docker images in [part I](https://dev.to/dejavo/how-to-create-build-a-docker-image-56o3) (if you haven’t read that yet, I would recommend it since this part is based on it). In this part, I’ll explain how to run and delete an image of the container you’ve built.

![](https://cdn-images-1.medium.com/max/640/0*RPNGttEyTXJrg2bj)<figcaption>let’s continue — <a href="https://www.google.com/url?sa=i&amp;url=https%3A%2F%2Fwww.linkedin.com%2Fbusiness%2Ftalent%2Fblog%2Fpassive-candidate-recruiting&amp;psig=AOvVaw03kt3DER3Q4ui3OWiwtqTk&amp;ust=1627306064290000&amp;source=images&amp;cd=vfe&amp;ved=0CAsQjRxqFwoTCODkrNSp_vECFQAAAAAdAAAAABBI">src</a></figcaption>

Running an image is as simple as `docker run -it image\_name:tag` . Let’s break it down; tag is the one you set when building it; note that if you remove it, then docker assumes you point to the latest. The meaning of `-it` isinteractive terminalWhich is loading the terminal driver in an interactive mode in a foreground mode, basically a way to run commands on the docker from our terminal. Thus, in our case `docker run -it my\_app` will suffice.

Once we run the container, docker names it (weirdly) and set an id. To get this information, rundocker ps And below is its output:

```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71ed79a8d99f my_app “/bin/bash” 6sec ago Up 5sec eager_elbakyan
```

to simply stop a container, we can use the command `docker stop container\_id` where in our case, `docker stop 71ed79a8d99f`. It’s also possible to enter exit in the terminal to get the same outcome. Duly note, this command doesn’t delete the container — more on that as we advance.

Furthermore, if you’d like to get all types of containers, just run `docker ps -a` and now it will be shown with an exit status. If you’d like to rerun it, just type `docker start container\_id`, btw docker allows using the name instead of the container\_id and, it is possible to set the name of the image as follows: `docker run -it — name dvir\_app my\_app`

Sometimes we would like to run a container without connecting to it; in this case, we can use the `-d` flag (_detached_): `docker run -d image\_name` .  
If eventually, we would like to connect the docker, we’ll just need to run: `docker exec -it container\_id /bin/bash` and simple as that, we will get a terminal into our running docker. More commands can be found at the official site:

[Docker run reference](https://docs.docker.com/engine/reference/run/)

Among the commands I’ve written above, there are a few more popular:

`docker run --rm -it image\_name` will remove the docker’s file system after we close the container.

`docker run —-env my\_life=”/now/or\_never.txt” -it image\_name` Usually, we use some environment variables on our container, the `—-env` flag helps you set it during a run.

We can use the `--entrypoint` flag to change the entry point set in the docker image. for example, if the docker image is set to run a service, but we would like to get a bash terminal before it runs it, we can simply set it like this:

`docker run -it --entrypoint /bin/bash image\_name` or in a shorter version `docker run -it image\_name /bin/bash` .

![](https://cdn-images-1.medium.com/max/480/1*2M0XPTA6UKHb-rgE1t8sUw.gif)<figcaption>delete everything — <a href="https://giphy.com/clips/rickandmorty-season-4-episode-2-s4e2-DtJ5uJZwLFRYqXshdC">src</a></figcaption>

Lastly, I would like to touch the how to delete an image/container. Those files usually weigh quite a lot, so most of keeping our system clean and sharp. The command `docker rmi image\_name/id` will delete the image; if we would like to remove a container, simply type `docker rm image\_id` . Both won’t clear the space of those files, so a different command needs to be used:   
`docker image prune` , it will clean the dangling image (as I wrote in part I). A deadlier command is `docker system prune` Which removes all stopped containers, unused networks, dangling images, and build cache.

Finally, to destroy everything and start from scratch `docker system prune -a` , more on that can be found on the official documentation:

[docker system prune](https://docs.docker.com/engine/reference/commandline/system_prune/)

There are also official Docker images of all the well-known distributions, such as Ubuntu, Postgres, and more, which are stored in a repository called docker hub:

[Docker Hub Container Image Library | App Containerization](https://hub.docker.com/)

You can even upload your container there. So, how can you download from it?  
Just do `docker pull`. For example, if we want to download ubuntu, we’ll search it in the hub and reach it: [https://hub.docker.com/\_/ubuntu](https://hub.docker.com/_/ubuntu). All the listed images are visible on the tag tab, and we will choose depending on the need. Eventually `docker pull ubuntu:latest` will pull the latest image from the docker hub (since we haven’t specified the exact repo, the default is the hub).

That’s it for this part; I hope you’ve found it relevant and as simplified as it can be. More on networks, volumes, using docker-compose, etc. on part III

[![](https://cdn-images-1.medium.com/max/545/0*HcO6Za8-DohIuOeP.jpeg)](https://www.buymeacoffee.com/dvirsegal)<figcaption>if you liked it…</figcaption>