Important Docker Commands Every Beginner Should Know
Docker makes it easier to deploy applications using containers. Below are some key Docker commands that will help you get started.
1. Docker Run
This command creates and starts a container from an image. If the image isn’t available locally, Docker pulls it first.
docker run <image_name>
docker run --name <container_name> <image_name>
2. Docker Pull
Downloads an image from Docker Hub. By default, it pulls the latest version.
docker pull <image_name>
3. Docker PS
Lists running containers. Use flags to show all containers (`-a`), the latest container (`-l`), or just the container IDs (`-q`).
docker ps [options]
4. Docker Stop
Stops a running container by name or ID.
docker stop <container_ID>
5. Docker Start
Restarts a stopped container.
docker start <container_ID>
6. Docker RM
Removes a container. Add -f to force removal.
docker rm <container_name_or_ID>
7. Docker RMI
Deletes an image to free up space.
docker rmi <image_name_or_ID>
8. Docker Exec
Runs a command inside a running container. Flags like -d (background) and -e (environment variables) are useful.
docker exec {options} <container_name>
9. Docker Ports
Recommended by LinkedIn
Maps a port from the host to the container for external access.
docker run -d -p <host_port>:<container_port> <container_name>
10. Docker Login
Authenticates with Docker Hub, allowing you to push and pull images. You'll be prompted for your credentials.
docker login
11. Docker Push
Uploads a customised image to Docker Hub after you've built it.
docker push <image_name_or_ID>
12. Docker Build
Builds a Docker image using a Dockerfile. The -t option assigns a tag to the image.
docker build -t <image_name:tag> .
13. Docker Stop Multiple Containers
Stops multiple containers at once by specifying their names or IDs.
docker stop <container1> <container2> <container3>
14. Docker Restart
Restarts a container, useful for troubleshooting.
docker restart <container_name_or_ID>
15. Docker Inspect
Displays detailed information about a container or image, useful for debugging.
docker inspect <container_name_or_ID>
16. Docker Commit
Creates a new image from an existing container with modifications.
docker commit <container_name_or_ID> <new_image_name:tag>
These commands form the foundation of Docker. Practice them daily to become more familiar with Docker.