src

MY HUMBLE EFFORT TO SIMPLIFY DOCKER PART II

How to run, delete a Docker image

Solving the “It works on my machine” syndrome

FAUN — Developer Community 🐾
5 min readAug 2, 2021

--

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:

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 rundocker ps -a and now it will be shown with an exit status. If you’d like to rerun it, just typedocker 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:

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

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:

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. 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…

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Dvir Segal

Senior software engineer. Love to ask questions and write about their answers.

No responses yet

Write a response