Most Used Docker Commands

Download and install docker desktop

Follow the steps provided in the docker docs to install and setup docker desktop docs.docker.com

BUILD This command is used to create an image from a set of insteuctions provided in a file called Dockerfile.

docker build -t imageName .

-t specifies the tag name for the image. ‘.’ specifies the path for of the Dockerfile.

IMAGES This command is used to list all the images.

docker images

RUN This command is used to create and start a new Docker container based on a specified Docker Image. Comand:

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

Run an interactive container: options = -it

Port mapping: options = -p host_port:container_port

Set env variables: options = -e NAME=value

Run container with name: options = –name container-name

Run container in background: options = -d

Limit Resources: options = –memory=512m –cpus=0.5

Run container with name: options = –name container-name

PS This command is used to list all the running containers.

docker ps

To see all the containers present even the ones which are not running.

docker ps -a

STOP This command is used to stop a running container.

docker stop container_name/container_id

ATTACH This command is used to attach you terminal’s std input, output and error to a running container using the container’s Id or name. This allows to connect to the running container interactively.

docker attach container_name

Although pressing ctrl-C is stopping the whole container. Also while running the container use -dit.

docker run -ditp 9010:9010 –name order-container order-service

COMMIT This command is used to create a new image based on the changes made to a container. It allows you to save the current state of a container as a new image.

docker commit container_name OR docker commit container_id

While it can be used to create an image that is similar to another one, it might not be the most recommended way to create replicas or ensure consistency across environments.

INSPECT is used to obtain detailed information about Docker objects such as containers, images, volumes, and networks. It provides a JSON-formatted output that includes a wealth of information about the specified Docker object. The information retrieved can be useful for various purposes, including troubleshooting, scripting, and understanding the configuration of Docker resources.

docker inspect container_name OR docker inspect image_name OR docker inspect network_name OR docker inspect volume_name

PULL This command is used to pull a image from our dockerhub.

docker pull image-name

PUSH This command is used to push a image from our dockerhub.

docker push image-name

LOGS This command is used to get the logs of a runnning container.

docker logs container-name

RM This command is used to remove a stopped container.

docker rm container-name

RMI This command is used to remove an image.

docker rmi image-name

SOME MORE USEFUL COMMANDS:

  • Docker version
  • Docker search
  • Docker restart
  • Docker kill
  • Docker exec
  • Docker login
  • Docker history
  • Docker copy
  • Docker logout
  • Docker volume

Docker Networking

BUILD This command is used to create an image from a set of insteuctions provided in a file called Dockerfile.

Docker networking provides complete isolation for docker containers. It means a user can link a docker container to many networks. It requires very less OS instances to run the workload.

Types of Docker Network:

  1. Bridge: It is the default network driver. We can use this when different containers communicate with the same docker host.
  2. Host: When you don’t need any isolation between the container and host then it is used.
  3. Overlay: For communication with each other, it will enable the swarm services.
  4. None: It disables all networking.
  5. macvlan: This network assigns MAC(Media Access control) address to the containers which look like a physical address.

docker commands docker commands docker commands docker commands docker commands docker commands

Leave a Reply

Your email address will not be published. Required fields are marked *