Monday, July 1, 2024

Docker Commands

What are Docker commands?

Docker commands allow you to build, run, manage, and deploy Docker containers. For such complex projects, Docker provides a command line interface (CLI) that allows us to manage containers and images and perform general Docker tasks. The Docker CLI comes with around 60 commands, which can be grouped into four sections: managing containers, managing images, actions on Docker Hub, and general commands.

Basic Docker commands cheat sheet

We will cover the different basic Docker commands and their basic usage. For a better overview, the commands in this tutorial are divided into the following seven categories:

Docker management commands; Docker Hub commands; Docker images command; Docker container commands; Docker network commands; Docker volume commands; Docker Swarm commands.

1. Docker management commands

The Docker management commands are general commands used to get system information or help from Docker CLI.

We will start with the command that lists all the other commands, docker --help.

docker --help

docker –help -> You can also use –help on all subcommands.

docker -d -> To start the Docker daemon.

docker info -> To display Docker system-wide information.

docker version -> To display the current installed Docker version.

 

2. Docker Hub commands

Docker Hub is a service provided by Docker that hosts container images. You can download and even push your own images in Docker Hub, making it accessible to everyone connected to the service.

The Docker Hub related commands are:

docker login

docker login -u <username> -> To login into Docker.

docker push

docker push <username>/<image_name> -> To publish an image to Docker Hub.

docker search

docker search <image_name> -> To search for an image in Docker Hub.

docker pull

To pull an image from the Docker Hub.

docker pull <image_name> -> docker pull is one of the most common Docker commands:

To run a container, we need to first pull the container and then run it.

3. Docker images command

The following commands allow it to work with the Docker images.

docker build

docker build -> To build an image from a Dockerfile.

docker commit

docker commit <container_name>  -> To create a new image from a container’s changes.

docker history

docker history <container_name> -> To show the history of an image.

docker images

docker images -> To list images.

docker import

docker import  -> To import the contents from a tarball to create a filesystem image.

docker load

docker load -> To load an image from a tar archive or STDIN.

docker rmi

docker rmi <container_name> -> To remove one or more images.

docker save

docker save <container_name> -> To save images to a tar archive or STDOUT.

docker tag

docker tag -> To tag an image into a repository.

4. Docker container commands

docker run --name

docker run --name <container_name> <image_name>  -> To create and run a container from an image.

docker run -p

docker run -p <host_port>:<container_port> <image_name>  -> To run a container with port mapping.

docker run -d

docker run -d <image_name>  -> To run a container in the background.

docker start|stop

docker start|stop <container_name> (or <container-id>)  -> To start or stop an existing container.

docker rm

docker rm <container_name>  -> To remove a stopped container.

docker exec -it

docker exec -it <container_name> sh  -> To open a shell inside a running container.

docker logs

docker logs -f <container_name>  -> To fetch and follow the logs of a container.

docker inspect

docker inspect <container_name> (or <container_id>)  -> To inspect a running container.

docker ps

docker ps  -> To list currently running containers.

docker ps -a

docker ps -a  -> To list all docker containers.

docker container stats

docker container stats  -> To view resource usage stats.

5. Docker network commands

Docker allows containers to communicate between each other. This can be done via Docker networks. Below are the Docker network commands:

docker network create

docker network create -> To create a new Docker network.

docker network connect

docker network connect -> To connect a container to a network.

docker network disconnect

docker network disconnect -> To disconnect a container from a network.

docker network inspect

docker network inspect -> To display information about a Docker network.

docker network ls

docker network ls -> To list all the networks.

docker network rm

docker network rm -> To remove one or more networks.

6. Docker volume commands

Docker volumes are used for permanent data storage. Containers mount those volumes and make them accessible from inside the containers.

Here are the Docker commands related to volume management.

docker volume create

docker volume create -> To create a new Docker volume.

docker volume ls

docker volume ls -> To list all Docker volumes.

docker volume rm

docker volume rm -> To remove one or more volumes.

docker volume inspect

docker volume inspect -> To display volume information.

7. Docker Swarm commands

Docker swarm mode is an advanced Docker feature for managing a cluster of Docker daemon intended for production environments. A swarm consists of a swarm manager and nodes where services are deployed.

Docker Swarm mode supports scaling, container replicas, network overlays, encrypted communications, service discovery, and rolling updates across multiple machines.

docker node ls

docker node ls -> To list nodes.

docker service create

docker service create -> To create a new service.

docker service ls

docker service ls -> To list services.

docker service scale

docker service scale -> To scale services.

docker service rm

docker service rm -> To remove a service from the swarm.

No comments:

Post a Comment