Docker Common Command You need to know
Tags: docker, programming
Published: 2022-07-23 17:58:42

## **Docker Common Command You need to know**

## **Docker CLI Cheatsheet**

> ### `docker build`
>
> ```yml
> docker build [options] .
>   -t "app/container_name"    # name
>   --build-arg APP_HOME=$APP_HOME    # Set build-time variables
> ```
>
> Create an `image` from a Dockerfile.
>
> ### `docker run`
>
> ```yml
> docker run [options] IMAGE
>   # see `docker create` for options
> ```
>
> #### Example
>
> ```
> $ docker run -it debian:buster /bin/bash
> ```
>
> Run a command in an `image`.
>
> ## **Manage containers**
>
> ###  1 . `docker create`
>
> ```yml
> docker create [options] IMAGE
>   -a, --attach               # attach stdout/err
>   -i, --interactive          # attach stdin (interactive)
>   -t, --tty                  # pseudo-tty
>       --name NAME            # name your image
>   -p, --publish 5000:5000    # port map (host:container)
>       --expose 5432          # expose a port to linked containers
>   -P, --publish-all          # publish all ports
>       --link container:alias # linking
>   -v, --volume `pwd`:/app    # mount (absolute paths needed)
>   -e, --env NAME=hello       # env vars
> ```
>
> #### Example
>
> ```
> $ docker create --name app_redis_1 \
>   --expose 6379 \
>   redis:3.0.2
> ```
>
> Create a `container` from an `image`.
>
> ### 2. `docker exec`
>
> ```yml
> docker exec [options] CONTAINER COMMAND
>   -d, --detach        # run in background
>   -i, --interactive   # stdin
>   -t, --tty           # interactive
> ```
>
> #### Example
>
> ```
> $ docker exec app_web_1 tail logs/development.log
> $ docker exec -t -i app_web_1 rails c
> ```
>
> Run commands in a `container`.
>
> ### 3. `docker start`
>
> ```yml
> docker start [options] CONTAINER
>   -a, --attach        # attach stdout/err
>   -i, --interactive   # attach stdin
> 
> docker stop [options] CONTAINER
> ```
>
> Start/stop a `container`.
>
> ### 4 . `docker ps`
>
> ```
> $ docker ps
> $ docker ps -a
> $ docker kill $ID
> ```
>
> Manage `container`s using ps/kill.
>
> ### 5. `docker logs`
>
> ```
> $ docker logs $ID
> $ docker logs $ID 2>&1 | less
> $ docker logs -f $ID # Follow log output
> ```
>
> See what’s being logged in an `container`.
>
> ## **Manage Images**
>
> ### 1. `docker images`
>
> ```sh
> $ docker images
>   REPOSITORY   TAG        ID
>   ubuntu       12.10      b750fe78269d
>   me/myapp     latest     7b2431a8d968
> $ docker images -a   # also show intermediate
> ```
>
> Manages `image`s.
>
> ### 2. `docker rmi`
>
> ```yml
> docker rmi b750fe78269d
> ```
>
> Deletes `image`s.
>
> ## **Manage Clean up**
>
> ### 1. Clean all
>
> ```sh
> docker system prune
> ```
>
> Cleans up dangling images, containers, volumes, and networks (ie, not associated with a container)
>
> ```sh
> docker system prune -a
> ```
>
> Additionally remove any stopped containers and all unused images (not just dangling images)
>
> ### 2. Containers
>
> ```sh
> # Stop all running containers
> docker stop $(docker ps -a -q)
> 
> # Delete stopped containers
> docker container prune
> ```
>
> 
>
> ​	**3.Images**
>
> ```sh
> docker image prune [-a]
> ```
>
> Delete all the images
>
> ### 4. Volumes
>
> ```sh
> docker volume prune
> ```
>
> Delete all the volumes
>
> 

​			

​		

## **docker-compose Cheatsheet**

