Docker for Developers

JasonStraughan1 152 views 60 slides Aug 17, 2023
Slide 1
Slide 1 of 60
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60

About This Presentation

Docker for Developers talk from the San Antonio Web Dev Meetup in Aug 2023

Never used Docker? This is perfect for you!

New to Docker? You'll learn something for sure!

Links included for all slides, code, and examples

Go from no Docker experience to a fully running web app in one slide deck!


Slide Content

San Antonio Web Developer Meetup
18 Aug 2023
Docker for Developers

What is Docker?
Docker is an operating system for containers. Similar to how a
virtual machine virtualizes (removes the need to directly
manage) server hardware, containers virtualize the operating
system of a server. Docker is installed on each server and
provides simple commands you can use to build, start, or stop
containers.
-AWS docs on Docker

What is Virtualization?
Virtualization is a process that allows a computer to share its
hardware resources with multiple digitally separated
environments. Each virtualized environment runs within its
allocated resources, such as memory, processing power, and
storage.
-Amazon AWS Docs

What are Containers?
A container is a standard unit of software that packages up
code and all its dependencies so the application runs quickly and
reliably from one computing environment to another. A Docker
container image is a lightweight, standalone, executable
package of software that includes everything needed to run
an application: code, runtime, system tools, system libraries
and settings.
-Docker Docs

What are Images?
A Docker image is a read-only template used to build
containers. Images are used to store and ship applications.
-Wikipedia

What is Docker, again?
Docker is an operating system for containers.

Containers virtualize the operating system of a server.

Docker provides simple commands you can use to build,
start, or stop containers.

Virtual Machines
Hypervisor runs VMs w/ Guest OSes
Each VM has its own Guest OS
Heavy use of RAM and storage space
Popular VMs:
- Virtualbox
- VMWare
Containers
Run engine on Host OS
Are lightweight using little RAM or storage
Each App/Container runs without guest OS
Popular Container Providers:
-Docker
-RKT (pronounced “Rocket”)
Containers vs Virtual Machines

Let’s setup Docker!
Quick Start like a BOSS

Our (Basic) Options
Docker Desktop
Docker Desktop is a GUI
application for quickly and
securely building and
sharing containerized
applications

Docker Engine
Docker Engine is an open
source containerization
technology for building and
containerizing your
applications using the CLI.

Docker Hub
Docker Hub is the world's
easiest way to create,
manage, and deliver your
team's container
applications.

Kubernetes
Kubernetes is an
open-source container
orchestration system for
automating software
deployment, scaling, and
management.

Our (Basic) Options
Docker Desktop
Docker Desktop is a GUI
application for quickly and
securely building and
sharing containerized
applications

Docker Engine
Docker Engine is an open
source containerization
technology for building and
containerizing your
applications using the CLI.

Docker Hub
Docker Hub is the world's
easiest way to create,
manage, and deliver your
team's container
applications.

Kubernetes
Kubernetes is an
open-source container
orchestration system for
automating software
deployment, scaling, and
management.

Homepage:
https://www.docker.com/

Documentation:
https://docs.docker.com/

Docker Hub:
https://hub.docker.com/
Install Docker Desktop
Windows/Linux/Mac

Play With Docker:
https://labs.play-with-docker.com/

A simple, interactive and fun playground to
learn Docker
Try Docker without Installing!

Let’s run some containers!
Using pre-built Images from Docker Hub

docker run image

Common Flags:
-i : Keep STDIN open even if not attached
-t : Allocate a pseudo-TTY

Examples:
docker run -it node
docker run -it python

Basic Docker CLI commands [docker run]

Basic Docker CLI commands [docker ps]
docker ps

Common Flags:
-a : Show all containers (default shows just running)

Example:
docker ps -a

docker rm container

Common Flags:
-f, --force : Force the removal of a running container (uses SIGKILL)
-l, --link : Remove the specified link
-v, --volumes : Remove anonymous volumes associated with the container

Examples:
docker rm 52b2fc6c1a3e
docker rm - v mynamedcontainer
Basic Docker CLI commands [docker rm]

Spin up containers
docker run dbs with ease

Other common flags:
-p : Publish a container's port(s) to the host
-d : Run container in background and print container ID
-e : Set environment variables
--name string : Assign a name to the container
--rm : Automatically remove the container when it exits

