Skip to content

Latest commit

 

History

History
382 lines (252 loc) · 5.97 KB

5.commands.md

File metadata and controls

382 lines (252 loc) · 5.97 KB

Comprehensive Docker Commands Reference

Docker Images

List Images

docker images

Description: Lists all Docker images on your local system.

Pull an Image

docker pull <image_name>:<tag>

Description: Downloads an image from a Docker registry. For example:

docker pull ubuntu:latest

Build an Image

docker build -t <image_name>:<tag> <path_to_dockerfile>

Description: Builds an image from a Dockerfile. For example:

docker build -t myapp:1.0 .

Tag an Image

docker tag <source_image>:<source_tag> <destination_image>:<destination_tag>

Description: Tags an image with a new name and tag. For example:

docker tag myapp:1.0 myrepo/myapp:latest

Remove an Image

docker rmi <image_name>:<tag>

Description: Removes an image from the local system. For example:

docker rmi myapp:1.0

Docker Containers

List Containers

docker ps

Description: Lists all running containers. Use docker ps -a to list all containers, including stopped ones. For example:

docker ps

Run a Container

docker run [OPTIONS] <image_name>:<tag>

Description: Creates and starts a new container. For example:

docker run -d -p 80:80 nginx

Stop a Container

docker stop <container_id>

Description: Stops a running container. For example:

docker stop 123456

Start a Container

docker start <container_id>

Description: Starts a stopped container. For example:

docker start 123456

Restart a Container

docker restart <container_id>

Description: Restarts a running or stopped container. For example:

docker restart 123456

Remove a Container

docker rm <container_id>

Description: Removes a container from the local system. For example:

docker rm 123456

Execute a Command in a Running Container

docker exec -it <container_id> <command>

Description: Runs a command in a running container. For example:

docker exec -it 123456 bash

Copy Files to/from a Container

docker cp <container_id>:<source_path> <destination_path>
docker cp <source_path> <container_id>:<destination_path>

Description: Copies files between a container and the local filesystem. For example:

docker cp 123456:/app/config.json ./config.json
docker cp ./app/config.json 123456:/app/config.json

Docker Networks

List Networks

docker network ls

Description: Lists all Docker networks. For example:

docker network ls

Create a Network

docker network create <network_name>

Description: Creates a new Docker network. For example:

docker network create mynetwork

Connect a Container to a Network

docker network connect <network_name> <container_id>

Description: Connects a container to a network. For example:

docker network connect mynetwork 123456

Disconnect a Container from a Network

docker network disconnect <network_name> <container_id>

Description: Disconnects a container from a network. For example:

docker network disconnect mynetwork 123456

Docker Volumes

List Volumes

docker volume ls

Description: Lists all Docker volumes. For example:

docker volume ls

Create a Volume

docker volume create <volume_name>

Description: Creates a new Docker volume. For example:

docker volume create myvolume

Inspect a Volume

docker volume inspect <volume_name>

Description: Displays detailed information about a volume. For example:

docker volume inspect myvolume

Remove a Volume

docker volume rm <volume_name>

Description: Removes a volume from the local system. For example:

docker volume rm myvolume

Docker System

Prune System

docker system prune

Description: Removes all stopped containers, unused networks, dangling images, and build cache. Use with caution!For example:

docker system prune

Prune Images

docker image prune

Description: Removes unused images. For example:

docker image prune

Prune Containers

docker container prune

Description: Removes stopped containers. For example:

docker container prune

Prune Volumes

docker volume prune

Description: Removes unused volumes. For example:

docker volume prune

Docker Logs

View Logs of a Container

docker logs <container_id>

Description: Displays logs from a container. For example:

docker logs 123456

Docker Compose

Up a Stack

docker-compose up -d

Description: Creates and starts containers as defined in the docker-compose.yml file. For example:

docker-compose up -d

Down a Stack

docker-compose down

Description: Stops and removes all containers defined in the docker-compose.yml file. For example:

docker-compose down

Logs for a Stack

docker-compose logs

Description: Shows logs from all services in the stack. For example:

docker-compose logs

Helpful Aliases

To streamline your workflow, consider adding these aliases to your shell configuration file (.bashrc, .zshrc, etc.):

alias dps='docker ps'
alias dpsa='docker ps -a'
alias drm='docker rm -f'
alias di='docker images'
alias dimg='docker image prune -a'
alias dcont='docker container prune'
alias dvol='docker volume prune'

Conclusion

Docker commands are essential tools for managing containers, images, networks, and volumes. Understanding and using these commands effectively will help you streamline your development and deployment workflows. For more detailed information, refer to the Docker Documentation.