> ## Common commands
>
> ```
> # Starts existing containers for a service.
> docker-compose start
> 
> # Stops running containers without removing them.
> docker-compose stop
> 
> # Pauses running containers of a service.
> docker-compose pause
> 
> # Unpauses paused containers of a service.
> docker-compose unpause
> 
> # Lists containers.
> docker-compose ps
> 
> # Builds, (re)creates, starts, and attaches to containers for a service.
> docker-compose up
> 
> # Stops containers and removes containers, networks, volumes, and images created by up.
> docker-compose down
> ```



## **Tipps & Tricks**



```
#!/bin/sh

docker-compose down &&
docker-compose rm &&
docker-compose build &&
docker-compose up -d
sleep 1
docker rmi $(docker images -f "dangling=true" -q)
echo y | docker volume prune
printf "\n... HAPPY CODING ...\n\e[0m"
```

### **Removing unused containers**

> Docker containers have a `status` field indicating where they are at in their lifecycle. According to the [docs](https://docs.docker.com/engine/reference/commandline/ps/#filtering), `status` can be one of `created`, `restarting`, `running`, `removing`, `paused`, `exited`, or `dead`.
>
> First, we need to get the IDs of the containers with status `exited` or `dead` as follows:
>
> ```shell
> docker ps --filter status=exited --filter status=dead -q
> ```
>
> Then, we can reuse the above command to delete these containers with the following command:
>
> ```shell
> docker rm $(docker ps --filter=status=exited --filter=status=dead -q)
> ```
>
> A one-liner alternative to remove all stopped containers is:
>
> ```shell
> docker container prune
> ```





### Removing all containers

> First, we need to stop all running containers. We can get the IDs of the running containers as follows:
>
> ```shell
> docker ps -q
> ```
>
> Then, we can stop all the containers with:
>
> ```shell
> docker stop $(docker ps -q)
> ```
>
> You can replace `docker stop` with `docker kill` in the above command to forcibly stop the containers.
>
> Finally, we can delete all containers:
>
> ```shell
> docker rm $(docker ps -a -q)
> ```





### Removing dangling images

> Dangling images, as mentioned by the [documentation](https://docs.docker.com/engine/reference/commandline/images/#filtering), are final images (i.e, not intermediary build layers) that no longer have an associated tag with them.
>
> We can get the image ID for such images as follows:
>
> ```shell
> docker images --filter dangling=true -q
> ```
>
> Then, we can delete those images with the following command:
>
> ```shell
> docker rmi $(docker images --filter dangling=true -q)
> ```
>
> A one-liner alternative to remove all dangling images is:
>
> ```shell
> docker image prune
> ```





### Removing all images

> Docker doesn’t allow to remove images that have an associated container, so to really delete all images, it is necessary first to [remove all containers](https://renehernandez.io/snippets/cleaning-local-docker-cache/#removing-all-containers).
>
> Similarly to the previous section, we need the IDs of all the images, which we can get using:
>
> ```shell
> docker images -a -q
> ```
>
> Then, we can combine it with `docker rmi`:
>
> ```shell
> docker rmi $(docker images -a -q)
> ```
>
> A one-liner alternative to remove all images is:
>
> ```shell
> docker image prune -a
> ```





### Removing volumes

> Volumes also take space in the host machine. They are never deleted automatically since they may contain data that can be reused by different containers or directly from the host.
>
> Then, to remove all docker volumes use:
>
> ```shell
> docker volume prune
> ```





### Removing networks

> Although docker networks don’t consume too much disk space, they create `iptables` rules, network devices and routing table entries. To prune these objects, you can run:
>
> ```shell
> docker network prune
> ```





**Removing everything**

> Instead of manually pruning different types of resources, you may be interested on wiping out everything from your local cache. For that we can leverage the `docker system prune` command as follows:
>
> To remove containers, images and networks use:
>
> ```shell
> docker system prune
> ```
>
> To remove containers, images, networks and volumes, use
>
> ```shell
> docker system prune --volumes
> ```