SlideShare uma empresa Scribd logo
1 de 44
Baixar para ler offline
Docker: Up and Running
Docker Images
Release 2022-09-13-0935
Instructor
Sean P. Kane
@spkane
@superorbital_io
Follow Along Guide
Textual Slides
Copyright © 2015-2022, Sean P. Kane (@spkane) 3
Katacoda Online Sandbox
https://learning.oreilly.com/scenarios/devops-tools-
sandbox/9781098126469/
Copyright © 2015-2022, Sean P. Kane (@spkane) 4
Prerequisites (1 of 2)
A recent computer and OS
Recent/Stable Linux, macOS, or Windows 10+
root/admin rights
Sufficient resources to run one 2 CPU virtual machine (VM)
CPU Virtualization extensions MUST be enabled in your
BIOS/EFI
Reliable and fast internet connectivity
Docker Community/Desktop Edition (on Linux also install Docker
Compose V2)
Copyright © 2015-2022, Sean P. Kane (@spkane) 5
Prerequisites (2 of 2)
A graphical web browser
A text editor
A software package manager
Git client
General comfort with the command line will be helpful.
[optional] tar, wget, curl, jq, SSH client
Copyright © 2015-2022, Sean P. Kane (@spkane) 6
A Note for Powershell Users
Terminal commands reflect the Unix bash shell. PowerShell users will
need to adjust the commands.
Unix Variables
export MY_VAR=test
echo ${MY_VAR}
Windows 10+ Variables (powershell)
$env:my_var = "test"
Get-ChildItem Env:my_var
Copyright © 2015-2022, Sean P. Kane (@spkane) 7
Translation Key
 - Unix Shell Line Continuation
