Docker is a widely used containerization platform, and there are several essential commands you can use to interact with Docker.
Docker commands:
Managing Containers:
docker run <image>
: Create and start a new container based on an image.docker ps
: List running containers.docker ps -a
: List all containers (including stopped ones).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 stopped container.docker rm -f <container>
: Forcefully remove a running container.Managing Images:
docker pull <image>
: Download an image from a registry.docker images
: List locally available images.docker rmi <image>
: Remove an image.docker build -t <image-name> <path-to-dockerfile>
: Build a Docker image from a Dockerfile.docker tag <source-image> <target-image>
: Tag an image to give it a new name and optional tag.docker push <image>
: Push an image to a container registry.Viewing Logs and Inspecting Containers:
docker logs <container>
: View the logs of a container.docker inspect <container>
: Retrieve detailed information about a container.Managing Networks:
docker network ls
: List Docker networks.docker network create <network-name>
: Create a new Docker network.docker network connect <network> <container>
: Connect a container to a network.docker network disconnect <network> <container>
: Disconnect a container from a network.Managing Volumes:
docker volume ls
: List Docker volumes.docker volume create <volume-name>
: Create a Docker volume.docker volume rm <volume-name>
: Remove a Docker volume.Managing Docker Compose:
docker-compose up
: Start services defined in a docker-compose.yml
file.docker-compose down
: Stop and remove containers defined in a docker-compose.yml
file.docker-compose logs
: View logs of services in a docker-compose.yml
file.docker-compose ps
: List services and their status in a docker-compose.yml
file.Executing Commands Inside Containers:
docker exec -it <container> <command>
: Run a command inside a running container interactively.Container Statistics:
docker stats
: Monitor container resource usage (CPU, memory, etc.) in real-time.System Information:
docker info
: Display system-wide information about Docker.docker version
: Show the Docker version information.Cleaning Up:
docker system prune
: Remove all unused containers, networks, volumes, and images not referenced by any container.These are some of the fundamental Docker commands to get you started. Docker offers many more commands and options for advanced use cases, so refer to the official Docker documentation for more information and examples.
Here are some useful Docker commands:
These are just a few of the many useful Docker commands. For more information, please see the Docker documentation:
https://docs.docker.com/engine/reference/commandline/cli/
Here are some examples of how to use Docker commands:
docker pull ubuntu
docker run ubuntu
ls
command in the Ubuntu container:
docker exec $(docker ps -l -q) ls
docker stop $(docker ps -l -q)
docker rm $(docker ps -l -q)
docker build -t my-image .
my-image
image to Docker Hub:
docker push my-image
Docker is a powerful tool for containerizing applications. By learning the basic Docker commands, you can start to use Docker to streamline your development and deployment workflows.