フッターコンテンツにスキップ
IRONSECUREDOCを使用する

Docker Compose環境変数(開発者向けチュートリアル)

Deploying applications across different environments often leads to compatibility headaches. An app might run perfectly on a developer's machine but encounter issues in testing or production because of differences in operating systems, dependencies, or configurations. Docker solves this problem by packaging applications into self-contained units that include everything needed to run the app, ensuring it works consistently everywhere, from development to production.

Docker Compose brings powerful benefits to containerized applications, especially with its use of environment variables to simplify configuration across different setups. By allowing key settings to be managed outside the code, Docker Compose makes deploying complex applications more straightforward and secure. Building on these benefits, this article will also show how you can use IronSecureDoc for secure document processing, tapping into Docker Compose’s flexibility to handle configurations for encryption and redaction efficiently.

What is Docker?

Docker is an open-source system to ease application development, deployment, and running in a system through containerization. Containers are ultra-lightweight portable units that contain both an application and its dependencies so that they run uniformly everywhere—from the developer's machine to production servers. Containers share the kernel of the host's operating system compared to traditional virtual machines and, therefore, are much faster and more efficient.

Docker Compose Environment Variables (Developer Tutorial): Figure 1 - Docker webpage

This would mean a virtualized template in the form of Docker images to make and maintain this set of containers. Furthermore, the platform contains Docker Hub, which is in a way a container image repository. The beauty of this technology lies in its ease of use concerning scalability, portability, and efficiency, which is one reason it has gained such popularity among many DevOps and cloud-based development workflows.

What are Environment Variables in Docker-Compose?

In Docker, an environment variable in the form of a key-value pair is used to configure settings in the containerized application. Such variables can be useful to control the behavior of the application itself without modifying its code, since easily, configurations such as database credentials and API keys or environment modes (for example: development, production) may be changed.

In Docker, an environment attribute can be defined inside the Dockerfile, inside the docker-compose.yml file, or passed at runtime using the docker run command. With the use of environment variables, Docker allows consistent and flexible application deployment across different environments and manages sensitive data like passwords and API tokens more effectively.

How to Create Environment Variables in Docker?

We can define environment variables in Docker in quite a few ways. They can be defined with a Docker Compose file, the docker-compose.yml file, in an environment file, or even at runtime, as you are executing the docker run command. Remember, keeping environment variables separate from the main configuration file leads to easier organization of variables! Here is a list of the various methods that can be applied to define your variables.

Set Environment Variables in the Dockerfile

We can define environment variables directly in the Dockerfile, using the ENV instruction. This can be helpful if you want to include default values for the variables within your Docker image.

# Dockerfile
# Set the application environment
ENV APP_ENV=development
# Set the database URL
ENV DATABASE_URL=postgres://user:password@db:1234/mydev

The setting of environment variables with the values defined in the Dockerfile will automatically apply at the runtime of the container.

Set Environment Variables in Docker-Compose.yml

We can define environment variables for each service inside a docker-compose.yml with the help of the environment keyword. This is handy when you are using Docker Compose to manage a couple of services.

version: '3.8'
services:
  myapp:
    image: myapp:latest
    environment:
      - APP_ENV=development
      - DATABASE_URL=postgres://user:password@db:1234/mydev
version: '3.8'
services:
  myapp:
    image: myapp:latest
    environment:
      - APP_ENV=development
      - DATABASE_URL=postgres://user:password@db:1234/mydev
YAML

Set Environment Variables at Runtime

We can specify environment variables when running a container by using the -e flag along with the docker run command. This is good for transient and dynamic values, which you probably wouldn't add to the Dockerfile.

docker run -e APP_ENV=development -e DATABASE_URL=postgres://user:password@db:1234/mydev myapp:latest
docker run -e APP_ENV=development -e DATABASE_URL=postgres://user:password@db:1234/mydev myapp:latest
SHELL

Using Environment Files (.env)

We can store environment variables in a file like .env and load them into your Docker containers. In Docker Compose, we will refer to it with the env_file directive.

# .env file
APP_ENV=production
DATABASE_URL=postgres://user:password@db:1234/mydev
# .env file
APP_ENV=production
DATABASE_URL=postgres://user:password@db:1234/mydev
SHELL

