# My Docker Cheat Sheet

This is a collection of some Docker statements. Mainly for myself to copy-paste and save time.
![copy-paste-meme](https://media.tenor.com/0heitU7-tg4AAAAC/copy-paste-paste.gif align="center")

# 1. Docker file template with MySQL and adminer
1. In your `root directory`, create following files
```
.
├── scripts/
│   ├── schema.sql
│   ├── data.sql
│   └── mycustom.cnf
└── docker-compose.yml
```
1. Edit your`docker-compose.yml` as follows
```bat
version: "3.8"
services:
  db:
    image: mysql:8.0.33
    container_name: mysql8.0.33
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: playground
    volumes:
      - "./scripts/schema.sql:/docker-entrypoint-initdb.d/1.sql"
      - "./scripts/data.sql:/docker-entrypoint-initdb.d/2.sql"
      - "./scripts/mycustom.cnf:/etc/mysql/conf.d/custom.cnf"
    ports:
      - 3306:3306

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
```
1. You can add your initial database schema in `scripts/schema.sql`
```
CREATE TABLE `test` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
```
1. You can add your initial data in `scripts/data.sql`
```
INSERT INTO test (name)
VALUES ("Dennis");
```
1. Run your docker file with `docker-compose`
```
docker-compose up
```
1. Open your browser at `localhost:8080`, login to your MySQL instance and have fun.

# 2. Exploring the docker filesystem
```bat
docker exec -t -i <container_name> /bin/bash
```
`-t` (terminal): This option allows you to interact with the container using a terminal-like interface. It is commonly used when you need to run interactive commands inside the container, such as an interactive shell session.<br>
`-i` (interactive): This option enables you to provide input to the command running inside the container.<br>

# 3. Connect to mysql via command line
```bat
docker exec -t -i <container_name> mysql -u my_user -p
```

# 4. Copy a file from docker container to host machine
```bat
docker cp <container-id>:/file/path/within/container /host/file/path/target
```

# 5. Publish docker image to Docker Hub
1. When you login to your Docker Hub, you will see the default command how to push a image to your docker hub. For example
```bat
docker push dcnis/popup-chinese-backend-quarkus:tagname
```
2. If you would execute this command locally, you will probably get an error which says docker cannot find the image with the tag ```dcnis/popup-chinese-backend-quarkus```. So first we have to create a new ```tag```.
3. List docker images
```bat
docker images
quarkus/popup-chinese-backend-quarkus
```
We can see we have one images with the name ```quarkus/popup-chinese-backend-quarkus```. Based on this image, we create the new tag. 

4. Create a new ```tag``` from your existing docker image.
```bat
docker tag quarkus/popup-chinese-backend-quarkus dcnis/popup-chinese-backend-quarkus
```
5. Check the images
```bat
docker images
quarkus/popup-chinese-backend-quarkus                 latest
dcnis/popup-chinese-backend-quarkus                 latest
```
6. Push the image
```bat
% docker push dcnis/popup-chinese-backend-quarkus
Using default tag: latest
The push refers to repository [docker.io/dcnis/popup-chinese-backend-quarkus]
48efe9a978d6: Pushed 
5f77935eec15: Pushed 
3b6ba2682483: Pushed 
129f98c51b7a: Pushed 
41c90dc5a18e: Pushed 
5f70bf18a086: Pushed 
5282f3464e78: Pushed 
latest: digest: sha256:0baf3b80fdc4ea7fd73f199f88d8c91099e3191ca8705dd0e79d68dd67013d01 size: 1780
```
7. Continuous workflow: To speed up your development process, you can create the image directly with the correct name and then push the new image to Docker Hub.
```bat
% docker build -f src/main/docker/Dockerfile.native-micro -t dcnis/popup-chinese-backend-quarkus .
% docker push dcnis/popup-chinese-backend-quarkus
```

# 6. Spin up PostgreSQL Database with Docker
```
docker run --name postgresql -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres
```



