- person Penulis:
-
Dimas Maryanto
PT. Tabeldata Informatika
- account_balance_wallet Donasi via:
- Saweria a/n Dimas Maryanto
- lock_open Join Premium Members:
- Udemy.com
-
Daftar Materi
-
1. Pengenalan Docker 8
-
2. Docker Registry 3
-
3. Docker Container CLI 8
-
1. Docker CLI (Command Line Interface)
2. Management Docker Container
3. Management Docker Images
4. Run a command in a running container
5. Expose services to outside using ports
6. Copying files/content between container and filesystem
7. Logging, Inspect, & Resource Usage Statistics Containers
8. Run a Container using Environtment File
-
4. Docker Networks 7
-
5. Docker Volumes 5
-
6. Dockerfile 15
-
1. Build Docker Image Overview
2. Usage docker build
3. FROM Instruction
4. Environtment Replacement
5. Copying Resources
6. Excluding files/directories
7. Label Instruction
8. Execution Instruction
9. CMD vs ENTRYPOINT?
10. Exposing Ports
11. User, Volumes and Working Directory
12. Health Check Instruction
13. Multiple Stage Builds
14. Best practices for writing Dockerfiles
15. Best practices for scanning images
-
7. Study Kasus: Build docker image 14
-
1. Build specific docker image by programming languages
2. Build Docker Image for Java Webapp
3. Build Java Web using maven-docker-plugin
4. Build docker image for spring-boot
5. Springboot - using Environtment
6. Springboot - where data such as files/images we stored?
7. Springboot - Using Database
8. Build docker image for Angular Project
9. Angular - Access Rest API
10. Angular - Proxy to backend
11. Build docker image for PHP
12. Build Docker image for Laravel Framework
13. Laravel - Using Frontend & Rest API
14. Laravel - Using Database
-
8. Docker Compose 19
-
1. Overview of Docker Compose
2. Get started with Docker Compose
3. Overview of docker-compose CLI
4. Compose file specification and syntax
5. Environment variables in Compose
6. Volume in Compose
7. Share data between Containers in Compose
8. Using sshfs for share data in Compose
9. Using NFS for share data in Compose
10. Networking Overview in Compose file
11. Network links in Compose file
12. Specify custom networks in Compose file
13. Dependency between services in Compose file
14. Build docker image using Compose file
15. Using profiles with Compose file
16. Multiple Compose files to Add & Override attribute
17. Example use case of multiple compose files
18. Scale services using compose command
19. Use Compose in production
-
9. Study Kasus: Docker Compose 7
-
10. Docker Context 8
-
11. Study Kasus: Docker for CI 8
-
1. Overview of Study Cases using docker for CI
2. Setup environment for CI using Gitlab & Nexus OSS
3. The `.gitlab-ci.yml` file
4. Pipeline: PHP deployment using Gitlab CI
5. Pipeline: Java Web deployment using Gitlab CI
6. Pipeline: spring-boot deploy with Gitlab CI
7. Pipeline: Angular deploy with Gitlab CI
8. Pipeline: Laravel deploy with Gitlab CI
-
12. Docker Machine 7
-
13. Study Kasus: Ansible for Docker 4
-
14. Docker Swarm
- Materi: belum tersedia...
-
15. Study Kasus: Docker Swarm
- Materi: belum tersedia...
-
16. Docker on Cloud using GCP
- Materi: belum tersedia...
- Lastest Posts
- 09 Apr 23 Working with Deployment object
- 26 Feb 23 Study cases: Microservice apps (...
- 05 Feb 23 Welcome to the Nutanix HCF (Hybr...
- 04 Feb 23 Silabus SRE - Nutanix AHV: Pemul...
- 17 Jan 23 What is Workload Resources?
- 17 Jan 23 Overview Kubernetes Workloads re...
- 15 Jan 23 Getting started with Transaction...
- 14 Jan 23 Overview of Concurrency Control
- 14 Jan 23 Time your practice (part 3)
- 08 Jan 23 Cleanup Data from Table
Environment variables in Compose
Hai semuanya di materi kali ini kita akan membahas Environment variable khususnya dalam Compose file, diantaranya
- Set environtment variable in container
- The
.env
file - Operation System environment variables
- Using the
--env-file
option - Pass environment variables to containers
- The
env_file
configuration option - Set environment variables with
docker-compose run
Ok langsung ja, kita ke pembahasan yang pertama yaitu
Set environtment variable in container
You can set environment variables in a service’s containers with the environment
key, just like with docker run -e VARIABLE=VALUE ...
:
The .env
file
You can set default values for any environment variables referenced in the Compose file, or used to configure Compose, in an environment file named .env
. The .env
file path is as follows:
- Starting with
+v1.28
,.env
file is placed at the base of the project directory - Project directory can be explicitly defined with the
--file
option orCOMPOSE_FILE
environment variable. Otherwise, it is the current working directory where the docker compose command is executed (+1.28
). - For previous versions, it might have trouble resolving
.env
file with--file
orCOMPOSE_FILE
. To work around it, it is recommended to use--project-directory
, which overrides the path for the.env
file. This inconsistency is addressed in+v1.28
by limiting the filepath to the project directory.
Contohnya kita buat file .env
seperti berikut:
Dan berikut adalah file docker-compose.yaml
seperti berikut:
When you run docker-compose up
, the web
service defined above uses the image nginx:1.12.1
. You can verify this with the config command, which prints your resolved application config to the terminal:
➜ env-file docker-compose config
services:
web:
image: nginx:1.21.1
ports:
- published: 80
target: 80
version: '3.8'
Operation system environment variables
Selain menggunakan .env
file, compose file juga listen dari OS Environtment variable, sebagai contoh kita masih menggunakan docker-compose.yaml
sebelumnya yaitu:
Sekarang coba set env variable di OS dengan perintah seperti berikut:
For Bash script:
For Powershell script:
Sekarang coba check dengan perintah seperti berikut docker-compose config
, maka hasilnya seperti berikut:
## perintah berikut akan meng-override file `.env` file karena levelnya lebih tinggi
➜ env-file $Env:NGINX_VERSION="latest"
➜ env-file docker-compose config
services:
web:
image: nginx:latest
ports:
- published: 80
target: 80
version: '3.8'
# To remove Env NGINX_VERSION from Session
➜ env-file Remove-Item Env:NGINX_VERSION
here’s the priority used by Compose to choose which value to use:
- Compose file
- Shell environment variables
- Environment file
- Dockerfile
- Variable is not defined
Using the --env-file
option
By passing the file as an argument, you can store it anywhere and name it appropriately, for example, .env.ci
, .env.dev
, .env.prod
. Passing the file path is done using the --env-file
option:
Jika di jalankan maka hasilnya seperti berikut:
## create copy file of `.env` to `.env.dev`
➜ env-file cp .env .env.dev
# the edit value in `.env.dev` file
➜ env-file sed -i 's|1.21.1|mainline|g' .env.dev
➜ env-file cat .env.dev
NGINX_VERSION=mainline
➜ env-file docker-compose --env-file .env.dev config
services:
web:
image: nginx:mainline
ports:
- published: 80
target: 80
version: '3.8'
Pass environment variables to containers
You can pass environment variables from your shell straight through to a service’s containers with the ‘environment’ key by not giving them a value, just like with docker run -e VARIABLE ...
:
Buat file baru dengan nama .env
seperti berikut:
Dan berikut adalah file docker-compose.yaml
seperti berikut:
Jika kita coba validate maka hasilnya seperti berikut:
➜ pass-env cat .env
POSTGRES_PASSWORD=test_db
DB_USERNAME=test_db
POSTGRES_DB=test_db
➜ pass-env cat docker-compose.yaml
version: '3.8'
services:
db:
image: postgres:12.6
environment:
- POSTGRES_PASSWORD
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_DB
ports:
- 5432:5432
➜ pass-env docker-compose --env-file .env config
services:
db:
environment:
POSTGRES_DB: test_db
POSTGRES_PASSWORD: test_db
POSTGRES_USER: test_db
image: postgres:12.6
ports:
- published: 5432
target: 5432
version: '3.8'
The value of the POSTGRES_DB
variable in the container is taken from the value for the same variable in the shell in which Compose is run.
The env_file
configuration option
You can pass multiple environment variables from an external file through to a service’s containers with the env_file
option, just like with docker run --env-file=FILE ...
:
Buat file baru dengan nama database.dev.env
seperti berikut:
Dan berikut adalah file docker-compose.yaml
seperti berikut:
Jika kita coba validate maka hasilnya seperti berikut:
➜ env-file-option docker-compose config
services:
db:
environment:
POSTGRES_DB: hr_db
POSTGRES_PASSWORD: dev_db
POSTGRES_USER: dev_db
image: postgres:12.6
ports:
- published: 5432
target: 5432
version: '3.8'
Set environment variables with docker-compose run
Similar to docker run -e
, you can set environment variables on a one-off container with docker-compose run -e
:
Berikut adalah docker-compose.yaml
template:
Kemudian coba jalan perintah berikut
Jika dijalankan maka hasilnya seperti berikut:
➜ docker-compose-run docker-compose -f .\docker-compose.yaml run `
>> -e POSTGRES_DB=hr_db `
>> -e POSTGRES_USER=prod_db `
>> -d db
Creating docker-compose-run_db_run ... done
docker-compose-run_db_run_bef93b57be79
➜ docker-compose-run docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------
docker-compose-run_db_run_bef93b57be79 docker-entrypoint.sh postgres Up 5432/tcp
➜ docker-compose-run docker container inspect docker-compose-run_db_run_bef93b57be79 -f '{{json .Config.Env}}' | python.exe -m json.tool
[
"POSTGRES_PASSWORD=hr_db",
"POSTGRES_DB=hr_db",
"POSTGRES_USER=prod_db",
"PGDATA=/var/lib/postgresql/data"
]
Yuk simak juga videonya,
Dan jika temen-temen belajar hal baru kali ini jangan lupa buat Like, Subcribe, dan Share ke temen kalian. Terimakasih!!!