We can manage multiple files, with environment variables outside the configuration files through the help of env files.

What is IronSecureDoc?

IronSecureDoc for Docker allows developers to easily add a secure document processing capability to their containerized applications. Having learned Docker, you can encapsulate your ASP.NET Core application with IronSecureDoc in a homogeneous environment that makes it easier to deploy and scale. To do this, you'll build a Dockerfile that orchestrates the construction of your ASP.NET Core application using the IronSecureDoc library and possibly other installation scripts or configurations needed to get things working.

Docker Compose Environment Variables (Developer Tutorial): Figure 2 - IronSecureDoc webpage

Besides, it includes a docker-compose.yml file declaring the service dependencies and environment variables and mapped ports to get on. This makes performing document security tasks much more accessible so your application can run efficiently and effectively in an environment other than the one used during development or production. Installation and Configuration of IronSecureDoc Like in the case of Docker, proper installation and configuration of IronSecureDoc will be needed to properly realize its capabilities: document encryption, redaction, etc.

Key Features of IronSecureDoc

IronSecureDoc offers a range of powerful features for PDF security and document management:

  • Encryption: Provides 128- or 256-bit encryption with password-based security to protect document confidentiality.
  • Redaction: Removes sensitive information, such as personal identifiers, to meet privacy standards and regulations.
  • Digital Signing: Supports digital signing and notarization with .pfx or .p12 certificates to ensure document authenticity.
  • REST API: Flexible API enables seamless integration with other software and workflows.
  • Docker Integration: Native support for Docker simplifies deployment and scaling for cloud or on-premises applications.

These features make IronSecureDoc an excellent choice for sectors handling sensitive documents, such as legal, healthcare, and finance.

Installing and Running IronSecureDoc

Step 1

To install IronSecureDoc, run the following command in a terminal window or Command Prompt to get the IronSecureDoc Docker image from the repository.

docker pull ironsoftwareofficial/ironsecuredoc
docker pull ironsoftwareofficial/ironsecuredoc
SHELL

Docker Compose Environment Variables (Developer Tutorial): Figure 3 - Console output for getting the IronSecureDoc image

Step 2

Once the IronSecureDoc image has been pulled, we can use the following docker-compose command to run the image into the Docker container.

docker container run --rm -p 8080:8080 -e IronSecureDoc_LicenseKey=<IRONSECUREDOC_LICENSE_KEY> -e ENVIRONMENT=Development -e HTTP_PORTS=8080 ironsoftwareofficial/ironsecuredoc:latest
docker container run --rm -p 8080:8080 -e IronSecureDoc_LicenseKey=<IRONSECUREDOC_LICENSE_KEY> -e ENVIRONMENT=Development -e HTTP_PORTS=8080 ironsoftwareofficial/ironsecuredoc:latest
SHELL

Docker Compose Environment Variables (Developer Tutorial): Figure 4 - Console output from running the IronSecureDoc image

We use the docker container to run IronSoftware's official repository. The command line shown above breaks into several parts which are explained below.

Command Explanation

  • docker container run - This command uses the given image to construct and launch a new Docker container.
  • --rm - Automatically cleans up the container immediately once it stops running. It removes all the unused containers at the time of the completion of any process.
  • -p 8080:8080 - Publishes the container's port 8080, so you can access it on your machine from http://localhost:8080.
  • -e IronSecureDoc_LicenseKey=<IRONSECUREDOC_LICENSE_KEY> - Set an environment variable in the running container named IronSecureDoc_LicenseKey that lets you turn on and use licensed features of IronSecureDoc. Replace <IRONSECUREDOC_LICENSE_KEY> with your actual key.
  • -e ENVIRONMENT=Development - The environment variable is set to Development. This means that the container needs to be run in development mode. Normally, this container is used for the test or debug cases; other than that, it varies from nonproduction configurations.
  • -e HTTP_PORTS=8080 - This environment variable is used to specify that the container should expose and listen to port 8080 for HTTP traffic. It ensures access to the service inside the container by passing through this particular port.
  • ironsoftwareofficial/ironsecuredoc:latest - This is the Docker image. This specifies that the latest version of the image should be used from the Docker registry for IronSecureDoc.

IronSecuredoc Container

