10.1 Useful Docker Commands
Docker provides a variety of commands that allow you to manage containers, images, networks, and other aspects of your Docker environment. Here are some commonly used Docker commands along with their explanations:
-
docker run
: This command creates and starts a new container based on a specified image. For example,docker run nginx
would run a container using the Nginx image. -
docker ps
: Use this command to list all running containers. It provides information such as the container ID, image used, status, and other details. -
docker images
: This command lists all available Docker images on your system. It displays the image ID, repository, tag, and size. -
docker pull
: Use this command to download a Docker image from a remote registry. For example,docker pull ubuntu
would download the latest Ubuntu image. -
docker stop
: This command stops a running container. You need to specify the container ID or name as an argument. -
docker start
: Use this command to start a stopped container. Again, you need to provide the container ID or name. -
docker restart
: This command restarts a running container. Similar to thedocker stop
anddocker start
commands, you need to specify the container ID or name. -
docker rm
: Use this command to remove one or more containers. You can specify container IDs or names as arguments, and Docker will remove them. -
docker rmi
: This command removes one or more Docker images. Similar todocker rm
, you need to provide image IDs or names. -
docker exec
: Use this command to run a command inside a running container. For example,docker exec -it container_name bash
would open a shell inside the specified container. -
docker logs
: This command displays the logs generated by a container. You can specify the container ID or name to view its logs. -
docker build
: Use this command to build a Docker image based on a Dockerfile. It reads the instructions in the Dockerfile and creates an image.
These are just a few examples of useful Docker commands. Docker provides many more commands and options to manage containers, images, networks, volumes, and other resources. Feel free to explore the Docker documentation for more details and to learn about additional commands.