Docker Commands

Docker Commands

Docker is a platform for developing, shipping and running applications in containers. Containers are lightweight, isolated environments that contain everything needed to run an application, including the code, runtime, system tools, and libraries. Docker provides a command-line interface (CLI) to interact with containers and manage containerized applications. Here are some commonly used Docker commands:

Docker Build Commands:-

CommandDescription
docker build -t <image_name> .Build a Docker image from a Dockerfile and tag it.
docker build --no-cache -t <image_name> .Build a Docker image without using the cache.
docker build -f <dockerfile_name> -t <image_name> .Build an image using a specified Dockerfile.

Docker Clean Up Commands:-

CommandDescription
docker system pruneRemove all unused Docker resources.
docker container pruneRemove all stopped containers.
docker image pruneRemove unused images.
docker volume pruneRemove unused volumes.
docker network pruneRemove unused networks.

Container Interaction Commands:-

CommandDescription
docker run <image_name>Run a Docker image as a container.
docker start <container_id>Start a stopped container.
docker stop <container_id>Stop a running container.
docker restart <container_id>Restart a running container.
docker exec -it <container_id> <command>Execute a command inside a container interactively.

Container Inspection Commands:-

CommandDescription
docker psList running containers.
docker ps -aList all containers (including stopped ones).
docker logs <container_id>Fetch the logs of a specific container.
docker inspect <container_id>Inspect detailed information about a container.

Image Commands:-

CommandDescription
docker imagesList available Docker images.
docker pull <image_name>Pull a Docker image from a registry.
docker push <image_name>Push a Docker image to a registry.
docker rmi <image_id>Remove a Docker image.

Docker Run Commands:-

CommandDescription
docker run -d <image_name>Run a Docker image in detached mode.
docker run -p <host_port>:<container_port> <image_name>Publish container ports to the host.
docker run -v <host_path>:<container_path> <image_name>Mount a host directory or volume to a container.
docker run --name <container_name> <image_name>Assign a custom name to the container.

Docker Registry Commands:-

CommandDescription
docker loginLog in to a Docker registry.
docker logoutLog out from a Docker registry.
docker search <term>Search for Docker images in a registry.
docker pull <registry>/<image_name>Pull a Docker image from a specific registry.

Docker Service Commands (Swarm):-

CommandDescription
docker service create --name <service_name> <image_name>Create a Docker service within a Swarm.
docker service lsList running Docker services in a Swarm.
docker service scale <service_name>=<replicas>Scale the replicas of a Docker service.
docker service logs <service_name>View logs of a Docker service in a Swarm.

Docker Network Commands:-

CommandDescription
docker network create <network_name>Create a Docker network.
docker network lsList available Docker networks.
docker network inspect <network_name>Inspect detailed information about a Docker network.
docker network connect <network_name> <container_name>Connect a container to a Docker network.

Docker Volume Commands:-

CommandDescription
docker volume create <volume_name>Create a Docker volume.
docker volume lsList available Docker volumes.
docker volume inspect <volume_name>Inspect detailed information about a Docker volume.
docker volume rm <volume_name>Remove a Docker volume.