SlideShare a Scribd company logo
Docker in a JS Developer’s Life
Docker in a JS Developers’ Life
Valerii Radchenko
Senior Software Engineer
18.12.2017
3
Agenda
1. About Docker
2. Work with Dockerfiles
3. Docker CLI commands
4. Javascript and Docker practical tips
Conclusion
4
About Docker
5
What is Docker?
● Docker allows shipping your applications together with an
environment.
● Docker is not a virtual machine, it uses the resource isolation
features of the Linux kernel such as cgroups and kernel
namespaces.
● Docker provides a free public cloud-based registry.
6
When should I use Docker?
● Do you need to run multiple copies of a single application (e.g,
PostgreSQL, MySQL)?
If yes, choose Docker.
● Are you OK with using one OS version for your application?
If yes, choose Docker.
● Do you need to run as many applications as you can on a limited
hardware budget?
If yes, choose Docker.
● Do you need multiple (or obscure) OS’es?
If yes, go with a VM.
7
Images and Containers
8
Images
● An image is a read-only template with instructions for creating a
Docker container.
● Images based on other images.
Containers
● A container is a runnable instance of an image.
● A container is an isolated process.
● You can manage containers using Docker API or CLI and even
create a new image based on its current state.
9
Work with Dockerfiles
10
NodeJS simple express http server Dockerfile example
11
FROM instruction
FROM <image>[:<tag>] [AS <name>]
The FROM instruction initializes a new build stage and sets the Base
Image for subsequent instructions. As such, a valid Dockerfile must
start with a FROM instruction.
The image can be any valid image – it is especially easy to start by
pulling an image from the Public Repositories.
12
RUN instruction
RUN ["executable", "param1", "param2"]
RUN <command> && <command>
The RUN instruction will execute any commands in a new layer on
top of the current image and commit the results.
The resulting committed image will be used for the next step in the
Dockerfile.
13
ENTRYPOINT instruction
ENTRYPOINT command param1 param2
ENTRYPOINT ["executable", "param1", "param2"]
An ENTRYPOINT allows you to configure a container that will run as
an executable.
14
CMD instruction
CMD command param1 param2
CMD ["executable","param1","param2"]
The main purpose of a CMD is to provide defaults for an executing
container. These defaults can include an executable, or they can
omit the executable, in which case you must specify an
ENTRYPOINT instruction as well.
15
COPY instruction
COPY ["<src>",... "<dest>"]
COPY <src>... <dest>
The COPY instruction copies new files or directories from <src> and
adds them to the filesystem of the container at the path <dest>.
16
ADD instruction
ADD ["<src>",... "<dest>"]
ADD <src>... <dest>
The ADD instruction copies new files, directories or remote file URLs
from <src> and adds them to the filesystem of the image at the path
<dest>.
17
EXPOSE instruction
EXPOSE <port> [<port>/<protocol>...]
The EXPOSE instruction informs Docker that the container listens on
the specified network ports at runtime.
You can specify whether the port listens on TCP or UDP, and the
default is TCP if the protocol is not specified.
18
VOLUME instruction
VOLUME ["/data"]
VOLUME /data
It creates a mount point with the specified name and marks it as
holding externally mounted volumes from native host or other
containers.
The value can be a JSON array, VOLUME ["/var/log/"], or a plain
string with multiple arguments, such as VOLUME /var/log or
VOLUME /var/log /var/db.
More information/examples — Share Directories via Volumes
documentation.
19
WORKDIR instruction
WORKDIR /path/to/workdir
The WORKDIR instruction sets the working directory for any RUN,
CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the
Dockerfile.
If the WORKDIR doesn’t exist, it will be created even if it’s not used
in any subsequent Dockerfile instruction.
20
Docker CLI commands
21
Build command
● Docker build –t name[:tag] <context path>
- The docker build command builds Docker images from a Dockerfile and a
“context”.
- A build’s context is the set of files located in the specified PATH or URL.
● Flags
- --tag , -t Name and optionally a tag in the ‘name:tag’ format
- --file , -f Name of the Dockerfile (Default is ‘PATH/Dockerfile’)
22
Run command
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
● Docker runs processes in isolated containers.
● A container is a process which runs on a host.
● The host may be local or remote.
23
Run command FLAGS
Flags
- --name Container identification.
- -p=[] Publish a container᾿s port or a range of ports to the host.
- -v, --volumeBind mount a volume.
- --rm Automatically clean up the container and remove the file system
when the container exits.
- -it Keep STDIN open even if not attached and Allocate a pseudo-tty
- -d Detached mode (background mode).
24
ps command
docker ps [OPTIONS]
- Show containers list.
Flags
--all , -a Show all containers (default shows just running)
--quiet , -q Only display numeric IDs
25
Images command
docker images [OPTIONS] [REPOSITORY[:TAG]]
- Show images list.
Flags
--all , -a Show all images (default hides intermediate images)
--quiet , -q Only display numeric IDs
26
Exec command
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
-Run a command in a running container.
Flags
--detach , -d Detached mode: run command in the background.
--env , -e Set environment variables.
-it Keep STDIN open even if not attached and Allocate a pseudo-tty
27
Attach command
docker attach [OPTIONS] CONTAINER
- Attach local standard input, output, and error streams to a running
container.
28
Commit command
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- Create a new image from a container’s changes.
29
Start, Stop, Restart commands
docker (stop | start | restart) [OPTIONS] CONTAINER [CONTAINER...]
30
Login command
docker login [OPTIONS] [SERVER]
- Log in to a Docker registry.
31
Pull command
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- Pull an image or a repository from a registry.
32
Push command
docker push [OPTIONS] NAME[:TAG]
- Push an image or a repository to a registry.
33
Volume command
docker volume create [OPTIONS] [VOLUME]
- Create a volume.
docker volume ls [OPTIONS]
- Volumes list.
docker volume rm [OPTIONS] VOLUME [VOLUME...]
- Remove one or more volumes.
34
Network command
docker network create [OPTIONS] NETWORK
- Create a network.
docker network ls [OPTIONS]
- Networks list.
docker network rm NETWORK [NETWORK...]
- Remove one or more networks.
35
Javascript and Docker practical tips
36
Always use .dockerignore
• The current working directory where you are located when you issue a
docker build  command is called the build context.
• All of the recursive contents of files and directories in the current directory
are sent to the Docker daemon as the build context.
• Inadvertently including files that are not necessary for building the image
results in a larger build context and larger image size.
• These in turn can increase build time, time to pull and push the image, and
the runtime size of containers.
Reduce image size
• Make RUN instruction for every command
RUN apt-get update
RUN apt-get install curl
• Use full-size base images
FROM node:8
• Do not clean after build
• Install unnecessary applications
Wrong
• Combine commands in one RUN instruction
RUN apt-get update && 
apt-get install curl
• Use alpine images whenever you can,
otherwise use slim versions
FROM node:8-alpine
FROM node:8-slim
• Clean applications that you do not need in a
runtime
• Clean cache
• Use multi-stage build
Right
38
Multi-stage building
• Multi-stage builds are a new feature requiring Docker 17.05 or higher.
• Multi-stage builds are useful to anyone who has struggled to optimize
Dockerfiles while keeping them easy to read and maintain.
• With multi-stage builds, you use multiple FROM statements in your
Dockerfile.
• Each FROM instruction can use a different base, and each of them begins
a new stage of the build.
• You can selectively copy artifacts from one stage to another, leaving
behind everything you don’t want in the final
39
Install node modules before copy source code
COPY ./ $HOME/
RUN npm install && 
npm run build
Wrong
COPY ./package.json $HOME
RUN npm install
COPY ./ $HOME/
RUN npm run build
Right
41
NodeJS simple express http server Dockerfile example
42
Quick tips
• Use the --rm flag, so that the container data is removed after it finishes.
• Backup container data from time to time.
• Use docker exec to "enter into a container“. Never install SSH daemon in a container.
• Use scripts to remove stopped containers and images if you have a lack of disk space
- Bash (Linux, MacOS)
• docker rm $(docker ps -aq) –f
• docker rmi $(docker images –aq) -f
- CMD (Windows)
• FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
• FOR /f "tokens=*" %i IN ('docker images -a -q') DO docker rmi %i
• Prefer COPY over ADD.
• Careful with adding data in a volume in Dockerfile
43
MongoDB on Windows
• There is a known issue with mounting db folder to Windows host machine.
• As a workaround, you can create a volume in docker (docker volume create
<name>) and mount it to MongoDB /data/db folder.
• Do not forget to make dumps from time to time.
44
Angular hot reload inside a docker container
You can develop your angular application right inside a docker container.
• At first to start developing you must setup ng server to use 0.0.0.0 as a host
instead of localhost ng serve -H 0.0.0.0
• Then set up polling, because inotify does not work on mounted volume
ng set defaults.poll 100
• After that you must publish the container᾿s port by default it is 4200 and
mount your Angular project directory to your container working directory.
• You must reinstall node modules inside the container.
• Execute “npm start” and that’s it, very simple.
45
Conclusion
46
What next?
• Docker compose
• Docker swarm mode
• Learn GNU Linux
47
Useful links
• JS TechTalk Docker examples
• Official Docker documentation
• Docker Tips and Tricks
• Why use Docker
Thank you
Docker in a JS Developer’s Life

