Enhancing Skills

Docker commands: I use as BASH with Variables

As an experienced Docker user, I’ve written scripts to automate various tasks related to managing and using containers on the command line. You can use these scripts to start, stop, restart, log errors and tail logs for your containers with ease. Additionally, you can use attach.sh to attach to a running container’s terminal and interact with it in real-time. With Docker, you can quickly and easily manage multiple containers on the same host, allowing you to run different applications and services on the same machine without worrying about remembering port or overlapping dependencies. These scripts are based on my work for Docker Assistant on github.

env.sh

This code is creating an environment file for a Docker container named “catapp”. The file sets three variables: Variable1, Variable2, and Variable3, each with a value. The ContainerName variable specifies the name of the container that this environment file will be used with.

The code is written in a Bash-like syntax, with comments starting with #. Comments are essentual to working with Bash and Docker.

In Bash, you can access the values of these variables using the ${VARIABLE_NAME} syntax. For example, if you want to access the value of Variable1, you can use ${Variable1}. More to come on this.

# Created by Quesenbery, D
# ToolbaxAid.com
#
# Date Thu Nov 17 13:56:23 EST 2022
#
Variable1=value1
Variable2=value2
Variable3=value3
ContainerName=catapp

This is a Bash script called env.sh that performs the following actions:

It outputs the values of three environment variables named Variable1, Variable2, and Variable3. These variables are set to specific values in the .env file.

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., env.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# env.sh#

clear

BASE_EXE=`basename "$0"`
echo ">>>>> $BASE_EXE <<<<<"

#load_env
source ./.env

echo " Variable1 : $Variable1"
echo " Variable2 : $Variable2"
echo " Variable3 : $Variable3"

up.sh

This is a Bash script called up.sh that performs the following actions:

It outputs another line of dashes (-----------------------------------------) to separate the output from the next command.

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., up.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

It outputs a line of dashes (-----------------------------------------) to separate the output from the next command.

It runs the docker-compose up -d command, which starts the Docker containers specified in a docker-compose.yml file in the current directory (the -d flag means “detached” and runs the container in the background).

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# up.sh
#
clear
BASE_EXE=`basename "$0"`
echo ">>>>> Executing: $BASE_EXE <<<<<"

#load_env
source ./.env

echo "-----------------------------------------"
docker-compose up -d
echo "-----------------------------------------"

down.sh

This is a Bash script called down.sh that performs the following actions:

It outputs another line of dashes (-----------------------------------------) to separate the output from the next command

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., down.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

It outputs a line of dashes (-----------------------------------------) to separate the output from the next command.

It runs the docker-compose down command, which stops and removes the Docker containers specified in a docker-compose.yml file in the current directory (the -d flag means “detached” and runs the container in the background).

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# down.sh
#
clear
BASE_EXE=`basename "$0"`
echo ">>>>> Executing: $BASE_EXE <<<<<"

#load_env
source ./.env

echo "-----------------------------------------"
docker-compose down
echo "-----------------------------------------"

log.error.sh

This is a Bash script called log.error.sh that performs the following actions:

It outputs another line of dashes (-----------------------------------------) to separate the output from the next command.

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., log.error.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

It outputs a line of dashes (-----------------------------------------) to separate the output from the next command.

It runs the docker logs -f ${ContainerName} 2>/dev/null command, which streams the logs for the container with the name specified in the ${ContainerName} variable (the -f flag means “follow” and keeps the command running and streaming the logs).

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# log.error.sh
#
clear
BASE_EXE=`basename "$0"`
echo ">>>>> Executing: $BASE_EXE <<<<<"

#load_env
source ./.env

echo "-----------------------------------------"
echo "docker logs -f ${ContainerName} 2>/dev/null"
docker logs -f ${ContainerName} 2>/dev/null
echo "-----------------------------------------"

log.tail.sh

This is a Bash script called log.tail.sh that performs the following actions:

It outputs another line of dashes (-----------------------------------------) to separate the output from the next command

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., log.tail.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

It outputs a line of dashes (-----------------------------------------) to separate the output from the next command.

It runs the docker-compose logs -f command, which streams the logs for all containers specified in a docker-compose.yml file in the current directory (the -f flag means “follow” and keeps the command running and streaming the logs).

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# log.tail.sh
#
clear
BASE_EXE=`basename "$0"`
echo ">>>>> Executing: $BASE_EXE <<<<<"

#load_env
source ./.env

echo "-----------------------------------------"
docker-compose logs -f
echo "-----------------------------------------"

restart.sh

This is a Bash script called restart.sh that performs the following actions:

