30 Docker Commands to Manage Containers
It’s been five years since I started using Docker in production. Over time, I’ve become familiar with its API for packaging and distributing my applications. Daily use—whether writing a Dockerfile, running containers, or debugging issues—makes you internalize its API, which is actually quite intuitive.
I’ll group the commands according to their use:
Docker Installation and Setup
Section titled “Docker Installation and Setup”-
docker —version
Terminal window docker --versionShows the installed version of Docker on your system.
-
docker info
Terminal window docker infoProvides detailed information about your Docker installation, including containers, images, and system configuration.
-
docker login
Terminal window docker loginAllows you to authenticate with a Docker registry (such as Docker Hub) so you can push or pull private images.
Docker Images
Section titled “Docker Images”-
docker pull
Terminal window docker pull <image_name>Downloads an image from a Docker registry to your local machine.
-
docker images
Terminal window docker images*************************************************************************************REPOSITORY TAG IMAGE ID CREATED SIZEruizdev7/portfolio-frontend 1.0.0 bc5e7370f8a7 11 days ago 54.2MBruizdev7/portfolio-backend 1.0.0 777aed368f07 11 days ago 246MBmoby/buildkit buildx-stable-1 72a94020693f 2 months ago 216MBmysql 8.0 c6f7ec307380 3 months ago 772MBexcalidraw/excalidraw latest 4c54ecfa158f 3 months ago 92.9MBfireflyiii/core latest f5e8a551d54b 4 months ago 733MBjc21/nginx-proxy-manager latest 9f5e0949eb63 4 months ago 1.09GBalpine latest aded1e1a5b37 5 months ago 7.83MBnginx latest 53a18edff809 5 months ago 192MBLists all Docker images stored locally.
-
docker rmi
Terminal window docker rmi <image_id>Removes a Docker image from your local machine.
-
docker build
Terminal window docker build -t <image_name> .Builds a Docker image from a Dockerfile.
Docker Containers
Section titled “Docker Containers”-
docker run
Terminal window docker run -d --name <container_name> <image_name>Creates and runs a new container from an image. Useful flags include:
-d --detach: runs the container in the background and prints the container ID--name <container_name> <image_name>: creates a container from the selected image and assigns it a name.
-
docker ps
Terminal window docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa0b311e915b8 jc21/nginx-proxy-manager:latest "/init" 5 days ago Up 4 days 0.0.0.0:80-81->80-81/tcp, 0.0.0.0:443->443/tcp proxy-managerbc6eaa1e2d87 excalidraw/excalidraw:latest "/docker-entrypoint.…" 5 days ago Up 4 days (healthy) 0.0.0.0:5001->80/tcp excalidraw668a3af577fa ruizdev7-portfolio-backend "flask run --host=0.…" 5 days ago Up 4 days 0.0.0.0:8000->6000/tcp backende789e5c966f3 ruizdev7-portfolio-frontend "docker-entrypoint.s…" 5 days ago Up 4 days 0.0.0.0:5173->3000/tcp frontendShows the containers currently running.
-
docker ps -a
Terminal window docker ps -aLists all containers, including those that are stopped.
-
docker stop
Terminal window docker stop <container_id>Stops a running container.
-
docker start
Terminal window docker start <container_id>Starts a stopped container.
-
docker restart
Terminal window docker restart <container_id>Restarts a container (stops and starts it again).
-
docker rm
Terminal window docker rm <container_id>Removes a container (it must be stopped first).
-
docker exec
Terminal window docker exec -it <container_id> /bin/bashRuns a command inside a running container (for example, opens a bash terminal).
Docker Volumes
Section titled “Docker Volumes”-
docker volume create
Terminal window docker volume create <volume_name>Creates a new Docker volume for persistent data storage.
-
docker volume ls
Terminal window docker volume lsLists all existing volumes.
-
docker volume inspect
Terminal window docker volume inspect <volume_name>Shows detailed information about a specific volume.
-
docker volume rm
Terminal window docker volume rm <volume_name>Removes a volume.
Docker Networks
Section titled “Docker Networks”-
docker network create
Terminal window docker network create <network_name>Creates a new Docker network.
-
docker network ls
Terminal window docker network lsLists all Docker networks.
-
docker network inspect
Terminal window docker network inspect <network_name>Shows detailed information about a specific network.
-
docker network connect
Terminal window docker network connect <network_name> <container_name>Connects a container to an existing network.
-
docker network disconnect
Terminal window docker network disconnect <network_name> <container_name>Disconnects a container from a network.
Docker Compose
Section titled “Docker Compose”-
docker-compose up
Terminal window docker-compose upBuilds, creates, starts, and attaches the services defined in a
docker-compose.ymlfile. -
docker-compose down
Terminal window docker-compose downStops and removes the containers, networks, images, and volumes defined by
docker-compose.yml. -
docker-compose ps
Terminal window docker-compose psLists the containers managed by Docker Compose.
-
docker-compose build
Terminal window docker-compose buildBuilds or rebuilds the services defined in
docker-compose.yml. -
docker-compose logs
Terminal window docker-compose logsShows the logs of the containers managed by Docker Compose.
-
docker-compose exec
Terminal window docker-compose exec <service_name> <command>Runs a command in a container managed by Docker Compose.
These commands cover the basics of Docker and should help you get started with managing Docker containers, images, volumes, networks, and using Docker Compose for multi-container applications.