docker run --name mongodb --rm -d -p 27017:27017 mongo
docker run --rm -e POSTGRES_PASSWORD=password123 postgres
Start container from Docker Hub image using Docker CLI

Node.js Application
Using Image via Dockerfile

Basic Dockerfile for Node.js Application
DigitalOcean provides a sample Nodejs application that you can use to deploy on their
platform.

You can find it on GitHub at the following URL:
https://github.com/digitalocean/sample-nodejs

Basic Dockerfile for Node.js Application
FROM node

WORKDIR /app

COPY . /app/

RUN npm install

EXPOSE 3000

CMD ["npm", "start"]

Start Node.js Application using Docker CLI - docker run
First, we’ll build the image using docker build

docker build .

Then we’ll run the container using docker run

docker run image-name

NO SERVER FOUND
FAILURE!
We need to expose the PORT from our CONTAINER to
the HOST

From the docker run --help command:

-p : Publish a container's port(s) to the host

docker run -p 3000:3000 image-name

Start/Stop/Restart Containers
Just unplug it and plug it back in

Basic Docker CLI commands [docker start]
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Aliases:
docker container start, docker start

Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container's STDIN

Basic Docker CLI commands [docker stop]
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Aliases:
docker container stop, docker stop

Options:
-s, --signal string Signal to send to the container
-t, --time int Seconds to wait before killing the container

Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]

Restart one or more containers

Aliases:
docker container restart, docker restart

Options:
-s, --signal string Signal to send to the container
-t, --time int Seconds to wait before killing the container

Basic Docker CLI commands [docker restart]

Attached/Detached Containers
The psychology mode of containerization

Attaching to Running Container
By default, if you run a Container without -d, you run in "attached mode".
If you started a container in detached mode (with -d) you can still attach to it
afterwards without restarting using the following command:
docker attach CONTAINER

This attaches you to a running Container with an ID or name of CONTAINER.

●Deleting Images and Containers
●Removing Stopped Containers
●Copying files into and from a Container
●Naming & Tagging Containers
●Sharing via Docker Hub
Container/Image basics

Data in Docker
Containers, Images, Volumes…OH MY!

Images are READ ONLY
We’ll talk about this more in a bit

Image
•Read-only
•Provided by developer
•Data added in build phase
•Fixed: unchanged once
built

This is for Application and
Environment data
Container
•TEMP Read/Write
•Produced in running
container
•Stored in memory or
temp files
•Dynamic and
not-persistent

This is for Temporary App Data
Images / Containers / Volumes
Volume
•Produced in running
container
•Stored in files or DB
•Persists if container
stops/restarts

This is for permanent stored data

Volumes
Anonymous, Named, and others…

Anonymous Volumes
Anonymous volumes are removed automatically, when a container is removed if you start / run a
container with the --rm option
If you start a container without that option, the anonymous volume would NOT be remove
If you then re-create and re-run the container (run docker run ... again), a new anonymous volume will
be created.
Now you just start piling up a bunch of unused anonymous volumes

clear them via docker volume rm VOL_NAME or docker volume prune

Named Volumes
Named Volumes persist once a container is removed

They can be added to the container on run

Named Volumes allow you to persist data in databases, application file stores, etc

These Volumes are managed by Docker

Named Volumes & Bind Mounts
What the What?

Anonymous Volume
•Deleted when container
stops (if using --rm)
•New volume created on
new container instance

This is for read-only data that
does not need to be persisted
between restarts
Named Volume
•Persists data when
container is stopped
•Can be attached to a
container at runtime

This is for persisted data that
needs to live between restarts
Anonymous/Named Volumes and Bind Mounts
Bind Mount
•Links a directory on the
host machine to a location
in the container with
real-time updating

This is for persisted data that can
change live from the host machine
and is primarily used for
development purposes

Dockerfile Layers
Cooking by the Book

Basic Dockerfile for Node.js Application
FROM node

WORKDIR /app

COPY package.json .

RUN npm install

EXPOSE 3000

CMD ["npm", "start"]

docker run -d \
-p 3000:3000 \
--mount type=bind,source=[INSERT PATH TO WORKING
DIRECTORY HERE],target=/app