IronSecureDoc's REST API lets users redact, certify, and encrypt documents after it is launched in Docker. Here's a link to the API endpoints and documentation with Swagger UI, once you have launched IronSecureDoc in a Docker container: http://localhost:8080/swagger/index.html.

Docker Compose Environment Variables (Developer Tutorial): Figure 5 - Swagger UI for you to interact with API endpoints

From the above instance, we can send a POST request to the IronSecureDoc API to submit a document for encryption:

curl -X 'POST' \
  'http://localhost:8080/v1/document-services/pdfs/encrypt?user_password=demo' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'pdf_file=@test.pdf;type=application/pdf'
curl -X 'POST' \
  'http://localhost:8080/v1/document-services/pdfs/encrypt?user_password=demo' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'pdf_file=@test.pdf;type=application/pdf'
SHELL

By doing this, IronSecureDoc will receive the document and properly encrypt it.

Conclusion

In sum, Docker Compose's environment variables allow the configuration of applications in a very flexible and efficient way since the configuration details are automatically separated from the application code. Consequently, managing different environments such as development, testing, and production is simpler, as only variables such as API keys, database credentials, and settings of an application need to be changed instead of its code.

Using Docker Compose to implement IronSecureDoc utilizes environment variables for securely handling the licensing information, for instance, the IronSecureDoc_LicenseKey, and also in specifying the HTTP ports or the environment mode preferred as either development or production using environment variables. Using environment variables for setup makes it simpler to deploy IronSecureDoc with much less cluttered and hard-to-scale configurations and boosts security.

Advanced features become accessible through a valid license of IronSecureDoc. The application of the tool is strictly dependent on certain terms of use. You can also utilize other high-performance libraries that Iron Software offers to make the development process easier and faster, providing robust functionalities in working with PDFs, text recognition, and barcodes for any conceivable application.

Docker Compose Environment Variables (Developer Tutorial): Figure 6 - IronSecureDoc licensing page

よくある質問

Docker Composeがアプリケーションのデプロイをどのように改善しますか?

Docker Composeは、環境変数を使用して外部から構成を管理することで、アプリケーションのデプロイを改善します。これにより、アプリケーションコードを変更せずに異なる環境で簡単にスケーリングと適応が可能になります。

Dockerでは環境変数がどのような役割を果たしますか?

Dockerの環境変数は、コードから独立してアプリケーションの設定を構成する、キーと値のペアです。これにより、パスワードやAPIキーなどの機密情報を安全に管理できます。

IronSecureDocは、安全なドキュメント処理のためにDockerとどのように統合されますか?

IronSecureDocはDockerと統合し、Dockerコマンドを使用してダウンロードして実行できるDockerイメージを提供します。環境変数を使用して、暗号化、編集、デジタル署名などの安全なドキュメント処理機能を設定します。

Docker Composeで環境変数を使用する利点は何ですか?

Docker Composeで環境変数を使用することで、柔軟な構成管理が可能になり、機密データをアプリケーションコードの外に置くことでセキュリティが向上し、さまざまな環境でのデプロイプロセスが効率化されます。

Docker環境でIronSecureDocを実行する方法は?

Docker環境でIronSecureDocを実行するには、必要な環境変数を指定してdocker container run --rm -p 8080:8080 -e IronSecureDoc_LicenseKey= -e ENVIRONMENT=Development -e HTTP_PORTS=8080 ironsoftwareofficial/ironsecuredoc:latest というコマンドを使用します。

IronSecureDocが提供するドキュメントセキュリティの機能は何ですか?

IronSecureDocは、ドキュメントの暗号化、編集、デジタル署名、およびDockerとのシームレスな統合などの機能を提供し、コンテナ化された環境での安全なドキュメント処理を可能にします。

Docker Composeセットアップで環境変数を設定する方法は?

環境変数は、Docker Composeでdocker-compose.ymlファイル、環境ファイル、または実行時コマンドを使用して設定できます。これにより、アプリケーション構成を管理する柔軟性とセキュリティが提供されます。

Dockerデプロイにおいて、構成をコードから分離することが重要である理由は?

Dockerデプロイにおいて、環境変数を使用して構成をコードから分離することは、セキュリティを強化し、さまざまな環境での管理を簡素化し、機密情報の漏えいのリスクを低減します。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。