` - Powershell Line Continuation (sort of)
${MY_VAR} - Is generally a place holder in the slides.
Copyright © 2015-2022, Sean P. Kane (@spkane) 8
Linux Container Mode
On the Windows platform make sure that you are running in Linux
Container mode.
Copyright © 2015-2022, Sean P. Kane (@spkane) 9
A Note About Proxies & VPNs
Proxies can interfere with some Docker activities if they are not
configured correctly and VPNs can increase audio and video
streaming issues in class.
If required, you can configure a proxy in Docker Desktop via the
preferences.
Docker
Docker-Compose
Copyright © 2015-2022, Sean P. Kane (@spkane) 10
Instructor Environment
Operating System: macOS (v12.5.X+)
Terminal: iTerm2 (Build 3.X.X+) - https://www.iterm2.com/
Shell Prompt Theme: Starship - https://starship.rs/
Shell Prompt Font: Fira Code -
https://github.com/tonsky/FiraCode
Text Editor: Visual Studio Code (v1.X.X+) -
https://code.visualstudio.com/
Copyright © 2015-2022, Sean P. Kane (@spkane) 11
Docker in Translation
Docker client
The docker command used to control most of the Docker
workflow and talk to remote Docker servers.
Docker server
The dockerd command used to launch the Docker daemon. This
turns a Linux system into a Docker server that can have
containers deployed, launched, and torn down via a remote
client.
Copyright © 2015-2022, Sean P. Kane (@spkane) 12
Docker in Translation
Virtual Machine
In general, the docker server can be only directly run on Linux.
Because of this, it is common to utilize a Linux virtual machine to
run Docker on other development platforms. Docker
Community/Desktop Edition makes this very easy.
Copyright © 2015-2022, Sean P. Kane (@spkane) 13
Docker in Translation
Docker/OCI images
Docker images consist of one or more filesystem layers and some
important metadata that represent all the files required to run a
Dockerized application. A single Docker image can be copied to
numerous hosts. A container will typically have both a name and
a tag. The tag is generally used to identify a particular release of
an image.
OCI - Open Container Initiative
Copyright © 2015-2022, Sean P. Kane (@spkane) 14
Docker in Translation
Linux Containers
A Linux Container is a single instantiation of a Docker or OCI-
standard image. A specific container can only exist once;
however, you can easily create multiple containers from the
same image.
Copyright © 2015-2022, Sean P. Kane (@spkane) 15
Testing the Docker Setup
$ docker image ls
$ docker container run -d --rm --name quantum 
--publish mode=ingress,published=18080,target=8080 
docker.io/spkane/quantum-game:latest
$ docker container ls
In a web browser, navigate to port 18080 on your Docker server.
$ docker container stop quantum
$ docker container ls
$ docker container ls -a
Copyright © 2015-2022, Sean P. Kane (@spkane) 16
Exploring the Dockerfile
$ cd ${HOME}
$ mkdir class
$ cd ${HOME}/class
$ git clone https://github.com/spkane/balance_game.git 
--config core.autocrlf=input
$ cd balance_game
Open & explore Dockerfile in your text editor
Full Documentation:
https://docs.docker.com/engine/reference/builder/
Copyright © 2015-2022, Sean P. Kane (@spkane) 17
Registering with Docker Hub
Create an account at: https://hub.docker.com/
Copyright © 2015-2022, Sean P. Kane (@spkane) 18
Create Your Image Repository
Login: https://hub.docker.com/
Click: Create Repository+
Enter name: balance_game
Set visibility: public
Click: Create
Copyright © 2015-2022, Sean P. Kane (@spkane) 19
Docker Login
$ docker login
$ cat ~/.docker/config.json
Copyright © 2015-2022, Sean P. Kane (@spkane) 20
Registry Authentication
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "q378t348q7tb78bfs387b==",
"email": "me@example.com"
}
}
}
Copyright © 2015-2022, Sean P. Kane (@spkane) 21
Building Your First Image
$ export HUB_USER=${USER}
$ docker image build -t docker.io/${HUB_USER}/balance_game:latest .
$ docker image push docker.io/${HUB_USER}/balance_game:latest
$ docker search ${HUB_USER}
$ docker container run -d --rm --name balance_game 
--publish mode=ingress,published=18081,target=80 
docker.io/${HUB_USER}/balance_game:latest
$ docker container stop balance_game
Copyright © 2015-2022, Sean P. Kane (@spkane) 22
Docker Hub API Examples
$ curl -s -S 
"https://registry.hub.docker.com/v2/repositories/library/alpine/tags/" 
| jq '."results"[]["name"]' | sort
Copyright © 2015-2022, Sean P. Kane (@spkane) 23
A Typical Build Process
$ DOCKER_BUILDKIT=0 docker image build -t docker.io/${HUB_USER}/balance_game
1. Client uploads build context to the server
2. Base image pulled, if required
3. New intermediate container created from base image
or empty container created, if using FROM scratch
4. Dockerfile command executed inside intermediate container
5. New image/layer created from intermediate container
6. If there is another step, a new intermediate container is created
from the last step, and then the build goes back to step 3.
Copyright © 2015-2022, Sean P. Kane (@spkane) 24
Advanced Dockerfile Techniques
Copyright © 2015-2022, Sean P. Kane (@spkane) 25
Keep it Small
Each and every layer’s size matters
Don’t install unnecessary files
$ cd ${HOME}/class
$ docker container run --rm -d --name outyet-small 
--publish mode=ingress,published=8090,target=8080 
docker.io/spkane/outyet:1.9.4-small
$ docker container export outyet-small -o export.tar
$ tar -tvf export.tar
$ docker container stop outyet-small
$ rm export.tar
Copyright © 2015-2022, Sean P. Kane (@spkane) 26
Debugging an Image
If your image has a shell installed you can access it using a
command like this:
$ docker image ls
$ docker container run --rm -ti docker.io/spkane/outyet:1.9.4-small /bin/sh
But without a shell in the image this will fail.
Copyright © 2015-2022, Sean P. Kane (@spkane) 27
Debugging an Image
So, let's fix this:
$ git clone https://github.com/spkane/outyet.git 
--config core.autocrlf=input
$ cd outyet
Copyright © 2015-2022, Sean P. Kane (@spkane) 28
Multi-Stage Images
FROM golang:1.9.4 as builder
COPY . /go/src/outyet
WORKDIR /go/src/outyet
ENV CGO_ENABLED=0
ENV GOOS=linux
RUN go get -v -d && 
go install -v && 
go test -v && 
go build -ldflags "-s" -a -installsuffix cgo -o outyet .
Copyright © 2015-2022, Sean P. Kane (@spkane) 29
Multi-Stage Images
# To support debugging, let's use alpine instead of scratch
FROM alpine:latest as deploy
# Since we are using alpine we can simply install these
RUN apk --no-cache add ca-certificates
WORKDIR /
COPY --from=builder /go/src/outyet/outyet .
# (or) COPY --from=0 /go/src/outyet/outyet .
EXPOSE 8080
CMD ["/outyet", "-version", "1.9.4", "-poll", "600s", "-http", ":8080"]
Copyright © 2015-2022, Sean P. Kane (@spkane) 30
Building the Improved Image
$ docker image build -f Dockerfile -t outyet:1.9.4-local .
Copyright © 2015-2022, Sean P. Kane (@spkane) 31
Debugging an Image
Now that we have a shell, let's try this again:
$ docker image ls
$ docker container run --rm -ti outyet:1.9.4-local /bin/sh
Once inside the new container:
$ ls -lFa /outyet
$ exit
Copyright © 2015-2022, Sean P. Kane (@spkane) 32
Debugging a Broken Build (1 of 2)
Break the Dockerfile and then try building it again.
RUN apc --no-cache add ca-certificates
$ docker image build -f Dockerfile .
Copyright © 2015-2022, Sean P. Kane (@spkane) 33
Debugging a Broken Build (2 of 2)
Let's debug the last successful image in that build
$ docker container run --rm -ti ${IMAGE_ID} /bin/sh
Once inside the new container:
$ apc --no-cache add ca-certificates
$ apk --no-cache add ca-certificates
$ exit
Copyright © 2015-2022, Sean P. Kane (@spkane) 34
Smart Layering
Each and every layer’s size matters
Clean up, inside of each step.
$ cd ${HOME}/class
$ cd balance_game
$ docker image build -f Dockerfile.fedora .
$ docker image tag ${IMAGE_ID} size${#}
$ docker image history size${#}
Copyright © 2015-2022, Sean P. Kane (@spkane) 35
Smart Layering
edit Dockerfile.fedora , build, and re-examine size
RUN dnf install -y httpd
RUN dnf clean all
RUN dnf install -y httpd && 
dnf clean all
Copyright © 2015-2022, Sean P. Kane (@spkane) 36
Timing commands in Windows
In the next exercise we will be timing commands using a Unix
utility. If you are on Windows and want to try to time these
commands locally, you can try something like this in Powershell.
PS C:> $t = Measure-Command { docker image build --no-cache . }
PS C:> Write-Host That command took $t.TotalSeconds to complete.
Copyright © 2015-2022, Sean P. Kane (@spkane) 37
Order Matters
Keep commands that change towards the end of your Dockerfile.
$ cd ${HOME}/class
$ cd balance_game
$ time docker image build --no-cache .
$ time docker image build .
edit start.sh
$ time docker image build .
Copyright © 2015-2022, Sean P. Kane (@spkane) 38
Order Matters
Add to the top of Dockerfile:
ADD start.sh /
And then remove the same line from the bottom of the Dockerfile.
$ time docker image build --no-cache .
$ time docker image build .
$ vi start.sh
$ time docker image build .
Copyright © 2015-2022, Sean P. Kane (@spkane) 39
What We Have Learned
What a Dockerfile is
Building a Docker image
Using Docker Hub
Keeping Images Small
Keeping Builds Fast
Multi-Stage Dockerfiles
Debugging Images
Copyright © 2015-2022, Sean P. Kane (@spkane) 40
Additional Reading
The 12-Factor App
http://12factor.net/
Official Docker Documentation
https://docs.docker.com/
Docker: Up and Running
http://shop.oreilly.com/product/0636920153566.do
Copyright © 2015-2022, Sean P. Kane (@spkane) 41
Additional Learning Resources
https://learning.oreilly.com/
Copyright © 2015-2022, Sean P. Kane (@spkane) 42
Student Survey
Please take a moment to fill out the class survey linked to from the
bottom of the ON24 audience screen.
O’Reilly and I value your comments about the class.
Thank you!
Copyright © 2015-2022, Sean P. Kane (@spkane) 43
Any Questions?
Sean P. Kane
Providing stellar Kubernetes engineering and workshops.
https://superorbital.io/contact/
Copyright © 2015-2022, Sean P. Kane (@spkane) 44

Mais conteúdo relacionado

Semelhante a classdockerimages.pdf

Semelhante a classdockerimages.pdf (20)

Into to Docker (Central PA Java User Group - 8/14/2017)
Into to Docker (Central PA Java User Group - 8/14/2017)Into to Docker (Central PA Java User Group - 8/14/2017)
Into to Docker (Central PA Java User Group - 8/14/2017)
 
Docker in Action
Docker in ActionDocker in Action
Docker in Action
 
Learning Docker with Thomas
Learning Docker with ThomasLearning Docker with Thomas
Learning Docker with Thomas
 
Oracle Database 18c Docker.pdf
Oracle Database 18c Docker.pdfOracle Database 18c Docker.pdf
Oracle Database 18c Docker.pdf
 
Docker
DockerDocker
Docker
 
Apt get no more let Vagrant, Puppet and Docker take the stage
Apt get no more let Vagrant, Puppet and Docker take the stageApt get no more let Vagrant, Puppet and Docker take the stage
Apt get no more let Vagrant, Puppet and Docker take the stage
 
The Dockerfile Explosion and the Need for Higher Level Tools by Gareth Rushgrove
The Dockerfile Explosion and the Need for Higher Level Tools by Gareth RushgroveThe Dockerfile Explosion and the Need for Higher Level Tools by Gareth Rushgrove
The Dockerfile Explosion and the Need for Higher Level Tools by Gareth Rushgrove
 
Docker - Der Wal in der Kiste
Docker - Der Wal in der KisteDocker - Der Wal in der Kiste
Docker - Der Wal in der Kiste
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to Docker
 
Docker Container As A Service - Mix-IT 2016
Docker Container As A Service - Mix-IT 2016Docker Container As A Service - Mix-IT 2016
Docker Container As A Service - Mix-IT 2016
 
Dockerfiles building docker images automatically v (workdir, env, add, and ...
Dockerfiles   building docker images automatically v (workdir, env, add, and ...Dockerfiles   building docker images automatically v (workdir, env, add, and ...
Dockerfiles building docker images automatically v (workdir, env, add, and ...
 
Dockerizing a Symfony2 application
Dockerizing a Symfony2 applicationDockerizing a Symfony2 application
Dockerizing a Symfony2 application
 
Running the Oracle SOA Suite Environment in a Docker Container
Running the Oracle SOA Suite Environment in a Docker ContainerRunning the Oracle SOA Suite Environment in a Docker Container
Running the Oracle SOA Suite Environment in a Docker Container
 
Delivering Docker & K3s worloads to IoT Edge devices
Delivering Docker & K3s worloads to IoT Edge devicesDelivering Docker & K3s worloads to IoT Edge devices
Delivering Docker & K3s worloads to IoT Edge devices
 
Running Docker in Development & Production (DevSum 2015)
Running Docker in Development & Production (DevSum 2015)Running Docker in Development & Production (DevSum 2015)
Running Docker in Development & Production (DevSum 2015)
 
How to Improve Your Image Builds Using Advance Docker Build
How to Improve Your Image Builds Using Advance Docker BuildHow to Improve Your Image Builds Using Advance Docker Build
How to Improve Your Image Builds Using Advance Docker Build
 
Docker for .NET Developers - Michele Leroux Bustamante, Solliance
Docker for .NET Developers - Michele Leroux Bustamante, SollianceDocker for .NET Developers - Michele Leroux Bustamante, Solliance
Docker for .NET Developers - Michele Leroux Bustamante, Solliance
 
JDD2014: Docker.io - versioned linux containers for JVM devops - Dominik Dorn
JDD2014: Docker.io - versioned linux containers for JVM devops - Dominik DornJDD2014: Docker.io - versioned linux containers for JVM devops - Dominik Dorn
JDD2014: Docker.io - versioned linux containers for JVM devops - Dominik Dorn
 
Docker Essentials Workshop— Innovation Labs July 2020
Docker Essentials Workshop— Innovation Labs July 2020Docker Essentials Workshop— Innovation Labs July 2020
Docker Essentials Workshop— Innovation Labs July 2020
 
Docker
DockerDocker
Docker
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 

classdockerimages.pdf

  • 1. Docker: Up and Running Docker Images Release 2022-09-13-0935
  • 3. Follow Along Guide Textual Slides Copyright © 2015-2022, Sean P. Kane (@spkane) 3
  • 5. Prerequisites (1 of 2) A recent computer and OS Recent/Stable Linux, macOS, or Windows 10+ root/admin rights Sufficient resources to run one 2 CPU virtual machine (VM) CPU Virtualization extensions MUST be enabled in your BIOS/EFI Reliable and fast internet connectivity Docker Community/Desktop Edition (on Linux also install Docker Compose V2) Copyright © 2015-2022, Sean P. Kane (@spkane) 5
  • 6. Prerequisites (2 of 2) A graphical web browser A text editor A software package manager Git client General comfort with the command line will be helpful. [optional] tar, wget, curl, jq, SSH client Copyright © 2015-2022, Sean P. Kane (@spkane) 6
  • 7. A Note for Powershell Users Terminal commands reflect the Unix bash shell. PowerShell users will need to adjust the commands. Unix Variables export MY_VAR=test echo ${MY_VAR} Windows 10+ Variables (powershell) $env:my_var = "test" Get-ChildItem Env:my_var Copyright © 2015-2022, Sean P. Kane (@spkane) 7
  • 8. Translation Key - Unix Shell Line Continuation ` - Powershell Line Continuation (sort of) ${MY_VAR} - Is generally a place holder in the slides. Copyright © 2015-2022, Sean P. Kane (@spkane) 8
  • 9. Linux Container Mode On the Windows platform make sure that you are running in Linux Container mode. Copyright © 2015-2022, Sean P. Kane (@spkane) 9
  • 10. A Note About Proxies & VPNs Proxies can interfere with some Docker activities if they are not configured correctly and VPNs can increase audio and video streaming issues in class. If required, you can configure a proxy in Docker Desktop via the preferences. Docker Docker-Compose Copyright © 2015-2022, Sean P. Kane (@spkane) 10
  • 11. Instructor Environment Operating System: macOS (v12.5.X+) Terminal: iTerm2 (Build 3.X.X+) - https://www.iterm2.com/ Shell Prompt Theme: Starship - https://starship.rs/ Shell Prompt Font: Fira Code - https://github.com/tonsky/FiraCode Text Editor: Visual Studio Code (v1.X.X+) - https://code.visualstudio.com/ Copyright © 2015-2022, Sean P. Kane (@spkane) 11
  • 12. Docker in Translation Docker client The docker command used to control most of the Docker workflow and talk to remote Docker servers. Docker server The dockerd command used to launch the Docker daemon. This turns a Linux system into a Docker server that can have containers deployed, launched, and torn down via a remote client. Copyright © 2015-2022, Sean P. Kane (@spkane) 12
  • 13. Docker in Translation Virtual Machine In general, the docker server can be only directly run on Linux. Because of this, it is common to utilize a Linux virtual machine to run Docker on other development platforms. Docker Community/Desktop Edition makes this very easy. Copyright © 2015-2022, Sean P. Kane (@spkane) 13
  • 14. Docker in Translation Docker/OCI images Docker images consist of one or more filesystem layers and some important metadata that represent all the files required to run a Dockerized application. A single Docker image can be copied to numerous hosts. A container will typically have both a name and a tag. The tag is generally used to identify a particular release of an image. OCI - Open Container Initiative Copyright © 2015-2022, Sean P. Kane (@spkane) 14
  • 15. Docker in Translation Linux Containers A Linux Container is a single instantiation of a Docker or OCI- standard image. A specific container can only exist once; however, you can easily create multiple containers from the same image. Copyright © 2015-2022, Sean P. Kane (@spkane) 15
  • 16. Testing the Docker Setup $ docker image ls $ docker container run -d --rm --name quantum --publish mode=ingress,published=18080,target=8080 docker.io/spkane/quantum-game:latest $ docker container ls In a web browser, navigate to port 18080 on your Docker server. $ docker container stop quantum $ docker container ls $ docker container ls -a Copyright © 2015-2022, Sean P. Kane (@spkane) 16
  • 17. Exploring the Dockerfile $ cd ${HOME} $ mkdir class $ cd ${HOME}/class $ git clone https://github.com/spkane/balance_game.git --config core.autocrlf=input $ cd balance_game Open & explore Dockerfile in your text editor Full Documentation: https://docs.docker.com/engine/reference/builder/ Copyright © 2015-2022, Sean P. Kane (@spkane) 17
  • 18. Registering with Docker Hub Create an account at: https://hub.docker.com/ Copyright © 2015-2022, Sean P. Kane (@spkane) 18
  • 19. Create Your Image Repository Login: https://hub.docker.com/ Click: Create Repository+ Enter name: balance_game Set visibility: public Click: Create Copyright © 2015-2022, Sean P. Kane (@spkane) 19
  • 20. Docker Login $ docker login $ cat ~/.docker/config.json Copyright © 2015-2022, Sean P. Kane (@spkane) 20
  • 21. Registry Authentication { "auths": { "https://index.docker.io/v1/": { "auth": "q378t348q7tb78bfs387b==", "email": "me@example.com" } } } Copyright © 2015-2022, Sean P. Kane (@spkane) 21
  • 22. Building Your First Image $ export HUB_USER=${USER} $ docker image build -t docker.io/${HUB_USER}/balance_game:latest . $ docker image push docker.io/${HUB_USER}/balance_game:latest $ docker search ${HUB_USER} $ docker container run -d --rm --name balance_game --publish mode=ingress,published=18081,target=80 docker.io/${HUB_USER}/balance_game:latest $ docker container stop balance_game Copyright © 2015-2022, Sean P. Kane (@spkane) 22
  • 23. Docker Hub API Examples $ curl -s -S "https://registry.hub.docker.com/v2/repositories/library/alpine/tags/" | jq '."results"[]["name"]' | sort Copyright © 2015-2022, Sean P. Kane (@spkane) 23
  • 24. A Typical Build Process $ DOCKER_BUILDKIT=0 docker image build -t docker.io/${HUB_USER}/balance_game 1. Client uploads build context to the server 2. Base image pulled, if required 3. New intermediate container created from base image or empty container created, if using FROM scratch 4. Dockerfile command executed inside intermediate container 5. New image/layer created from intermediate container 6. If there is another step, a new intermediate container is created from the last step, and then the build goes back to step 3. Copyright © 2015-2022, Sean P. Kane (@spkane) 24
  • 25. Advanced Dockerfile Techniques Copyright © 2015-2022, Sean P. Kane (@spkane) 25
  • 26. Keep it Small Each and every layer’s size matters Don’t install unnecessary files $ cd ${HOME}/class $ docker container run --rm -d --name outyet-small --publish mode=ingress,published=8090,target=8080 docker.io/spkane/outyet:1.9.4-small $ docker container export outyet-small -o export.tar $ tar -tvf export.tar $ docker container stop outyet-small $ rm export.tar Copyright © 2015-2022, Sean P. Kane (@spkane) 26
  • 27. Debugging an Image If your image has a shell installed you can access it using a command like this: $ docker image ls $ docker container run --rm -ti docker.io/spkane/outyet:1.9.4-small /bin/sh But without a shell in the image this will fail. Copyright © 2015-2022, Sean P. Kane (@spkane) 27
  • 28. Debugging an Image So, let's fix this: $ git clone https://github.com/spkane/outyet.git --config core.autocrlf=input $ cd outyet Copyright © 2015-2022, Sean P. Kane (@spkane) 28
  • 29. Multi-Stage Images FROM golang:1.9.4 as builder COPY . /go/src/outyet WORKDIR /go/src/outyet ENV CGO_ENABLED=0 ENV GOOS=linux RUN go get -v -d && go install -v && go test -v && go build -ldflags "-s" -a -installsuffix cgo -o outyet . Copyright © 2015-2022, Sean P. Kane (@spkane) 29
  • 30. Multi-Stage Images # To support debugging, let's use alpine instead of scratch FROM alpine:latest as deploy # Since we are using alpine we can simply install these RUN apk --no-cache add ca-certificates WORKDIR / COPY --from=builder /go/src/outyet/outyet . # (or) COPY --from=0 /go/src/outyet/outyet . EXPOSE 8080 CMD ["/outyet", "-version", "1.9.4", "-poll", "600s", "-http", ":8080"] Copyright © 2015-2022, Sean P. Kane (@spkane) 30
  • 31. Building the Improved Image $ docker image build -f Dockerfile -t outyet:1.9.4-local . Copyright © 2015-2022, Sean P. Kane (@spkane) 31
  • 32. Debugging an Image Now that we have a shell, let's try this again: $ docker image ls $ docker container run --rm -ti outyet:1.9.4-local /bin/sh Once inside the new container: $ ls -lFa /outyet $ exit Copyright © 2015-2022, Sean P. Kane (@spkane) 32
  • 33. Debugging a Broken Build (1 of 2) Break the Dockerfile and then try building it again. RUN apc --no-cache add ca-certificates $ docker image build -f Dockerfile . Copyright © 2015-2022, Sean P. Kane (@spkane) 33
  • 34. Debugging a Broken Build (2 of 2) Let's debug the last successful image in that build $ docker container run --rm -ti ${IMAGE_ID} /bin/sh Once inside the new container: $ apc --no-cache add ca-certificates $ apk --no-cache add ca-certificates $ exit Copyright © 2015-2022, Sean P. Kane (@spkane) 34
  • 35. Smart Layering Each and every layer’s size matters Clean up, inside of each step. $ cd ${HOME}/class $ cd balance_game $ docker image build -f Dockerfile.fedora . $ docker image tag ${IMAGE_ID} size${#} $ docker image history size${#} Copyright © 2015-2022, Sean P. Kane (@spkane) 35
  • 36. Smart Layering edit Dockerfile.fedora , build, and re-examine size RUN dnf install -y httpd RUN dnf clean all RUN dnf install -y httpd && dnf clean all Copyright © 2015-2022, Sean P. Kane (@spkane) 36
  • 37. Timing commands in Windows In the next exercise we will be timing commands using a Unix utility. If you are on Windows and want to try to time these commands locally, you can try something like this in Powershell. PS C:> $t = Measure-Command { docker image build --no-cache . } PS C:> Write-Host That command took $t.TotalSeconds to complete. Copyright © 2015-2022, Sean P. Kane (@spkane) 37
  • 38. Order Matters Keep commands that change towards the end of your Dockerfile. $ cd ${HOME}/class $ cd balance_game $ time docker image build --no-cache . $ time docker image build . edit start.sh $ time docker image build . Copyright © 2015-2022, Sean P. Kane (@spkane) 38
  • 39. Order Matters Add to the top of Dockerfile: ADD start.sh / And then remove the same line from the bottom of the Dockerfile. $ time docker image build --no-cache . $ time docker image build . $ vi start.sh $ time docker image build . Copyright © 2015-2022, Sean P. Kane (@spkane) 39
  • 40. What We Have Learned What a Dockerfile is Building a Docker image Using Docker Hub Keeping Images Small Keeping Builds Fast Multi-Stage Dockerfiles Debugging Images Copyright © 2015-2022, Sean P. Kane (@spkane) 40
  • 41. Additional Reading The 12-Factor App http://12factor.net/ Official Docker Documentation https://docs.docker.com/ Docker: Up and Running http://shop.oreilly.com/product/0636920153566.do Copyright © 2015-2022, Sean P. Kane (@spkane) 41
  • 43. Student Survey Please take a moment to fill out the class survey linked to from the bottom of the ON24 audience screen. O’Reilly and I value your comments about the class. Thank you! Copyright © 2015-2022, Sean P. Kane (@spkane) 43
  • 44. Any Questions? Sean P. Kane Providing stellar Kubernetes engineering and workshops. https://superorbital.io/contact/ Copyright © 2015-2022, Sean P. Kane (@spkane) 44