Docker is an open-source platform designed to streamline the development, deployment, and running of applications by using containerization. Containers are lightweight, portable, and isolated environments that bundle an application along with its dependencies, ensuring consistency across development, testing, and production environments.
1. Basic Docker Commands
docker version
: Displays the Docker version installed.docker info
: Shows detailed information about the Docker installation, including containers, images, and resources.
2. Working with Containers
docker run [image]
: Runs a container from an image. Example:docker run nginx
.docker ps
: Lists all running containers.docker ps -a
: Lists all containers, including stopped ones.docker stop [container_id]
: Stops a running container.docker start [container_id]
: Starts a stopped container.docker rm [container_id]
: Removes a container.docker exec -it [container_id] [command]
: Executes a command in a running container (e.g., to access a shell,docker exec -it [container_id] bash
).
3. Working with Images
docker pull [image]
: Downloads an image from Docker Hub.docker images
: Lists all downloaded images.docker rmi [image_id]
: Deletes an image from the local system.docker build -t [image_name] .
: Builds an image from a Dockerfile in the current directory.
4. Inspecting Containers and Images
docker inspect [container_or_image]
: Displays detailed information.docker logs [container_id]
: Shows logs from a container.docker stats
: Displays resource usage statistics for running containers.
5. Managing Volumes and Networks
docker volume create [volume_name]
: Creates a new volume.docker volume ls
: Lists all Docker volumes.docker network create [network_name]
: Creates a user-defined network.docker network ls
: Lists all Docker networks.
6. Docker Compose
docker-compose up
: Starts services defined in adocker-compose.yml
file.docker-compose down
: Stops and removes all services defined in thedocker-compose.yml
file.
These commands are foundational for working with Docker and can be combined or extended depending on your workflow.