More Related Content

What's hot (20)

PDF
5 steps to take setting up a streamlined container pipeline
Michel Schildmeijer
 
PDF
Introduction to Docker | Docker and Kubernetes Training
Shailendra Chauhan
 
PDF
Azure Kubernetes Service - benefits and challenges
Wojciech Barczyński
 
PDF
Nugwc k8s session-16-march-2021
Avanti Patil
 
PPTX
Deep Dive OpenShitt on Azure & .NET Core on OpenShift
Takayoshi Tanaka
 
PDF
OpenStack in Enterprise
Nalee Jang
 
PDF
Mongo db world 2014 nyc mongodb on azure - tips tricks and examples
Brian Benz
 
PDF
Container Orchestration @Docker Meetup Hamburg
Timo Derstappen
 
PPTX
Architecting for Microservices Part 2
Elana Krasner
 
PPTX
Ultimate Guide to Microservice Architecture on Kubernetes
kloia
 
PPTX
Microservices with containers in the cloud
Eugene Fedorenko
 
PDF
컨테이너 기술 소개 - Warden, Garden, Docker
seungdon Choi
 
PPTX
Kubernetes @ meetic
Sébastien Le Gall
 
PDF
How lagom helps to build real world microservice systems
Markus Eisele
 
PPTX
2020-02-10 Java on Azure Solution Briefing
Ed Burns
 
