docker in a js developer’s life
Post on 22-Jan-2018
778 Views
Preview:
TRANSCRIPT
3
Agenda
1. About Docker
2. Work with Dockerfiles
3. Docker CLI commands
4. Javascript and Docker practical tipsConclusion
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.
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.
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 param2ENTRYPOINT ["executable", "param1", "param2"]
An ENTRYPOINT allows you to configure a container that will run as an executable.
14
CMD instruction
CMD command param1 param2CMD ["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 instructionVOLUME ["/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 instructionWORKDIR /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
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 FLAGSFlags
- --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.
31
Pull command
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- Pull an image or a repository from 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.
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 commandRUN apt-get updateRUN 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 instructionRUN 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
Install node modules before copy source code
COPY ./ $HOME/RUN npm install && \ npm run build
Wrong
COPY ./package.json $HOMERUN npm install
COPY ./ $HOME/RUN npm run build
Right
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.
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
top related