# Docker Commands

Here’s a list of commands to remember for docker administration.

#### List Containers

To list all docker containers on a host:

```bash
sudo docker ps -a
```

#### Remove Containers

To remove all docker containers on a host:

```bash
sudo docker rm -f $(sudo docker ps -aq)
```

#### Container Logs

To see the logs for a container:

```bash
sudo docker logs containername
```

#### Container Stats

To get the stats of docker containers, as a single snapshot:

```bash
docker stats --no-stream
```

<span data-annotation-inline-node="true" data-annotation-mark="true" data-card-url="https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes" data-inline-card="true" data-renderer-start-pos="369"><span class="loader-wrapper"><span data-testid="hover-card-trigger-wrapper">Additional commands are here: </span></span></span><span data-annotation-inline-node="true" data-annotation-mark="true" data-card-url="https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes" data-inline-card="true" data-renderer-start-pos="369"><span class="loader-wrapper"><span data-testid="hover-card-trigger-wrapper">[<span class="_19itglyw _vchhusvi _r06hglyw _o5721jtm _1nmz9jpi _16d9qvcn _ca0qv77o _u5f31b66 _n3tdv77o _19bv1b66" data-testid="inline-card-icon-and-title"><span class="_19itglyw _vchhusvi _r06hglyw">How To Remove Docker Images, Containers, and Volumes | DigitalOcean</span></span>](https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes)</span></span></span>

#### Copy Files from Container

To copy files out of a docker container, do this:

```bash
sudo docker cp <imageid>:/pathincontainer ~/pathonhost
```

#### Running a Terminal Container

Sometimes, it’s necessary to spin up a blank docker container for testing network or other facilities from a terminal session in the container.

Here’s a quick command that will spin up a container with an ubuntu bash terminal.

<p class="callout info">NOTE: It also adds an /etc/hosts entry of (host.docker.internal) which is the docker entry for accessing the container’s host machine.</p>

This is useful if you are testing visibility of any host services from the container.

```bash
docker run --rm -it --add-host host.docker.internal:host-gateway --entrypoint bash ubuntu
```

Once started and at the terminal, you can install network utilities such as ping, curl, etc with this:

```bash
apt-get update; apt-get install curl; apt-get install inetutils-ping; apt-get install net-tools
```

#### Opening a Terminal Inside An Active Container

If you want to open a terminal, inside an already running container, you can use this command:

```bash
docker exec -it <container_id_or_name> /bin/bash
```

If the container doesn't have bash, use this:

```bash
docker exec -it <container_id_or_name> /bin/sh
```

As well, you can install network utilities, such as Ping, curl, etc, into the container while in its terminal:

```
apt-get update; apt-get install curl; apt-get install inetutils-ping; apt-get install net-tools
```

#### Get Docker Gateway Address

Here’s a pair of commands to get the docker’s bridge network gateway address and subnet:

```bash
docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}'

docker network inspect bridge --format='{{(index .IPAM.Config 0).Subnet}}'
```