---
tags: 🛠 tools
---
<style>
/* body {
background-color: #0093E9;
background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%) !important;
background-size: cover !important;
background-position: center !important;
background-attachment: fixed !important;
} */
div#doc pre {
filter: invert(1);
}
code {
font-family: "JetBrains Mono", "Cascadia Code";
}
/* div#doc {
margin-bottom: 25px !important;
padding: 25px;
background: #ffffff44;
backdrop-filter: blur(20px);
color: white;
border-radius: 10px;
} */
h1 {
display: flex;
justify-content: center;
align-items: center;
height: 15vh;
text-align: center;
font-size: 40px !important;
}
</style>
# 🐳 Docker
:::info
:information_source: **Disclaimer**
This note is for **`controller`** level so that this note doesn't contain any in-depth information. The purpose of this note is for quick referencing and explain **how to use** the tool in simple language.
:::
:::info
:link: **Further Reading**
- Docker documentation: https://docs.docker.com/
- `@LearnXinYminutes/docker`: https://learnxinyminutes.com/docs/docker/
:::
<hr>
:::success
:information_source: **Conventions**
- `$variable`: In the codeblocks below, the term started with `$` sign is the terms that need to be filled with your values. (*Eg: `$name` would be replaced by `postgres`*)
- `[optional]`: An optional field, it could be replaced or be omitted
- `(term1|term2)`: An option group, you have to chose between values inside the brackets
- `[term1|term2]`: An optional group, you have to chose between values inside the brackets or omit the group.
- The codeblocks have the line number would be **followed step by step**
- The codeblocks don't have line number would be treated as ***an option group***
:::
## Common commands
Docker has 2 types of commands:
```bash
docker ($mc|$ac) [--$option [$value]] [$action] $value
```
Eg:
```bash
docker image ls
```
- **Management commands** (`mc`)
- There are the major commands such as
`builder`, `buildx`, `compose`, `config`, `container`, `context`,
`manifest`, `network`, `node`, `plugin`, `scan`, `secret`, `service`,
`stack`, `swarm`, `system`, `trust`, `volume`
- **Action commands** (`ac`)
## UseCase: How to deploy Postgres Image on Docker
1. We firstly need to fetch the postgres image if the local machine doesn't contain it.
**Note:** Postgres requires the authentication password, so that we have to define it as an environment variable with `-e` option.
```bash=
docker run -e -d POSTGRES_PASSWORD=$password postgres
```
:::spoiler ❓ *What if I don't attach the password into the command?*
```bash
docker run postgres
```
Result:
```bash
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html
```
:::
2. When the container is runned successfully, we will check it's name with **`docker ps`**
Result:
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc9bf443024c postgres "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 5432/tcp gracious_mccarthy
```
3. And now we start to run the postgres with container's CLI
```bash=
docker exec -it "gracious_mccarthy" bash
```
- `-i`: ***Interactive***, allows you to interact with the shell
- `-t`: ***TTY***, give the output as a normal terminal
We are in the postgres container bash shell now, let open the postgres CLI `psql` and try some commands.
```=
root@bc9bf443024c:/# ls
bin dev etc lib media opt root sbin sys usr
boot docker-entrypoint-initdb.d home lib64 mnt proc run srv tmp var
root@bc9bf443024c:/# su postgres
postgres@bc9bf443024c:/$ psql
psql (14.2 (Debian 14.2-1.pgdg110+1))
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
postgres=#
```