Docker for DevOps engineer 1 minute read

Hai semuanya, di materi kali ini kita akan membahas tentang Copying Resource ke dalam image yang akan kita build. diantaranya menggunakan

  1. COPY Instruction
  2. ADD Instruction
  3. Cleanup

Copies new files/directory into image, kita bisa menggunakan perintah ADD dan COPY.

The ADD instruction copies new files, directories or remote file URLs from and adds them to the filesystem of the image at the path . The `COPY` instruction copies new files or directories from and adds them to the filesystem of the container at the path .

To add all files/directory from root context

COPY . /target

To copy all files ending with .jar

COPY *.jar /target

To copy files, ? is replaced with any single character, e.g., “home.txt”, “halo.txt”, “heyy.txt”, etc.

COPY h???.txt /target

To download files from source type url:

ADD https://github.com/BootcampYoutubeChannel/belajar-docker/archive/refs/tags/07b-copy-resource.tar.gz /target

To Extract file from source type zip, tar.gz etc:

ADD belajar-docker-07b-copy-resource.tar.gz /target

For Example

Download file belajar-docker-07b-copy-resource.tar.gz kemudian simpan dalam folder yang sama dengan file Dockerfile seperti berikut:

Jika di jalankan maka hasilnya seperti berikut:

 07-dockerfile   docker build -t dimmaryanto93/centos:0.3 .       
[+] Building 0.1s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                                   0.0s 
 => => transferring dockerfile: 168B                                                                                   0.0s 
 => [internal] load .dockerignore                                                                                      0.0s 
 => => transferring context: 2B                                                                                        0.0s 
 => [internal] load metadata for docker.io/library/centos:7                                                            0.0s 
 => [internal] load build context                                                                                      0.0s 
 => => transferring context: 61B                                                                                       0.0s 
 => [1/2] FROM docker.io/library/centos:7                                                                              0.0s 
 => CACHED [2/2] ADD *.tar.gz /usr/share/nginx/html                                                                    0.0s 
 => exporting to image                                                                                                 0.0s 
 => => exporting layers                                                                                                0.0s 
 => => writing image sha256:e4e14fbbd62885b0c6a18b6a444121f54884a90e21c2a00816e51d419e6dca79                           0.0s 
 => => naming to docker.io/dimmaryanto93/centos:0.3

  07-dockerfile   docker run --rm -it dimmaryanto93/centos:0.3     
8.0K    /usr/share/nginx/html/belajar-docker-07b-copy-resource/html
12K     /usr/share/nginx/html/belajar-docker-07b-copy-resource/conf
24K     /usr/share/nginx/html/belajar-docker-07b-copy-resource
28K     /usr/share/nginx/html/
28K     total

Cleaup

Seperti biasa, setelah kita mencoba praktikan kita bersih-bersih dulu ya. berikut perintahnya: