DEV Community 👩‍💻👨‍💻

Dvir Segal
Dvir Segal

Posted on • Originally published at dvirsegal.Medium on

How to run, delete a Docker image



Solving the “It works on my machine” syndrome

I covered the basics of creating and building Docker images in part I (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.

let’s continue — src

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:

71ed79a8d99f my_app “/bin/bash” 6sec ago Up 5sec eager_elbakyan
Enter fullscreen mode Exit fullscreen mode

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

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 .

delete everything — src

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

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

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: 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

if you liked it…

Top comments (0)

Create an Account! The only reason people scroll to the bottom...  
is because they want to read more.

Create an account to bookmark, comment, and react to articles that interest you.