You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+184-1Lines changed: 184 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -410,24 +410,207 @@ Example:
410
410
FROM ubuntu:18.04
411
411
```
412
412
413
-
414
413
#### RUN
415
414
415
+
RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. It is quite common to have multiple RUN instructions in a Dockerfile.
416
+
417
+
It has two forms
418
+
1. Shell Form: RUN <command>
419
+
```cmd
420
+
RUN npm start
421
+
```
422
+
2. Exec form RUN ["<executable>", "<param1>", "<param2>"]
423
+
```cmd
424
+
RUN [ "npm", "start" ]
425
+
```
426
+
416
427
#### ENTRYPOINT
428
+
An ENTRYPOINT allows you to configure a container that will run as an executable. It is used to run when container starts.
429
+
430
+
```cmd
431
+
Exec Form:
432
+
ENTRYPOINT ["executable", "param1", "param2"]
433
+
Shell Form:
434
+
ENTRYPOINT command param1 param2
435
+
436
+
Example:
437
+
FROM alpine:3.5
438
+
ENTRYPOINT ["/bin/echo", "Print ENTRYPOINT instruction of Exec Form"]
439
+
440
+
```
441
+
442
+
If an image has an ENTRYPOINT and pass an argument to it while running the container, it wont override the existing entrypoint but it just appends what you passed with the entrypoint. To override the existing ENTRYPOINT. you should user `–entrypoint` flag for the running container.
443
+
444
+
Let's see the behavior with the above dockerfile,
445
+
446
+
```cmd
447
+
Build image:
448
+
docker build -t entrypointImage .
449
+
450
+
Run the image:
451
+
docker container run entrypointImage // Print ENTRYPOINT instruction of Exec Form
CMD instruction is used to set a default command, which will be executed only when you run a container without specifying a command. But if the docker container runs with a command, the default command will be ignored.
459
+
460
+
The CMD instruction has three forms,
461
+
```cmd
462
+
1. Exec form:
463
+
CMD ["executable","param1","param2"]
464
+
2. Default params to ENTRYPOINT:
465
+
CMD ["param1","param2"]
466
+
3. Shell form:
467
+
CMD command param1 param2
468
+
```
469
+
470
+
The main purpose of the CMD command is to launch the required software in a container. For example, running an executable .exe file or a Bash terminal as soon as the container starts.
471
+
472
+
Remember, if docker runs with executable and parameters then CMD instruction will be overridden(Unlike ENTRYPOINT).
473
+
474
+
```cmd
475
+
docker run executable parameters
476
+
```
477
+
478
+
**Note:** There should only be one CMD command in your Dockerfile. Otherwise only the last instance of CMD will be executed.
419
479
420
480
#### COPY
481
+
The COPY instruction copies new files or directories from source and adds them to the destination filesystem of the container.
COPY tes? /absoluteDir/ // Copies all files or directories starting with test to destination container
490
+
```
421
491
492
+
The <src> path must be relative to the source directory that is being built. Whereas <dest> is an absolute path, or a path relative to `WORKDIR`.
422
493
#### ADD
494
+
The ADD instruction copies new files, directories or remote file URLs from source and adds them to the filesystem of the image at the destination path. The functionality is similar to COPY command and supports two forms of usage,
ADD tes? /absoluteDir/ // Copies all files or directories starting with test to destination container
503
+
```
504
+
505
+
ADD commands provides additional features such as downloading remote resources, extracting TAR files etc.
506
+
507
+
```cmd
508
+
1. Download an external file and copy to the destination
509
+
ADD http://source.file/url /destination/path
510
+
511
+
2. Copies compressed files and extract the content in the destination
512
+
ADD source.file.tar.gz /temp
513
+
```
423
514
424
515
#### ENV
516
+
The ENV instruction sets the environment variable <key> to the value <value>. It has two forms,
517
+
518
+
1. The first form, `ENV <key> <value>`, will set a single variable to a value.
519
+
2. The second form, `ENV <key>=<value> ...`, allows for multiple variables to be set at one time.
520
+
521
+
```cmd
522
+
ENV <key> <value>
523
+
ENV <key>=<value> [<key>=<value> ...]
524
+
525
+
Example:
526
+
ENV name="John Doe" age=40
527
+
ENV name John Doe
528
+
ENV age 40
529
+
```
425
530
426
531
#### EXPOSE
532
+
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. i.e, It helps in inter-container communication. You can specify whether the port listens on TCP or UDP, and the default is TCP.
533
+
534
+
```cmd
535
+
EXPOSE <port> [<port>/<protocol>...]
536
+
537
+
Example:
538
+
EXPOSE 80/udp
539
+
EXPOSE 80/tcp
540
+
```
541
+
542
+
But if you want to bind the port of the container with the host machine on which the container is running, use -p option of `docker run` command.
543
+
544
+
```cmd
545
+
docker run -p <HOST_PORT>:<CONTAINER:PORT> IMAGE_NAME
546
+
547
+
Example:
548
+
docker run -p 80:80/udp myDocker
549
+
```
427
550
428
551
#### WORKDIR
552
+
The WORKDIR command is used to define the working directory of a Docker container at any given time for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
553
+
554
+
```cmd
555
+
WORKDIR /path/to/workdir
556
+
557
+
Example:
558
+
WORKDIR /c
559
+
WORKDIR d
560
+
WORKDIR e
561
+
RUN pwd // /c/d/e
562
+
```
563
+
564
+
#### LABEL
565
+
The LABEL instruction adds metadata as key-value pairs to an image. Labels included in base or parent images (images in the FROM line) are inherited by your image.
0 commit comments