Skip to content

Dockerfiles

There are a few helper scripts that can be used to simplify the process of building and running Docker containers. These scripts are installed in the process of setting up dotfiles and can be found in the .local/bin directory.

dockerfiles-clone Script

The dockerfiles-clone script is designed to help you clone or update the Dockerfiles repository on your system. To use this script, follow these steps:

  1. Configure Environment Variables (optional): If you have specific values for DOCKERFILES_REPO or DOCKERFILES_DIR, you can set these environment variables before running the script. Otherwise, the script will use the default values specified in the script.
export DOCKERFILES_REPO="https://github.com/username/dockerfiles.git"
export DOCKERFILES_DIR="/path/to/dockerfiles"
  1. Run the Script: Execute the dockerfiles-clone script:
dockerfiles-clone

This script will perform the following tasks:

  • Check if the Dockerfiles repository is set. If not, it will display a message and exit.
  • If the Dockerfiles directory exists, it will prompt you to remove the existing directory.
    • If you choose to remove the existing directory, it will delete the directory and proceed to clone the repository.
    • If you choose not to remove the existing directory, it will display a message and exit.
  • If the Dockerfiles directory does not exist, it will clone the Dockerfiles repository to the specified directory.

After running the dockerfiles-clone script, your Dockerfiles repository will be cloned or updated, and you can start using the Dockerfiles on your system.

dockerfiles-pull Script

The dockerfiles-pull script helps you update the Dockerfiles repository on your system. To use this script, follow these steps:

  1. Configure Environment Variables (optional): If you have specific values for DOCKERFILES_REPO or DOCKERFILES_DIR, you can set these environment variables before running the script. Otherwise, the script will use the default values specified in the script.
export DOCKERFILES_REPO="https://github.com/username/dockerfiles.git"
export DOCKERFILES_DIR="/path/to/dockerfiles"
  1. Run the Script: Execute the dockerfiles-pull script:
dockerfiles-pull

This script will perform the following tasks:

  • Check if the Dockerfiles directory exists.
  • If the directory exists and is a valid Git repository, it will update the Dockerfiles repository using git pull --rebase --autostash.

After running the dockerfiles-pull script, your Dockerfiles repository will be updated, and you can continue using the latest Dockerfiles on your system.

dockerfiles-sync Script

The dockerfiles-sync script helps you synchronize Dockerfiles between your project directory and the shared Dockerfiles directory. To use this script, follow these steps:

  1. Configure Environment Variables (optional): If you have specific values for WORKSPACE_PROJECT_DIR or DOCKERFILES_SHARE_DIR, you can set these environment variables before running the script. Otherwise, the script will use the default values specified in the script.
export WORKSPACE_PROJECT_DIR="/path/to/workspace/project/directory"
export DOCKERFILES_SHARE_DIR="/path/to/dockerfiles/share/directory"
  1. Run the Script: Execute the dockerfiles-sync script with the desired options:
dockerfiles-sync [DOCKER_NAME] [--from-project] [--project-directory PROJECT_DIR] [--dockerfiles-directory DOCKERFILES_DIR] [-h|--help]

Available options:

  • DOCKER_NAME: Specify the name of a specific Docker project to sync.
  • --from-project: Sync Dockerfiles from the project directory to the shared Dockerfiles directory.
  • --project-directory PROJECT_DIR: Set the project directory path.
  • --dockerfiles-directory DOCKERFILES_DIR: Set the shared Dockerfiles directory path.
  • -h|--help: Display usage information.

Examples:

  • Sync all Dockerfiles from the shared Dockerfiles directory to the workspace project directory:

    dockerfiles-sync
    
  • Sync a specific Dockerfile from the shared Dockerfiles directory to the workspace project directory:

    dockerfiles-sync my-docker-project
    
  • Sync all Dockerfiles from the workspace project directory to the shared Dockerfiles directory:

    dockerfiles-sync --from-project
    
  • Sync a specific Dockerfile from the workspace project directory to the shared Dockerfiles directory:

    dockerfiles-sync my-docker-project --from-project
    

After running the dockerfiles-sync script, your Dockerfiles will be synchronized between the specified directories. The dk-compose script is a convenient wrapper for managing Docker Compose projects. To use the script, follow the usage format below:

dk-compose Script

dk-compose is a script for managing Docker containers using Docker Compose, with a comprehensive set of options for configuring the environment.

Usage

dk-compose COMMAND DOCKER_NAME [OPTIONS]

Arguments

  • COMMAND - The Docker Compose command to be executed. Valid commands include:

  • build: Build the Docker services as defined in the Compose file.

  • run: Execute a one-off command on a service.
  • up: Create and start the containers as per the Compose file.
  • down: Halt and remove containers, networks, and volumes as defined in the Compose file.
  • config: Validate and view the Compose file.

  • DOCKER_NAME - The name for the Docker project.

Options

  • -t, --tag TAG: Use a specific tag for the Docker Compose file and the environment file. The script will attempt to find files named docker-compose-TAG.yaml and docker.TAG.env, where TAG is the provided tag.

  • --from-project: Indicate to use the project directory as the source for the Docker Compose files, overriding the default Dockerfiles directory.

  • --project-directory PROJECT_DIR: Set a custom path to the project directory. If not specified, the script defaults to the preconfigured project directory.

  • --dockerfiles-directory DOCKERFILES_DIR: Set a custom path to the directory containing Dockerfiles. If not specified, the script defaults to the preconfigured Dockerfiles directory.

  • -c, --compose-filename COMPOSE_FILENAME: Indicate a custom Docker Compose filename. If not specified, the script defaults to docker-compose.yaml or docker-compose-TAG.yaml based on the tag provided.

  • -e, --env-file FILE: Specify the environment file for Docker Compose.

  • --shell-env-file FILE: Specify the shell environment file. If not provided, the script defaults to docker.env in the Dockerfiles directory.

  • --global-env-file FILE: Specify the global environment file.

  • -v, --version VERSION: Set the version of the Docker image to use. This will be exported as the VERSION environment variable for the Docker Compose process.

  • -l, --latest: Indicate to use the latest Docker image version. This sets the VERSION environment variable to "latest".

  • --push: Enable the script to push the Docker image to a remote repository upon successful build.

  • -n, --name CONTAINER_NAME: Specify a custom name for the Docker container to be created.

  • --network DOCKER_NETWORK_NAME: Specify the Docker network name to use or create. If the network doesn't exist, the script will create it.

  • -u, --username USERNAME: Specify the username for the Docker image. If not provided, the script will default to the username from the configuration.

  • -h, --help: Display the help message containing usage information for the script.

Examples

  1. Build a Docker project named my_project with tag dev:
dk-compose build my_project -t dev
  1. Run a one-off command (some_command) on the my_service service in the my_project Docker project:
dk-compose run my_project --name my_service some_command
  1. Start the containers defined in the my_project Docker project with tag prod:
dk-compose up my_project -t prod
  1. Stop and remove the containers, networks, and volumes defined in the my_project Docker project with tag prod:
dk-compose down my_project -t prod

Remember that the dk-compose script should be executable and located in a directory included in the PATH variable (e.g., .local/bin).