5 Docker commands for beginners that will make your life easier.

In this article we’ll 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 wanted to stop them all? Here you see we we can loop all the containers an print out the ID’s of each one. docker ps lists the containers -a causes all the container to be listed regardless if they are running or not, -q only display numeric IDs.

docker stop $(docker ps -a -q)

2. Cleanup and Remove Containers and Images

As seen above we use the same $(docker ps -a -q) to list all the containers. Then we use docker rm to remove them and -f forces the removal of a running container.

docker rm -f $(docker ps -a -q)

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.

docker rmi -f $(docker images -q)

3.Bash

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 docker ps then take that container’s ID and insert it in the below command. You should then see a command prompt that puts you inside the running container.

docker exec -it [CONTAINER_ID] bash

4.Prune

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 -a. This also works for containers, this removes all stopped containers but unlike the command above it won’t remove ALL containers.

docker image prune
docker container prune

5. Tag

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’t or can’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.

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker image tag my_old_image:1.0 my_new_image:1.1

Conclusion

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 list of them all check this out

Loading Facebook Comments ...