docker run -d -p 3000:3000 --mount type=bind,source="$(pwd)",target=/app
Basic Dockerfile for Node.js Application

Bind Mounts - Shortcuts
If you don't always want to copy and use the full path, you can use these shortcuts:
macOS / Linux: -v $(pwd):/app
Windows: -v "%cd%":/app

docker run -d \
-p 3000:3000 \
--mount type=bind,source="$(pwd)",target=/app


docker run -d -p 3000:3000 --mount type=bind,source="$(pwd)",target=/app
Basic Dockerfile for Node.js Application

Basic Dockerfile for Node.js Application
FROM node

WORKDIR /app

COPY package.json .

RUN npm install

EXPOSE 3000

VOLUME [ “/app/node_modules” ]

CMD ["npm", "start"]

{
"name": "app",

"scripts": {
"start": "nodemon --legacy-watch ./app ./bin/www "
},
"dependencies": {

"nodemon": "^2.0.4"
}
}


Add NODEMON

Docker Ignore File
.dockerignore stuffs

.dockerignore
Adding files & folders to the .dockerignore File

You can add files and folders to your .dockerignore file like:
Dockerfile
.git

This would ignore the Dockerfile itself as well as a potentially existing .git folder (if you are using Git in your
project).
In general, you want to add anything which isn't required by your application to execute correctly.

Networking
Cross-Container Communication

Docker Networks actually support different kinds of "Drivers" which influence the behavior of the
Network.
The default driver is the "bridge" driver - Containers can find each other by name if they are in the same
Network
The driver can be set when a Network is created, simply by adding the --driver option.
docker network create --driver bridge my-net

Of course, if you want to use the "bridge" driver, you can simply omit the entire option since "bridge" is the default
anyways.
Network Drivers

Docker also supports these alternative drivers - though you will use the "bridge" driver in most cases:
●host: For standalone containers, isolation between container and host system is removed (i.e. they
share localhost as a network)
●overlay: Multiple Docker daemons (i.e. Docker running on different machines) are able to connect
with each other. Only works in "Swarm" mode which is a dated / almost deprecated way of connecting
multiple containers
●macvlan: You can set a custom MAC address to a container - this address can then be used for
communication with that container
●none: All networking is disabled.
●3rd-party plugins: You can install third-party plugins which then may add all kinds of behaviors and
functionalities

As mentioned, the "bridge" driver makes most sense in the vast majority of scenarios.


Network Drivers (cont)

Multi-Container Application
https://github.com/JDStraughan/docker-mern-example

Multi-Container Application - Node.js + SPA + MongoDB
1.Start network
docker network create goals-net

2.Start MongoDB:

docker run --name mongodb --rm -d --network goals-net mongo

3.Start Backend Node.js App:

docker build -t goals-node .
docker run --name goals-backend --rm -d --network goals-net -p 80:80 goals-node

4.Start Frontend React App

docker build -t goals-react .
docker run --name goals-frontend --rm --network goals-net -p 3000:3000 -it goals-react

http://localhost:3000

Docker Compose
Multi-Container Orchestration

Compose is a tool for defining and running multi-container Docker
applications.

With Compose, you use a YAML file to configure your application’s
services.

Then, with a single command, you create and start all the
services from your configuration.
Docker Compose - Overview

There are two key commands:

docker compose up : Start all containers / services mentioned in the Docker Compose file
-d : Start in detached mode
--build : Force Docker Compose to re-evaluate / rebuild all images (otherwise, it only does that if an image
is missing)

docker compose down : Stop and remove all containers / services
-v : Remove all Volumes used for the Containers - otherwise they stay around, even if
the Containers are removed
Docker Compose - Key Commands

A Full Project
Let’s build some big stuff

Docker Compose - Full Projects
Awesome Compose
A curated list of Docker Compose
samples.
These samples provide a starting point
for how to integrate different services
using a Compose file and to manage their
deployment with Docker Compose.

https://github.com/docker/awesome-compose/tree/master

What’s Next?
What to do now that you’re using Docker

What’s Next?
Deploy a Dockerized Application:
●AWS EC2
●AWS ECS
●EFS w/ ECS
●Etc

Kubernetes:
●Managing Data & Volumes
●Networking
●Deployments (AWS EKS)

Kubernetes?