Introduction: Docker, a powerful containerization platform, simplifies the process of managing applications in containers. Learning essential Docker commands is fundamental for efficient container handling. Here are ten crucial Docker commands every developer should know:
- docker run
- Usage:
docker run -d -p 8080:80 nginx
- Description: Execute a container from an image.
- Example: Launch an nginx container in detached mode (-d) and map host port 8080 to container port 80.
- Usage:
- docker ps
- Usage:
docker ps
- Description: List running containers.
- Example: View running container details like container ID, image, status, etc.
- Usage:
- docker images
- Usage:
docker images
- Description: Display available images on the host.
- Example: List available images with details such as repository, tag, size, etc.
- Usage:
- docker pull
- Usage:
docker pull ubuntu:latest
- Description: Fetch an image from a registry.
- Example: Pull the latest Ubuntu image from the Docker Hub registry.
- Usage:
- docker stop
- Usage:
docker stop container_name
- Description: Stop a running container.
- Example: Halt the container specified by its name.
- Usage:
- docker rm
- Usage:
docker rm container_name
- Description: Remove a stopped container.
- Example: Delete the container with the specified name.
- Usage:
- docker rmi
- Usage:
docker rmi image_name
- Description: Delete an image.
- Example: Remove the image with the specified name.
- Usage:
- docker exec
- Usage:
docker exec -it container_name bash
- Description: Run a command in a running container.
- Example: Open an interactive shell within the specified container.
- Usage:
- docker build
- Usage:
docker build -t image_name .
- Description: Construct an image from a Dockerfile.
- Example: Build an image using the Dockerfile in the current directory with the specified name.
- Usage:
- docker-compose up
- Usage:
docker-compose up -d
- Description: Initiate services defined in a docker-compose.yml file.
- Example: Start services in detached mode (-d) as specified in the docker-compose.yml file.
- Usage:
Conclusion: Mastering these fundamental Docker commands is crucial for effectively managing containers and images. With Docker’s comprehensive command set, developers can streamline their containerization workflow, improving efficiency and deployment processes.