CLI Cheat Sheet
Create
- Build an Image from a Dockerfile
docker build -t <image_name> .
- Build an Image from a Dockerfile without the cache
docker build -t <image_name> . –no-cache
- Pull an image from a Docker Hub
docker pull <image_name>
Start Stop
- Start the docker daemon
docker -d
- Create and run a container from an image, with a custom name:
docker run --name <container_name> <image_name>
- Run a container with and publish a container’s port(s) to the host.
docker run -p <host_port>:<container_port> <image_name>
- Run a container in the background
docker run -d <image_name>
- Run and open Shell
docker run -it <image_name>
- Open a shell inside a running container
docker exec -it <container_name> sh
- Run a docker file
docker run <image_name>
- Start or stop an existing container
docker start|stop <container_name> (or <container-id>)
Remove
- Remove a stopped container
docker rm <container_name>
- Delete an Image
docker rmi <image_name>
- Remove all unused images
docker image prune
- Delete an Image by force
docker rmi -f <image_name>
List
- List local images
docker images
- List all docker containers (running and stopped)
docker ps --all
- Fetch and follow the logs of a container:
docker logs -f <container_name>
- To inspect a running container
docker inspect <container_name> (or <container_id>)
- Inspect running container to show IP PORTS
docker inspect -f 'IP: , Ports: ' <container_id>
- Show open ports in docker
docker port <container_id>
- To list currently running containers
docker ps
File Transfer
- Host to docker
docker cp /path/to/local/file <container_id>:/path/inside/container
- Docker to host
docker cp <container_id>:/path/inside/container/file /path/to/local/folder
Docker Hub
- Login into Docker
docker login -u <username>
- Publish an image to Docker Hub
docker push <username>/<image_name>
- Search Hub for an image
docker search <image_name>
Docker persistence
- Command to make docker persistent
docker update --restart=always <CONTAINER_ID>
- list all killed containers
docker ps -all to copy container id
- restart that container
docker start <CONTAINER_ID>
- attatch that container
docker attach <CONTAINER_ID>
- get shell
docker exec -it <CONTAINER_ID> /bin/zsh
- For persitent docker volume, follow https://docs.docker.com/get-started/docker-concepts/running-containers/persisting-container-data/
Others
- View resource usage stats
docker container stats
- Get help with Docker. Can also use –help on all subcommands
docker --help
- Display system-wide information
docker info