Docker is a popular tool used for containerization, which allows applications to be run in a consistent and isolated environment. To work effectively with Docker, it is important to be familiar with certain key commands. In this blog, we will discuss some of the most important commands for Docker.
Table of Contents
This command is used to start a container from a Docker image. For example, the following command can be used to start a container from the latest version of the Ubuntu image:
docker run -it ubuntu
The -it flag is used to start the container in interactive mode and attach a terminal to it.
This command is used to list all the running containers on the system. For example, the following command will list all running containers:
This command is used to stop a running container. For example, the following command will stop a container with the ID of 123456:
docker stop 123456
This command is used to remove a container. For example, the following command will remove a container with the ID of 123456:
docker rm 123456
This command is used to list all the Docker images on the system. For example, the following command will list all the images:
This command is used to remove a Docker image. For example, the following command will remove an image with the tag of ubuntu:
docker rmi ubuntu
This command is used to build a Docker image from a Dockerfile. For example, the following command will build an image from a Dockerfile in the current directory:
docker build -t my-image .
The -t flag is used to specify the name and tag of the image.
This command is used to run a command inside a running container. For example, the following command will run the ls command inside a container with the ID of 123456:
docker exec 123456 ls
Remove Unused Containers:
This command Removes Unused Containers
docker container prune
Remove Unused Images:
Remove dangling images (i.e., images that are not used by any container and don’t have a repository/tag association):
docker image prune
To remove all unused images (not just dangling ones):
docker image prune -a
If you want to clean up everything at once (unused containers, networks, volumes, and images), you can use:
docker system prune -a
Inspect Disk Usage:
To see where the most space is being used, you can use:
docker system df
Some bonus tips for working with Docker
Use docker-compose: Docker-compose is a tool that allows you to define and run multi-container Docker applications. By defining your application’s services and their dependencies in a YAML file, you can easily spin up your entire application with a single command.
Tag your images: When building Docker images, it’s a good practice to tag them with a version number or a unique identifier. This can help you keep track of changes and roll back to previous versions if needed.
Clean up unused resources: Docker can quickly take up a lot of disk space if you’re not careful. Be sure to regularly clean up any unused images, containers, and volumes using the docker system prune command.
Use Docker Hub: Docker Hub is a cloud-based repository where you can store and share your Docker images. By using Docker Hub, you can easily share your images with others and pull down pre-built images from the community.
Understand networking: By default, Docker containers run in their own isolated network namespace. To allow containers to communicate with each other, you’ll need to set up networking. Understanding Docker’s networking model is crucial for building multi-container applications.
Mount volumes: When running containers, it’s often useful to mount a volume from the host system into the container. This allows you to persist data between container runs and share data between containers. Use the -v flag when running a container to mount a volume.
Use environment variables: When defining your Dockerfile or docker-compose.yaml, consider using environment variables to make your application more flexible. This allows you to easily configure your application for different environments without having to modify the underlying code.
These are just a few of the most important commands for working with Docker. There are many other commands available, each with its own unique functionality. By familiarizing yourself with these key commands, you will be well on your way to becoming a Docker expert.