In a Docker context, Docker commands offer strong capabilities for controlling images and containers. Commands like "docker run" and "docker create" make it simple for users to build, run, and manage containers. "Docker images" and "docker ps" assist in listing and viewing available images and containers. Commands like "docker network" and "docker volume" make managing networks and volumes easier. Complex configurations involving numerous containers can be orchestrated with the help of Docker Compose and Docker Swarm commands. Control over system-wide data and image creation is also possible with the use of system and Dockerfile commands. To deal with Docker containers and images properly, you must comprehend these commands.
Here are Docker key commands :
Image Commands:
docker images
: List all available images.docker pull <image>
: Download an image from a registry.docker push <image>
: Upload an image to a registry.docker build -t <image-name> <path>: Build an image from a Dockerfile.
Container Commands:
docker ps:
List running containers.docker ps -a
: List all containers.docker create <image>
: Create a new container.docker start <container>
: Start a stopped container.docker stop <container>
: Stop a running container.docker restart <container>
: Restart a container.docker rm <container>
: Remove a container.docker rmi <image>
: Remove an image.docker exec -it <container> <command>
: Run a command in a running container.docker logs <container>
: View container logs.docker stats <container>
: Display container resource usage.docker top <container>
: Display running processes in a container.docker rename <container> <new-name>: Rename a container.
docker wait <container>
: Block until a container stops.
Container Management Commands:
docker-compose up
: Create and start containers defined in a Compose file.docker-compose down
: Stop and remove containers defined in a Compose file.docker-compose logs
: View logs from containers started with Compose.
Networking Commands:
docker network ls
: List networks.docker network create <network>
: Create a new network.docker network connect <network> <container>
: Connect a container to a network.docker network disconnect <network> <container>
: Disconnect a container from a network.
Volume Commands:
docker volume ls
: List volumes.docker volume create <volume>
: Create a new volume.docker volume rm <volume>
: Remove a volume.docker run -v <host-path>:<container-path> <image>
: Attach a volume to a container.
Registry Commands:
docker login
: Log in to a Docker registry.docker logout
: Log out of a Docker registry.
System Commands:
docker info
: Display system-wide information.docker version
: Display Docker version information.docker system df
: Show disk usage.docker system prune
: Remove all unused objects.docker system events
: Get real-time events from the server.
Docker Swarm Commands:
docker swarm init
: Initialize a swarm.docker node ls
: List nodes in a swarm.docker service create
: Create a new service in a swarm.docker service ls
: List services in a swarm.docker stack deploy
: Deploy a stack (Compose file) to a swarm.
Dockerfile Commands:
FROM <image>
: Specify the base image.RUN <command>
: Execute a command during image build.CMD [“executable”,”param1",”param2"]
: Define the default container command.WORKDIR /path
: Set the working directory for subsequent commands.COPY <src> <dest>
: Copy files or directories into the image.ADD <src> <dest>
: Copy files, including remote URLs.EXPOSE <port>
: Expose a port for networking.ENV <key> <value>
: Set environment variables.ARG <var-name>
: Define a build-time variable.VOLUME /path
: Create a mount point for a volume.USER <username>
: Set the user for subsequent commands.HEALTHCHECK CMD [“executable”]
: Set a health check instruction.ONBUILD <instruction>
: Add an instruction to be executed later.LABEL <key>=<value>
: Add metadata to an image.SHELL [“executable”, “param1”, “param2”]
: Override the shell used for executing commands.
Docker Compose Commands:
docker-compose up
: Create and start services defined in a Compose file.docker-compose down
: Stop and remove services defined in a Compose file.docker-compose logs
: View logs from services started with Compose.
Docker Hub Commands:
docker search <image>:
Search for images on Docker Hub.