It outputs another line of dashes (-----------------------------------------) to separate the output from the next command

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., restart.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

It outputs a line of dashes (-----------------------------------------) to separate the output from the next command.

It runs the docker-compose down command, which stops and removes all containers specified in a docker-compose.yml file in the current directory.

It runs the docker-compose up -d command, which starts all services defined in a docker-compose.yml file in the current directory in detached mode (i.e., in the background).

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# restart.sh
#
clear
BASE_EXE=`basename "$0"`
echo ">>>>> Executing: $BASE_EXE <<<<<"

#load_env
source ./.env

echo "-----------------------------------------"
docker-compose down
docker-compose up -d
echo "-----------------------------------------"

attach.sh

This is a Bash script called attach.sh that performs the following actions:

It outputs another line of dashes (-----------------------------------------) to separate the output from the next command.

It clears the terminal screen using the clear command.

It sets the variable BASE_EXE to the basename of the current script file (i.e., the file name without the path or any extensions).

It outputs a message to the console indicating that it is running and the name of the script file being used (i.e., attach.sh).

It loads the environment variables from a .env file in the current directory using the source ./.env command.

It outputs a line of dashes (-----------------------------------------) to separate the output from the next command.

It runs the docker exec -it ${ContainerName} sh command, which attaches to the container with the name specified in the ${ContainerName} variable and allows you to interact with it as if you were logged into it (the -i flag means “interactive” and the -t flag means “tty”).

#!/bin/bash
#
# Created by Quesenbery, D
# ToolbaxAid.com
# Date  10/20/2022
#
# attach.sh
#
clear
BASE_EXE=`basename "$0"`
echo ">>>>> Executing: $BASE_EXE <<<<<"

#load_env
source ./.env

echo "-----------------------------------------"
echo "docker exec -it ${ContainerName} sh"
docker exec -it ${ContainerName} sh
echo "-----------------------------------------"

The value of using environment variables for docker

Using an .env file for storing environment variables is a common practice in development and production environments. It allows you to keep sensitive information, such as database credentials or API keys, separate from your code and version control system, making it easier to manage and secure your project.

Here are some benefits of using an .env file:

  1. Environment variables are stored separately from the rest of your code, making it easier to keep sensitive information out of your version control system.
  2. You can use different environment variables for development and production environments, without having to duplicate your code or make significant changes to your project.
  3. Using an .env file makes it easier to manage and scale your project, as you can simply update the values in the file to change the behavior of your application.
  4. It allows you to keep different environment variables for different environments, such as development and production, without having to duplicate your code or make significant changes to your project.
  5. Using an .env file makes it easier to manage and scale your project, as you can simply update the values in the file to change the behavior of your application.
  6. It allows you to keep different environment variables for different environments, such as development and production, without having to duplicate your code or make significant changes to your project.
  7. Using an .env file makes it easier to manage and scale your project, as you can simply update the values in the file to change the behavior of your application.
  8. It allows you to keep different environment variables for different environments, such as development and production, without having to duplicate your code or make significant changes to your project.
  9. Using an .env file makes it easier to manage and scale your project, as you can simply update the values in the file to change the behavior of your application.
  10. It allows you to keep different environment variables for different environments, such as development and production, without having to duplicate your code or make significant changes to your project.

Here is an example of how you might create a .env file for the given docker-compose.yml file:

# .env file
container_name=myapp
image=myimage
database_url=https://example.com
database_port=5432

This .env file defines four environment variables: container_name, image, database_url, and database_port. The values of these variables are specified in the file, and can be used in your docker-compose.yml file to override the default values for each service.

Here is an example of how you might use this .env file with your given docker-compose.yml file:

version: '3'
services:
  myapp:
    container_name: ${container_name}
    image: ${image}
    environment:
      - database_url=${database_url}
      - database_port=${database_port}

In this example, the docker-compose.yml file defines a service named myapp, and specifies that it should use an image named myimage. The environment section of the myapp service specifies two environment variables: database_url and database_port. These variables are defined in the .env file, and can be used to override the default values for these variables.

Note that you can use this .env file with other commands that use the --env-file option, such as docker stack deploy or docker service create. This allows you to keep your environment variables separate from your code and version control system, making it easier to manage and scale your project.

When you run the containers using the docker-compose file, you can use the --env-file option to load the environment variables from the .env file into each container. For example:

docker-compose up -d --env-file .env

This will start the containers defined in the docker-compose file and load the environment variables from the .env file into each container. The values of these variables can then be used by the application running inside the container to connect to the database or other services


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.