My Docker Cheat Sheet

My Docker Cheat Sheet

This is a collection of some Docker statements. Mainly for myself to copy-paste and save time. copy-paste-meme

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
  2. Edit yourdocker-compose.yml as follows

    version: "3.8"
     image: mysql:8.0.33
     container_name: mysql8.0.33
     restart: always
       MYSQL_ROOT_PASSWORD: secret
       MYSQL_DATABASE: playground
       - "./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"
       - 3306:3306
     image: adminer
     restart: always
       - 8080:8080
  3. 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;
  4. You can add your initial data in scripts/data.sql
    INSERT INTO test (name)
    VALUES ("Dennis");
  5. Run your docker file with docker-compose
    docker-compose up
  6. Open your browser at localhost:8080, login to your MySQL instance and have fun.

2. Exploring the docker filesystem

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.
-i (interactive): This option enables you to provide input to the command running inside the container.

3. Connect to mysql via command line

docker exec -t -i <container_name> mysql -u my_user -p

4. Copy a file from docker container to host machine

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
    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

    docker images

    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.

    docker tag quarkus/popup-chinese-backend-quarkus dcnis/popup-chinese-backend-quarkus
  5. Check the images
    docker images
    quarkus/popup-chinese-backend-quarkus                 latest
    dcnis/popup-chinese-backend-quarkus                 latest
  6. Push the image
    % docker push dcnis/popup-chinese-backend-quarkus
    Using default tag: latest
    The push refers to repository []
    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.
    % 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