PPTX
Adf with docker
Eugene Fedorenko
 
PDF
2012 CloudStack Design Camp in Taiwan--- CloudStack Overview-2
tcloudcomputing-tw
 
PDF
Virtualized Containers - How Good is it - Ananth - Siemens - CC18
CodeOps Technologies LLP
 
PDF
Batch Applications for the Java Platform
Sivakumar Thyagarajan
 
PDF
(Draft) Kubernetes - A Comprehensive Overview
Bob Killen
 
5 steps to take setting up a streamlined container pipeline
Michel Schildmeijer
 
Introduction to Docker | Docker and Kubernetes Training
Shailendra Chauhan
 
Azure Kubernetes Service - benefits and challenges
Wojciech Barczyński
 
Nugwc k8s session-16-march-2021
Avanti Patil
 
Deep Dive OpenShitt on Azure & .NET Core on OpenShift
Takayoshi Tanaka
 
OpenStack in Enterprise
Nalee Jang
 
Mongo db world 2014 nyc mongodb on azure - tips tricks and examples
Brian Benz
 
Container Orchestration @Docker Meetup Hamburg
Timo Derstappen
 
Architecting for Microservices Part 2
Elana Krasner
 
Ultimate Guide to Microservice Architecture on Kubernetes
kloia
 
Microservices with containers in the cloud
Eugene Fedorenko
 
컨테이너 기술 소개 - Warden, Garden, Docker
seungdon Choi
 
Kubernetes @ meetic
Sébastien Le Gall
 
How lagom helps to build real world microservice systems
Markus Eisele
 
2020-02-10 Java on Azure Solution Briefing
Ed Burns
 
Adf with docker
Eugene Fedorenko
 
2012 CloudStack Design Camp in Taiwan--- CloudStack Overview-2
tcloudcomputing-tw
 
Virtualized Containers - How Good is it - Ananth - Siemens - CC18
CodeOps Technologies LLP
 
Batch Applications for the Java Platform
Sivakumar Thyagarajan
 
(Draft) Kubernetes - A Comprehensive Overview
Bob Killen
 

Similar to Docker in a JS Developer’s Life (20)

PPTX
Docker, LinuX Container
Araf Karsh Hamid
 
PDF
Docker @ Atlogys
Atlogys Technical Consulting
 
PDF
Getting Started with Docker
Anup Segu
 
PPTX
Powercoders · Docker · Fall 2021.pptx
IgnacioTamayo2
 
PDF
Docker introduction - Part 1
Alessandro Mignogna
 
PPTX
Docker for developers z java
andrzejsydor
 
PDF
Docker.pdf
UsamaMushtaq24
 
