Skip to content

Commit 54e1bc7

Browse files
committed
Add dockerfile commands
1 parent 4718f08 commit 54e1bc7

File tree

1 file changed

+184
-1
lines changed

1 file changed

+184
-1
lines changed

README.md

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,24 +410,207 @@ Example:
410410
FROM ubuntu:18.04
411411
```
412412

413-
414413
#### RUN
415414

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+
416427
#### 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
452+
453+
Override entrypoint:
454+
docker run --entrypoint "/bin/echo" entrypointImage "Override ENTRYPOINT instruction" // Override ENTRYPOINT instruction
455+
```
417456

418457
#### CMD
458+
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.
419479

420480
#### COPY
481+
The COPY instruction copies new files or directories from source and adds them to the destination filesystem of the container.
482+
483+
```cmd
484+
COPY [--chown=<user>:<group>] <src>... <dest>
485+
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]
486+
487+
Example:
488+
COPY test.txt /absoluteDir/
489+
COPY tes? /absoluteDir/ // Copies all files or directories starting with test to destination container
490+
```
421491

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`.
422493
#### 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,
495+
496+
```cmd
497+
ADD [--chown=<user>:<group>] <src>... <dest>
498+
ADD [--chown=<user>:<group>] ["<src>",... "<dest>"]
499+
500+
Example:
501+
ADD test.txt /absoluteDir/
502+
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+
```
423514

424515
#### 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+
```
425530

426531
#### 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+
```
427550

428551
#### 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.
566+
567+
```cmd
568+
LABEL <key>=<value> <key>=<value> <key>=<value> ...
569+
570+
Example:
571+
LABEL version="1.0"
572+
LABEL multi.label1="value1" \
573+
multi.label2="value2" \
574+
other="value3"
575+
```
576+
577+
You can view an image’s labels using the `docker image inspect --format='' myimage` command. The output would be as below,
578+
579+
```js
580+
{
581+
"version": "1.0",
582+
"multi.label1": "value1",
583+
"multi.label2": "value2",
584+
"other": "value3"
585+
}
586+
```
429587

430588
#### MAINTAINER
589+
The MAINTAINER instruction sets the Author field of the generated images.
590+
```cmd
591+
MAINTAINER <name>
592+
593+
Example:
594+
MAINTAINER John
595+
```
596+
597+
This command is deprecated status now and the recommended usage is with LABEL command
598+
```cmd
599+
LABEL maintainer="John"
600+
```
601+
602+
#### VOLUME
603+
The VOLUME instruction creates a mount point with the specified name and mounted volumes from native host or other containers.
604+
605+
```cmd
606+
VOLUME ["/data"]
607+
608+
Example:
609+
FROM ubuntu
610+
RUN mkdir /test
611+
VOLUME /test
612+
```
613+
431614
### Docker Compose
432615
Docker compose(or compose) is a tool for defining and running multi-container Docker applications.
433616
### Docker Swarm

0 commit comments

Comments
 (0)