{"id":1330,"date":"2019-05-23T22:42:50","date_gmt":"2019-05-23T22:42:50","guid":{"rendered":"http:\/\/bitcows.com\/?p=1330"},"modified":"2019-05-23T22:42:50","modified_gmt":"2019-05-23T22:42:50","slug":"5-docker-commands-for-beginners-that-will-make-your-life-easier","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=1330","title":{"rendered":"5 Docker commands for beginners that will make your life easier."},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bitcows.com\/wp-content\/uploads\/2019\/05\/docker5-1140x456.jpg\" alt=\"\" width=\"1140\" height=\"456\" class=\"alignnone size-column1-2\/5 wp-image-1341\" \/><\/p>\n<p>In this article we\u2019ll look at 5 Docker CLI commands you should know. These might not be the most common commands but knowing about these will come in handy for debugging, free up memory and clean up. <\/p>\n<h2>1. Stopping All Containers<\/h2>\n<p>Stopping a container is pretty straight forward <code>docker stop my_container<\/code> but what if you wanted to stop them all? Here you see we we can loop all the containers an print out the ID&#8217;s of each one. <code>docker ps<\/code> lists the containers <code>-a<\/code> causes all the container to be listed regardless if they are running or not, <code>-q<\/code> only display numeric IDs. <\/p>\n<pre class=\"prettyprint\">docker stop $(docker ps -a -q)<\/pre>\n<h2>2. Cleanup and Remove Containers and Images<\/h2>\n<p>As seen above we use the same <code>$(docker ps -a -q)<\/code> to list all the containers. Then we use <code>docker rm<\/code> to remove them and <code>-f<\/code> forces the removal of a running container.<\/p>\n<pre class=\"prettyprint\">docker rm -f $(docker ps -a -q)<\/pre>\n<p>Lastly as seen above we can remove all images running or other wise by applying the same logic as before. Listing each image id and force removing them.<\/p>\n<pre class=\"prettyprint\">docker rmi -f $(docker images -q)<\/pre>\n<h2>3.Bash<\/h2>\n<p>Lets say you have an image and you need to examine a log file in it, or add a file and re-tag it. You can use this command to bash or commandline into it and do your worst. First you will start up your image, and when its running you find out the containers ID by typing <code>docker ps<\/code> then take that container&#8217;s ID and insert it in the below command. You should then see a command prompt that puts you inside the running container.<\/p>\n<pre class=\"prettyprint\">docker exec -it [CONTAINER_ID] bash<\/pre>\n<h2>4.Prune<\/h2>\n<p>If you have limited resources on your server like I sometimes do. You might need to free up some memory. Doing a prune is the way to delete unused images that are just out there dangling and taking up valuable space. If you wan to remove all unused images not just dangling ones use the option <code>-a<\/code>. This also works for containers, this removes all stopped containers but unlike the command above it won&#8217;t remove ALL containers.<\/p>\n<pre class=\"prettyprint\">docker image prune<\/pre>\n<pre class=\"prettyprint\">docker container prune<\/pre>\n<h2>5. Tag<\/h2>\n<p>Tag creates a tag from TARGET_IMAGE that refers to SOURCE_IMAGE. A good reason to do this is if you have an image that you don&#8217;t or can&#8217;t rebuild and you need to add or remove something to it. This way you can use the source image, edit and re-tag it as something new.<\/p>\n<pre class=\"prettyprint\">docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]<\/pre>\n<pre class=\"prettyprint\">docker image tag my_old_image:1.0 my_new_image:1.1<\/pre>\n<h2>Conclusion<\/h2>\n<p>Obviously there are many more commands you can use and learn for Docker. These are just 5 that I have used consistanly over and over. Note: there are other options that can be combined with these commands if you need a <a href=\"https:\/\/docs.docker.com\/reference\/\" rel=\"noopener noreferrer\" target=\"_blank\">list of them all check this out<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we\u2019ll look at 5 Docker CLI commands you should know. These might not be the most common commands but knowing about these will come in handy for debugging, free up memory and clean up. 1. Stopping All Containers Stopping a container is pretty straight forward docker stop my_container but what if you&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=1330\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1333,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[41],"class_list":["post-1330","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","tag-docker"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/1330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1330"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/1330\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}