PPTX
DockerSADASDASDA SADASDASDASDASDASDLabs.pptx
MuhamedAhmed35
 
PDF
How to write a Dockerfile
Knoldus Inc.
 
PPTX
Primi passi con Docker - ItalianCoders - 12-01-2021
Alessandro Mignogna
 
PPSX
Docker Kubernetes Istio
Araf Karsh Hamid
 
PDF
Lecture eight to be introduced in class.
nigamsajal14
 
PDF
docker.pdf
EishaTirRaazia1
 
PDF
Work shop - an introduction to the docker ecosystem
João Pedro Harbs
 
PPTX
Docker
Dmitry Bolgarov
 
PDF
Docker 101 Workshop slides (JavaOne 2017)
Eric Smalling
 
PDF
Dockercon 23 - Getting started with Docker
ssuserfb6acb
 
PPTX
Docker Starter Pack
Saeed Hajizade
 
PPTX
Virtualization, Containers, Docker and scalable container management services
abhishek chawla
 
ODP
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
lenworthhenry
 
Docker, LinuX Container
Araf Karsh Hamid
 
Getting Started with Docker
Anup Segu
 
Powercoders · Docker · Fall 2021.pptx
IgnacioTamayo2
 
Docker introduction - Part 1
Alessandro Mignogna
 
Docker for developers z java
andrzejsydor
 
Docker.pdf
UsamaMushtaq24
 
DockerSADASDASDA SADASDASDASDASDASDLabs.pptx
MuhamedAhmed35
 
How to write a Dockerfile
Knoldus Inc.
 
Primi passi con Docker - ItalianCoders - 12-01-2021
Alessandro Mignogna
 
Docker Kubernetes Istio
Araf Karsh Hamid
 
Lecture eight to be introduced in class.
nigamsajal14
 
docker.pdf
EishaTirRaazia1
 
Work shop - an introduction to the docker ecosystem
João Pedro Harbs
 
Docker 101 Workshop slides (JavaOne 2017)
Eric Smalling
 
Dockercon 23 - Getting started with Docker
ssuserfb6acb
 
Docker Starter Pack
Saeed Hajizade
 
Virtualization, Containers, Docker and scalable container management services
abhishek chawla
 
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
lenworthhenry
 
Ad

More from GlobalLogic Ukraine (20)

PDF
GlobalLogic JavaScript Community Webinar #21 “Інтерв’ю без заспокійливих”
GlobalLogic Ukraine
 
PPTX
Deadlocks in SQL - Turning Fear Into Understanding (by Sergii Stets)
GlobalLogic Ukraine
 
PDF
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Ukraine
 
PDF
GlobalLogic Embedded Community x ROS Ukraine Webinar "Surgical Robots"
GlobalLogic Ukraine
 
PDF
GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
GlobalLogic Ukraine
 
PDF
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
GlobalLogic Ukraine
 
PPTX
Штучний інтелект як допомога в навчанні, а не замінник.pptx
GlobalLogic Ukraine
 
PPTX
Задачі AI-розробника як застосовується штучний інтелект.pptx
GlobalLogic Ukraine
 
PPTX
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
GlobalLogic Ukraine
 
PDF
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
GlobalLogic Ukraine
 
PDF
JavaScript Community Webinar #14 "Why Is Git Rebase?"
GlobalLogic Ukraine
 
PDF
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
GlobalLogic Ukraine
 
PPTX
Страх і сила помилок - IT Inside від GlobalLogic Education
GlobalLogic Ukraine
 
PDF
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic Ukraine
 
PDF
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic Ukraine
 
PDF
“How to Secure Your Applications With a Keycloak?
GlobalLogic Ukraine
 
PDF
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Ukraine
 
PPTX
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Ukraine
 
PDF
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic Ukraine
 
PDF
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
GlobalLogic Ukraine
 
GlobalLogic JavaScript Community Webinar #21 “Інтерв’ю без заспокійливих”
GlobalLogic Ukraine
 
Deadlocks in SQL - Turning Fear Into Understanding (by Sergii Stets)
GlobalLogic Ukraine
 
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Ukraine
 
GlobalLogic Embedded Community x ROS Ukraine Webinar "Surgical Robots"
GlobalLogic Ukraine
 
GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
GlobalLogic Ukraine
 
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
GlobalLogic Ukraine
 
Штучний інтелект як допомога в навчанні, а не замінник.pptx
GlobalLogic Ukraine
 
