# Commands

## List all the images

```bash
docker images
```

## Download image

<https://hub.docker.com/>

```bash
# docker pull [image]
docker pull mysql
```

## Delete image

```bash
# docker image rm [image]
# docker image rm [image]:[tag]
docker image rm mysql
docker image rm mysql:latest
docker image rm mysql:16
```

## Create a container with the image

### With random container name

It will generate an ID to the container.

```bash
# docker container create [image]
# docker create [image]
docker create mysql
```

Id generated:

```
a6e16354239fc76b4cc89216bfe46f0260500a7e798d62e319af213345bb13c8
```

### With custom container name

```bash
docker create --name monguito mongo
```

Id generated:

```
e1d70ca8719dcf07987eae76bac9a621d4531e9c9b8bdf8328b647e649d3f438
```

## Start a container

### Using the ID of the container

<pre class="language-bash"><code class="lang-bash"><strong># docker start [container_id]
</strong><strong>docker start a6e16354239fc76b4cc89216bfe46f0260500a7e798d62e319af213345bb13c8
</strong></code></pre>

### Using the custom name of the container

```bash
docker start monguito
docker ps # we will see the container running
```

### Using environment variables

For example, to create a mongodb container with specific user and password, to then use an app to connect to it, we use the env variable described in mongodb hub:

<https://hub.docker.com/_/mongo>

> Each image has different ways to configure

```bash
docker create -p27017:27017 --name monguito -e MONGO_INITDB_ROOT_USERNAME=nico -e MONGO_INITDB_ROOT_PASSWORD=password mongo
```

## Status of containers running&#x20;

```bash
docker ps
```

```bash
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS       NAMES
a6e16354239f   mysql     "docker-entrypoint.s…"   4 minutes ago   Up 2 seconds   27017/tcp   pedantic_rubin
```

## Stop a container

```bash
docker stop a6e16354239f
docker ps
# There is no one container running
```

## List of containers started and off

List all the containers in system.

```bash
docker ps --all
docker ps -a
```

```bash
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS                    PORTS     NAMES
a6e16354239f   mysql     "docker-entrypoint.s…"   22 hours ago   Exited (0) 22 hours ago             pedantic_rubin
```

## Delete a container

#### Using name

```bash
docker rm pedantic_rubin
docker ps -a #container deleted
```

## Expose a container in a host port

### Custom port

```bash
# docker create -p[port_in_host]:[port_in_container] --name [name] [image]
docker create -p27017:27017 --name monguito mongo
```

```bash
docker start monguito
docker ps
```

```bash
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
6c97203a3708   mongo     "docker-entrypoint.s…"   2 minutes ago   Up 2 seconds   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   monguito
```

### Random port

```bash
# docker create -p[port_in_container] --name [name] [image]
docker create -p27017 --name manguito mongo
```

```bash
docker start manguito
docker ps
```

```bash
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
2bdec62e384c   mongo     "docker-entrypoint.s…"   6 seconds ago   Up 1 second    0.0.0.0:32768->27017/tcp, :::32768->27017/tcp   manguito
```

## Check logs of container

```bash
docker logs monguito
```

Or to stay watching those logs:

```bash
docker logs --follow monguito
```

## Download the image, create and start the container

### With logs view, random data

```bash
docker run mongo
```

### Without logs view, detach mode and random data

```bash
docker run -d mongo
```

```
Unable to find image 'mongo:latest' locally
latest: Pulling from library/mongo
57c139bbda7e: Pull complete 
2a7c884ecb1c: Pull complete 
ddcbc3e219f6: Pull complete 
347abad2978b: Pull complete 
33ad7d6a71b8: Pull complete 
46b764746687: Pull complete 
0d22e1cb5ef2: Pull complete 
fef1dc21c099: Pull complete 
Digest: sha256:175ee5c400f24d2766cea492b4986851b4e7ec75fc08e354673149b86488019c
Status: Downloaded newer image for mongo:latest
309059638da6dc66af6cd40312bef60c36b1b7b6056bc6b3f38d824ab39b2e26
```

It generates our container ID:

```
309059638da6dc66af6cd40312bef60c36b1b7b6056bc6b3f38d824ab39b2e26
```

```bash
docker ps          
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS       NAMES
309059638da6   mongo     "docker-entrypoint.s…"   About a minute ago   Up About a minute   27017/tcp   crazy_thompson
```

### Without logs view and custom data

```bash
docker run -d --name monguito -p27017:27017 mongo
#a146ef22b6bb96889a273d8037f4613eefb5519e063faa8f84555e1fb43072e8
```

```bash
docker ps                                        
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                                           NAMES
a146ef22b6bb   mongo     "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   monguito
```

## Interact with networks

### List networks

```bash
docker network ls
```

```bash
NETWORK ID     NAME      DRIVER    SCOPE
b17449f934b1   bridge    bridge    local
6b89bd5bc050   host      host      local
fdc75e667bd0   none      null      local
```

### Create a network

```bash
# docker network create [nombre]
docker network create mired
```

```bash
NETWORK ID     NAME      DRIVER    SCOPE
b17449f934b1   bridge    bridge    local
6b89bd5bc050   host      host      local
9cbcc9aa1f95   mired     bridge    local
fdc75e667bd0   none      null      local
```

### Delete a network

```bash
docker network rm mired
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lanzt.gitbook.io/cheatsheet-pentest/docker/commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
