diff --git a/Dockerfile b/Dockerfile index 2c49017..b8bc66b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -103,5 +103,14 @@ RUN chmod +x /root/samples/run_sample.sh ENV DEBIAN_FRONTEND teletype ENV TZ="" +# Copy the entrypoint script into the container +COPY entrypoint.sh /usr/local/bin/entrypoint.sh + +# Make the entrypoint script executable +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Set the entrypoint to the entrypoint script +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] + # Define the default command to run when the container starts CMD ["/bin/bash"] diff --git a/README.md b/README.md index 03aeeec..af1d89c 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ - **Regular Updates**: Weekly checks and updates for core tools ensure the toolkit's reliability and security. - **Sample code**: Includes sample code demonstrating the usage of various tools available in the toolkit. - **Support for Build Variants**: Enables users to customize the toolkit by building it with their preferred versions of each tool. +- **Support Configuration Reusable**: Mounts a config folder on the host to the container.This allows reusing configurations in the container, like AWS and Azure login sessions, ... ## Prerequisites 🔓 @@ -36,29 +37,39 @@ Below is the versioning strategy for the repository and DockerHub: - Repository: `vX.Y.Z`, for example: `v1.2.3` - DockerHub: `X.Y.Z`, for example: `1.2.3`. (Usage: `docker pull tungbq/devops-toolkit:1.2.3`) - Tagging description: - - Specific tag (e.g., v0.1.0, v0.2.3): Contains the latest tooling version and repository features at the time this repository is tagged. - - In addition to that, we offer the latest tag on DockerHub (latest): Contains the latest tooling version and repository features inside the toolkit, which will be built and updated on a weekly basis. + - Specific tag (e.g., `0.1.0`, `0.2.3`): Contains the latest tooling version and repository features at the time this repository is tagged. + - In addition to that, we offer the latest tag on DockerHub (`latest`): Contains the latest tooling version and repository features inside the toolkit, which will be built and updated on a weekly basis. _NOTE_: In the following section, we use the latest tag in the documentation, but you can specify your desired tag based on your needs. ## Quick start 🔥 -```bash -# Use latest tag -docker run --network host -it --rm tungbq/devops-toolkit:latest +- Use latest tag -# Use specific tag -docker run --network host -it --rm tungbq/devops-toolkit:0.1.0 -``` + ```bash + mkdir ~/.devops-toolkit-config + docker run --network host -it --rm -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest + ``` -## Demo 📺 +- Use specific tag -Check out the full sample and instruction at [**samples**](./samples/) + ```bash + docker run --network host -it --rm -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:0.1.0 + ``` + + _NOTE_: + + - Note: You can replace `~/.devops-toolkit-config` with any desired folder path on your VM. + - Remove the `-v ~/.devops-toolkit-config:/config` option if you do not wish to store configurations on the host (not recommended for configuration reuse). + +## Demo 📺 ```bash -docker run --network host --rm tungbq/devops-toolkit:latest samples/run_sample.sh +docker run --network host --rm -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest samples/run_sample.sh ``` +Check out the full sample and instruction at [**samples**](./samples/) + ## Getting started 📖 ### Pull the official image from Docker Hub @@ -82,32 +93,44 @@ Once you have the image ready, you can start using the toolkit with the followin - Start devops-toolkit container -```bash -docker run --network host -it --rm tungbq/devops-toolkit:latest -``` + ```bash + docker run --network host -it --rm -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest + ``` - Now we are in the docker container terminal, let's explore it -```bash -root@docker-desktop:~# python3 --version -Python 3.12.2 + ```bash + root@docker-desktop:~# python3 --version + Python 3.12.2 -root@docker-desktop:~# terraform --version -Terraform v1.7.5 -on linux_amd64 + root@docker-desktop:~# terraform --version + Terraform v1.7.5 + on linux_amd64 -root@docker-desktop:~# kubectl version -Client Version: v1.29.3 -Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 + root@docker-desktop:~# kubectl version + Client Version: v1.29.3 + Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 -# ... more command as your needed -``` + root@docker-desktop:~# aws configure + root@docker-desktop:~# az login --use-device-code + + # ... more command as your needed + ``` + +## User Guide 📖 + +Explore the comprehensive guide below to gain insight into the detailed utilization of every tool within the toolkit. + +- Prepare configuration folder on the Host (Skip this step if you do not intend to reuse configurations.) -## User guide 📖 + ```bash + mkdir ~/.devops-toolkit-config + ``` -Explore the comprehensive guide below to gain insight into the detailed utilization of every tool within the toolkit + _NOTE:_ We are using `~/.devops-toolkit-config` to store toolkit configurations, but you can choose any folder name on the host. -- [**DevOps toolkit user guide**](./docs/usage/README.md) +- For detailed instructions on using specific tools, refer to: [**DevOps toolkit specific tool user guide**](./docs/usage/README.md) +- For instructions on common run modes, visit [**DevOps toolkit common run mode**](./docs/usage/run_mode.md) ## The DevOps Toolkit Core 🧰 diff --git a/docs/usage/ansible_usage.md b/docs/usage/ansible_usage.md index 08e36e1..0458681 100644 --- a/docs/usage/ansible_usage.md +++ b/docs/usage/ansible_usage.md @@ -15,10 +15,14 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). + ## Use case 1: Run Ansible sample code provided in the container ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -v ~/.devops-toolkit-config:/config -it tungbq/devops-toolkit:latest # You now in the container terminal ansible-playbook samples/ansible/check_os.yml @@ -27,7 +31,7 @@ ansible-playbook samples/ansible/check_os.yml ## Use case 2: Clone external code inside container ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -v ~/.devops-toolkit-config:/config -it tungbq/devops-toolkit:latest # You now in the container terminal # Now run your cloned script @@ -45,7 +49,7 @@ Clone the code to the host then mount to container ```bash # Given that we have code somewhere in you machine -docker run --rm -v "$(pwd)":/root/ansible_workspace --network host -it tungbq/devops-toolkit:latest +docker run --rm -v "$(pwd)":/root/ansible_workspace --network host -v ~/.devops-toolkit-config:/config -it tungbq/devops-toolkit:latest # Run the ansible code as usual ``` @@ -55,7 +59,7 @@ Clone the code to the host then mount code and `.ssh` folder to container ```bash # Given that we have code somewhere in you machine -docker run --rm -v ~/.ssh:/root/.ssh -v "$(pwd)":/root/ansible_workspace --network host -it tungbq/devops-toolkit:latest +docker run --rm -v ~/.ssh:/root/.ssh -v "$(pwd)":/root/ansible_workspace --network host -v ~/.devops-toolkit-config:/config -it tungbq/devops-toolkit:latest # Run the ansible code as usual ``` diff --git a/docs/usage/awscli_usage.md b/docs/usage/awscli_usage.md index a1959c2..2918ac1 100644 --- a/docs/usage/awscli_usage.md +++ b/docs/usage/awscli_usage.md @@ -19,10 +19,14 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). + ## Use case 1: Configure credentials and list S3 bucket with awscli ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest ############################################### # Now we are in the docker container terminal # ############################################### @@ -42,16 +46,6 @@ Default region name [None]: xxxxxxxx Default output format [None]: xxxxxxxx ``` -## Use case 2: Using AWS config from the host - -Mount the `.aws` when running toolkit container - -```bash -docker run --rm --network host -it -v ~/.aws:/root/.aws tungbq/devops-toolkit:latest -# List bucket -aws s3 ls -``` - ## Troubleshooting - For any issues, check [this reference](../troubleshooting/TROUBLESHOOTING.md) diff --git a/docs/usage/azurecli_usage.md b/docs/usage/azurecli_usage.md index 9efbb6b..1a2b561 100644 --- a/docs/usage/azurecli_usage.md +++ b/docs/usage/azurecli_usage.md @@ -18,25 +18,19 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). + ## Use case 1: Az login and run command ```bash -docker run --rm -it tungbq/devops-toolkit:latest -## To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code to authenticate -az login -# List all resource groups -az group list -``` - -## Use case 2: Using Azure config from the host +docker run --rm -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest -Mount the `.azure` folder from host when running toolkit container +# Login with AZ CLI +az login --use-device-code +## To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code to authenticate -```bash -docker run --rm -it -v ~/.azure:/root/.azure tungbq/devops-toolkit:latest -############################################### -# Now we are in the docker container terminal # -############################################### # List all resource groups az group list ``` @@ -44,7 +38,6 @@ az group list Sample Result ```bash -➜ ~ docker run --rm -it -v ~/.azure:/root/.azure tungbq/devops-toolkit:latest root@f097467db632:~# az group list [ { diff --git a/docs/usage/helm_usage.md b/docs/usage/helm_usage.md index 473e478..1c16403 100644 --- a/docs/usage/helm_usage.md +++ b/docs/usage/helm_usage.md @@ -19,12 +19,14 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` -## Use case 1: Deploy an application with Helm +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). -Mount the `.kube/config` file from the host to container (or other kubeconfig files you had) +## Use case 1: Deploy an application with Helm ```bash -docker run --rm --network host -it -v ~/.kube/config:/root/.kube/config tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest ############################################### # Now we are in the docker container terminal # ############################################### diff --git a/docs/usage/kubectl_usage.md b/docs/usage/kubectl_usage.md index 07a7a5e..a0cd5a9 100644 --- a/docs/usage/kubectl_usage.md +++ b/docs/usage/kubectl_usage.md @@ -15,12 +15,16 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` -## Use case 1: Use kubeconfig from the host +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). + +## Use case 1: Use kube config from the host Mount the `.kube/config` file from the host to container ```bash -docker run --rm --network host -it -v ~/.kube/config:/root/.kube/config tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest ############################################### # Now we are in the docker container terminal # ############################################### @@ -38,7 +42,6 @@ kubectl get deployment Sample Result ```bash -➜ ~ docker run --rm --network host -it -v ~/.kube/config:/root/.kube/config tungbq/devops-toolkit:latest root@docker-desktop:~# kubectl get nodes NAME STATUS ROLES AGE VERSION kind-control-plane Ready control-plane 21m v1.29.2 diff --git a/docs/usage/python_usage.md b/docs/usage/python_usage.md index 9047b65..d266b70 100644 --- a/docs/usage/python_usage.md +++ b/docs/usage/python_usage.md @@ -16,10 +16,14 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). + ## Use case 1: Run python sample code provided in the container ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest # You now in the container terminal python3 samples/python/rectangle_area_calculator.py ``` @@ -27,7 +31,7 @@ python3 samples/python/rectangle_area_calculator.py ## Use case 2: Clone external code inside container ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest # You now in the container terminal # Clone code mkdir python_workspace @@ -48,7 +52,7 @@ Clone the code to the host then mount to container ```bash # Given that we have code somewhere in you machine -docker run --rm -v "$(pwd)":/root/python_workspace --network host -it tungbq/devops-toolkit:latest +docker run --rm -v "$(pwd)":/root/python_workspace --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest # Run the python code as usual ``` diff --git a/docs/usage/run_mode.md b/docs/usage/run_mode.md new file mode 100644 index 0000000..7d507bd --- /dev/null +++ b/docs/usage/run_mode.md @@ -0,0 +1,52 @@ +# DevOps toolkit run mode + +The DevOps Toolkit image can be run in various modes. See [docker/container/run](https://docs.docker.com/reference/cli/docker/container/run/) for more information. Check the document below for further details. + +## Run option + +Command: `docker run tungbq/devops-toolkit:`, some of the common options can be used in any combination: + +- `-it`: Interactive mode. +- `--rm`: Remove the container after it completes (Ctrl + C or container exit event). +- `--network host`: Use the host (VM) network. +- `--name `: Assign a name to the container (e.g: `--name demo001`). +- `-v :/config`: Mount a config folder on the host to the container. This allows reusing configurations in the container, like AWS and Azure login sessions (e.g: `-v ~/.devops-toolkit-config:/config`, `-v /tmp/devops-toolkit-config-01:/config`). + +## Example + +- Run the image in interactive mode: + +```bash +docker run -it --rm tungbq/devops-toolkit:latest +``` + +- Run a specific tag of the toolkit: + +```bash +docker run -it --rm tungbq/devops-toolkit:0.1.0 +``` + +- Run the image with host network and remove the container after it completes: + +```bash +docker run --network host -it --rm tungbq/devops-toolkit:latest +``` + +- Run the image with default configuration and keep the container: + +```bash +docker run --network host -it tungbq/devops-toolkit:latest +``` + +- Run the image with default configuration, specify the container name, and keep the container: + +```bash +docker run --network host -it --name demo001 tungbq/devops-toolkit:latest +``` + +- Run the image and mount the configuration from the host, and remove the container after it completes: + +```bash +mkdir -p ~/.devops-toolkit-config # or other location you want to store the configuration +docker run --network host -it --rm -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest +``` diff --git a/docs/usage/terraform_usage.md b/docs/usage/terraform_usage.md index e213d29..7d2869f 100644 --- a/docs/usage/terraform_usage.md +++ b/docs/usage/terraform_usage.md @@ -15,10 +15,14 @@ To use the existing container instead of creating one, use `docker exec` command docker exec -it my_devops_toolkit /bin/bash ``` +## Common Run Modes + +For instructions on common run modes, visit [**DevOps Toolkit Common Run Mode**](../usage/run_mode.md). + ## Use case 1: Run terraform sample code provided in the container ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest # You now in the container terminal # Navigate to Terraform sample pushd samples/terraform/basic @@ -34,7 +38,7 @@ popd ## Use case 2: Clone external code inside container ```bash -docker run --rm --network host -it tungbq/devops-toolkit:latest +docker run --rm --network host -it -v ~/.devops-toolkit-config:/config tungbq/devops-toolkit:latest # You now in the container terminal # Now run your cloned script @@ -52,7 +56,7 @@ Clone the code to the host then mount to container ```bash # Given that we have code somewhere in you machine -docker run --rm -v "$(pwd)":/root/terraform_workspace --network host -it tungbq/devops-toolkit:latest +docker run --rm -v "$(pwd)":/root/terraform_workspace -v ~/.devops-toolkit-config:/config --network host -it tungbq/devops-toolkit:latest # Run the terraform code as usual ``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..64adcd5 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +CONFIG_DIR=/config + +# Function to create symlink if source exists +create_symlink() { + local src=$1 + local dest=$2 + if [ -e "$src" ]; then + ln -sf "$src" "$dest" + echo "Symlink created: $src -> $dest" + fi +} + +# Check if the /config directory exists and is not empty +if [ -d "$CONFIG_DIR" ]; then + echo "Custom configuration directory detected. Setting up symlinks..." + + # TODO: Check and only create if folder does not exist + ## config mount point + mkdir -p /config/.aws \ + /config/.azure \ + /config/.kube \ + /config/.terraform.d \ + /config/.config/helm \ + /config/.ansible + + ## root container path + mkdir -p /root/.config/ + + create_symlink "$CONFIG_DIR/.aws" "/root/.aws" + create_symlink "$CONFIG_DIR/.azure" "/root/.azure" + create_symlink "$CONFIG_DIR/.kube" "/root/.kube" + create_symlink "$CONFIG_DIR/.terraform.d" "/root/.terraform.d" + create_symlink "$CONFIG_DIR/.config/helm" "/root/.config/helm" + create_symlink "$CONFIG_DIR/.ansible" "/root/.ansible" + create_symlink "$CONFIG_DIR/.gitconfig" "/root/.gitconfig" +else + echo "No custom configuration directory detected. Using default configurations..." +fi + +# Execute the provided command +exec "$@"