Задачі AI-розробника як застосовується штучний інтелект.pptx
GlobalLogic Ukraine
 
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
GlobalLogic Ukraine
 
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
GlobalLogic Ukraine
 
JavaScript Community Webinar #14 "Why Is Git Rebase?"
GlobalLogic Ukraine
 
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
GlobalLogic Ukraine
 
Страх і сила помилок - IT Inside від GlobalLogic Education
GlobalLogic Ukraine
 
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic Ukraine
 
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic Ukraine
 
“How to Secure Your Applications With a Keycloak?
GlobalLogic Ukraine
 
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Ukraine
 
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Ukraine
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic Ukraine
 
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
GlobalLogic Ukraine
 
Ad

Recently uploaded (20)

PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Executive Business Intelligence Dashboards
vandeslie24
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 

Docker in a JS Developer’s Life

  • 2. Docker in a JS Developers’ Life Valerii Radchenko Senior Software Engineer 18.12.2017
  • 3. 3 Agenda 1. About Docker 2. Work with Dockerfiles 3. Docker CLI commands 4. Javascript and Docker practical tips Conclusion
  • 5. 5 What is Docker? ● Docker allows shipping your applications together with an environment. ● Docker is not a virtual machine, it uses the resource isolation features of the Linux kernel such as cgroups and kernel namespaces. ● Docker provides a free public cloud-based registry.
  • 6. 6 When should I use Docker? ● Do you need to run multiple copies of a single application (e.g, PostgreSQL, MySQL)? If yes, choose Docker. ● Are you OK with using one OS version for your application? If yes, choose Docker. ● Do you need to run as many applications as you can on a limited hardware budget? If yes, choose Docker. ● Do you need multiple (or obscure) OS’es? If yes, go with a VM.
  • 8. 8 Images ● An image is a read-only template with instructions for creating a Docker container. ● Images based on other images. Containers ● A container is a runnable instance of an image. ● A container is an isolated process. ● You can manage containers using Docker API or CLI and even create a new image based on its current state.
  • 10. 10 NodeJS simple express http server Dockerfile example
  • 11. 11 FROM instruction FROM <image>[:<tag>] [AS <name>] The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
  • 12. 12 RUN instruction RUN ["executable", "param1", "param2"] RUN <command> && <command> The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
  • 13. 13 ENTRYPOINT instruction ENTRYPOINT command param1 param2 ENTRYPOINT ["executable", "param1", "param2"] An ENTRYPOINT allows you to configure a container that will run as an executable.
  • 14. 14 CMD instruction CMD command param1 param2 CMD ["executable","param1","param2"] The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.
  • 15. 15 COPY instruction COPY ["<src>",... "<dest>"] COPY <src>... <dest> The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
  • 16. 16 ADD instruction ADD ["<src>",... "<dest>"] ADD <src>... <dest> The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.
  • 17. 17 EXPOSE instruction EXPOSE <port> [<port>/<protocol>...] The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
  • 18. 18 VOLUME instruction VOLUME ["/data"] VOLUME /data It creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. The value can be a JSON array, VOLUME ["/var/log/"], or a plain string with multiple arguments, such as VOLUME /var/log or VOLUME /var/log /var/db. More information/examples — Share Directories via Volumes documentation.
  • 19. 19 WORKDIR instruction WORKDIR /path/to/workdir The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
  • 21. 21 Build command ● Docker build –t name[:tag] <context path> - The docker build command builds Docker images from a Dockerfile and a “context”. - A build’s context is the set of files located in the specified PATH or URL. ● Flags - --tag , -t Name and optionally a tag in the ‘name:tag’ format - --file , -f Name of the Dockerfile (Default is ‘PATH/Dockerfile’)
  • 22. 22 Run command docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] ● Docker runs processes in isolated containers. ● A container is a process which runs on a host. ● The host may be local or remote.
  • 23. 23 Run command FLAGS Flags - --name Container identification. - -p=[] Publish a container᾿s port or a range of ports to the host. - -v, --volumeBind mount a volume. - --rm Automatically clean up the container and remove the file system when the container exits. - -it Keep STDIN open even if not attached and Allocate a pseudo-tty - -d Detached mode (background mode).
  • 24. 24 ps command docker ps [OPTIONS] - Show containers list. Flags --all , -a Show all containers (default shows just running) --quiet , -q Only display numeric IDs
  • 25. 25 Images command docker images [OPTIONS] [REPOSITORY[:TAG]] - Show images list. Flags --all , -a Show all images (default hides intermediate images) --quiet , -q Only display numeric IDs
  • 26. 26 Exec command docker exec [OPTIONS] CONTAINER COMMAND [ARG...] -Run a command in a running container. Flags --detach , -d Detached mode: run command in the background. --env , -e Set environment variables. -it Keep STDIN open even if not attached and Allocate a pseudo-tty
  • 27. 27 Attach command docker attach [OPTIONS] CONTAINER - Attach local standard input, output, and error streams to a running container.
  • 28. 28 Commit command docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] - Create a new image from a container’s changes.
  • 29. 29 Start, Stop, Restart commands docker (stop | start | restart) [OPTIONS] CONTAINER [CONTAINER...]
  • 30. 30 Login command docker login [OPTIONS] [SERVER] - Log in to a Docker registry.
  • 31. 31 Pull command docker pull [OPTIONS] NAME[:TAG|@DIGEST] - Pull an image or a repository from a registry.
  • 32. 32 Push command docker push [OPTIONS] NAME[:TAG] - Push an image or a repository to a registry.
  • 33. 33 Volume command docker volume create [OPTIONS] [VOLUME] - Create a volume. docker volume ls [OPTIONS] - Volumes list. docker volume rm [OPTIONS] VOLUME [VOLUME...] - Remove one or more volumes.
  • 34. 34 Network command docker network create [OPTIONS] NETWORK - Create a network. docker network ls [OPTIONS] - Networks list. docker network rm NETWORK [NETWORK...] - Remove one or more networks.
  • 35. 35 Javascript and Docker practical tips
  • 36. 36 Always use .dockerignore • The current working directory where you are located when you issue a docker build  command is called the build context. • All of the recursive contents of files and directories in the current directory are sent to the Docker daemon as the build context. • Inadvertently including files that are not necessary for building the image results in a larger build context and larger image size. • These in turn can increase build time, time to pull and push the image, and the runtime size of containers.
  • 37. Reduce image size • Make RUN instruction for every command RUN apt-get update RUN apt-get install curl • Use full-size base images FROM node:8 • Do not clean after build • Install unnecessary applications Wrong • Combine commands in one RUN instruction RUN apt-get update && apt-get install curl • Use alpine images whenever you can, otherwise use slim versions FROM node:8-alpine FROM node:8-slim • Clean applications that you do not need in a runtime • Clean cache • Use multi-stage build Right
  • 38. 38 Multi-stage building • Multi-stage builds are a new feature requiring Docker 17.05 or higher. • Multi-stage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain. • With multi-stage builds, you use multiple FROM statements in your Dockerfile. • Each FROM instruction can use a different base, and each of them begins a new stage of the build. • You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final
  • 39. 39
  • 40. Install node modules before copy source code COPY ./ $HOME/ RUN npm install && npm run build Wrong COPY ./package.json $HOME RUN npm install COPY ./ $HOME/ RUN npm run build Right
  • 41. 41 NodeJS simple express http server Dockerfile example
  • 42. 42 Quick tips • Use the --rm flag, so that the container data is removed after it finishes. • Backup container data from time to time. • Use docker exec to "enter into a container“. Never install SSH daemon in a container. • Use scripts to remove stopped containers and images if you have a lack of disk space - Bash (Linux, MacOS) • docker rm $(docker ps -aq) –f • docker rmi $(docker images –aq) -f - CMD (Windows) • FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i • FOR /f "tokens=*" %i IN ('docker images -a -q') DO docker rmi %i • Prefer COPY over ADD. • Careful with adding data in a volume in Dockerfile
  • 43. 43 MongoDB on Windows • There is a known issue with mounting db folder to Windows host machine. • As a workaround, you can create a volume in docker (docker volume create <name>) and mount it to MongoDB /data/db folder. • Do not forget to make dumps from time to time.
  • 44. 44 Angular hot reload inside a docker container You can develop your angular application right inside a docker container. • At first to start developing you must setup ng server to use 0.0.0.0 as a host instead of localhost ng serve -H 0.0.0.0 • Then set up polling, because inotify does not work on mounted volume ng set defaults.poll 100 • After that you must publish the container᾿s port by default it is 4200 and mount your Angular project directory to your container working directory. • You must reinstall node modules inside the container. • Execute “npm start” and that’s it, very simple.
  • 46. 46 What next? • Docker compose • Docker swarm mode • Learn GNU Linux
  • 47. 47 Useful links • JS TechTalk Docker examples • Official Docker documentation • Docker Tips and Tricks • Why use Docker