SlideShare a Scribd company logo
1 of 37
Download to read offline
Introduction To Docker
CMSC388T
Contents
2
Docker
Getting started with using Docker CLI and
working with containerized applications
Docker CLI
Understanding Docker and Containerized
Applications.
Dockerize your own application in a custom
docker image
Dockerizing an Application
1
2
3
1. Docker
Understanding Docker and
Containerized Applications.
● Isolates applications and allocates resources to
run that application
● VMs can be shared as images
● Aren’t dependent on the Host OS
● Multiple VMs can be run simultaneously using a
hypervisor
Virtual Machines
4
Image Source: docker.com
● Standard unit of software
● Packages code and dependencies
● Can be shared as Docker Images
● Multiple containers can be run simultaneously
● Portable - Can be used with any OS
● Lightweight - Uses the host operating system
● Secure - Strong default isolation features
● Sometimes used with VMs
Docker Containers
5
Image Source: docker.com
● Breaks large applications down into
smaller executable components
● Easy to maintain and test
● Loosely coupled and can be deployed
independently
● Can be combined with serverless
architecture (AWS Fargate)
Microservices
6
Image Source: hackernoon.com
● Develop applications that work on any OS
● Easy to share applications among teams
● Easy to scale across multiple servers
● Large applications can be broken into multiple
containers - one for each microservice
● Great solution for Cloud Computing
● Big community and library of Docker Images
Why Use Docker
7
Image Source: docker.com
● Removes Dependency on Infrastructure
● Allows developers to focus on application
development
● Microservices can be decoupled with different
cloud services
● Usually more cost effective
● Probably covered in more depth in a Cloud
Computing class
Serverless
8
Image Source: aws.amazon.com
Which of the following is an advantage of using Docker
Containers over Virtual Machines?
a) Multiple containers can be run on the same machine
b) Uses the Host OS
c) Can be shared as images
d) all of the above
Clicker Quiz
Which of the following is an advantage of using Docker
Containers over Virtual Machines?
a) Multiple containers can be run on the same machine
b) Uses the Host OS
c) Can be shared as images
d) all of the above
Clicker Quiz
2. Using Docker CLI
Working with Gitlab to work in a
DevOps environment with our existing
Github Repositories
Install Docker Engine
Install Docker Engine
• Docker Engine is available on
a variety of Linux platforms,
macOS and Windows 10
through Docker Desktop
There are many publicly available images that we can use to work with Docker.
The example below pulls a hello-world image using the docker pull command:
Pull An Image
13
[ ~ ]docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[ ~ ]docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
To create a container from an image we can use the docker create command
Create A Container
14
[ ~ ]docker create hello-world
2ffd5f2c5a7562fbf1d7b89a14c11a52e5843dd7938f380a8cd53f3952da99de
To run a container we can use the docker container start command to start a
container. The -i runs the container interactively and allows us to see the output
Run A Container
15
[ ~ ] docker container start -i 2ffd5f2c5a7562fbf1d7…
Hello from Docker!
This message shows that your installation appears to be working correctly.
There is a shortcut for building a container from an image and running it with the
docker run command. This will create a new container for an image and run it:
Run An Image
16
[ ~ ] docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To see what images are already installed on your machine you can use the
docker image ls command. We can see our hello-world image below:
List Images
17
[ ~ ]docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f63181f19b2f 13 hours ago 72.9MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
To list the containers that we have built, we can use the docker container ls command.
The -a flag allows us to see both stopped and running containers. There are two containers
below, one that was built with the docker create command and the other that was built with
docker run:
List Containers
18
[ ~ ]docker container ls –a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5017fd2b94c2 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago stoic_nobel
5f0cea57eacf ubuntu "bash" 10 minutes ago Exited (127) 8 minutes ago condescending_neumann
2ffd5f2c5a75 hello-world "/hello" 14 minutes ago Exited (0) 13 minutes ago hungry_mclaren
Running containers interactively allows you to run commands inside the container if it
supports it. We can use the openjdk image that we used before:
Running Interactively
19
This allows us to execute java commands line by line in a Java shell
[ ~ ]docker run -it openjdk
Unable to find image 'openjdk:latest' locally
latest: Pulling from library/openjdk
a73adebe9317: Pull complete
8b73bcd34cfe: Pull complete
1227243b28c4: Pull complete
Digest: sha256:7ada0d840136690ac1099ce3172fb02787bbed83462597e0e2c9472a0a63dea5
Status: Downloaded newer image for openjdk:latest
Jan 21, 2021 4:48:58 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
| Welcome to JShell -- Version 15.0.2
| For an introduction type: /help intro
jshell> System.out.println(”hello world");
Hello world
To see what containers are currently running, we can use the docker ps command. This
is useful when you are running containers in the background.
List Running Processes
20
[ ~ ]docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55e4a7c3ddcc openjdk "jshell" 11 seconds ago Up 10 seconds affectionate_kowalevski
Interactive shell
[ ~ ] docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
83ee3a23efb7: Pull complete
db98fc6f11f0: Pull complete
f611acd52c6c: Pull complete
Digest:
sha256:703218c0465075f4425e58fac086e09e1de5c340b12976ab9eb8ad26615c3715
Status: Downloaded newer image for ubuntu:latest
root@5f0cea57eacf:/#
How can you run a container?
a) docker container start [ContainerID]
b) docker run [ContainerID]
c) docker run [ImageID]
d) none of the above
Clicker Quiz
How can you run a container?
a) docker container start [ContainerID]
b) docker run [ContainerID]
c) docker run [ImageID]
d) none of the above
Clicker Quiz
3. Dockerize An
Application
Dockerize your own application in a
custom docker image
Revisiting the Calculator TestRepo
25
Let’s dockerize the code in the repository
we created in Lecture 6. It has a calculator
class and two JUnit tests for add and
subtract.
Adding A Dockerfile
26
To create a custom docker image. We need
to create a Dockerfile. The dockerfile
specifies how our image should be built.
Dockerfile
Dockerfile Syntax
27
● From - The base image to use
● Run - Runs commands when building
the docker image
● Workdir - Specifies the directory that
commands are run from
● User - Switches users
● Copy - Copies Files
● CMD - Runs commands when
running the container
Dockerfile
Dockerfile Explained
28
1. Use the OpenJDK image to have a pre-
configured java environment
2. Add a new user “ojdk” that we will be
using for executing scripts
3. Create a directory that will contain our
files and give permission to our user
4. Change the working directory to the
directory we created
5. Copy the java and junit files
6. Switch to user ojdk
7. Compile all of our code
8. Copy files to the working directory and
give permissions to ojdk
9. Run all tests
Dockerfile
To build a docker image using a Dockerfile we can use the docker image build
command and provide it the directory where the Dockerfile exists. The --tag
option allows us to name and tag the docker image.
Build A Docker Image
29
We can use the docker run command to run our image and we can see that our
tests are being run:
Run Our Custom Image
30
We can use docker run -it to run our image interactively and open a bash
shell in our working directory:
Run Interactively
31
Docker Compose File
32
Docker Compose files can be used to run
multiple services at once and is great once
you have many microservices as part of
your application. For our project we create
a single service calculator that
● is built using a custom Dockerfile
● tagged with the name calculator
● restarts unless it is stopped
docker-compose.yml
To run our docker compose file, we use the docker-compose up command. This
builds all images and and runs and containers.
Run Docker Compose
33
The calculator container will keep restarting unless its stopped. To stop all
services, we can use the docker-compose down command:
Stop Containers
34
To remove all unused docker resources, we can use the docker system prune
command with the --all flag:
Clean Up
35
What’s the difference between the RUN and CMD commands
in the Dockerfile?
a) CMD is executed when images are being built while
RUN is executed when containers are started
b) RUN is executed when images are being built while
CMD is executed when containers are started
c) There is no difference between RUN and CMD
d) There is no difference between RUN and CMD unless
the container is started interactively
Clicker Quiz
What’s the difference between the RUN and CMD commands
in the Dockerfile?
a) CMD is executed when images are being built while
RUN is executed when containers are started
b) RUN is executed when images are being built while
CMD is executed when containers are started
c) There is no difference between RUN and CMD
d) There is no difference between RUN and CMD unless
the container is started interactively
Clicker Quiz

More Related Content

Similar to docker.pdf

Powercoders · Docker · Fall 2021.pptx
Powercoders · Docker · Fall 2021.pptxPowercoders · Docker · Fall 2021.pptx
Powercoders · Docker · Fall 2021.pptxIgnacioTamayo2
 
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 2020CloudHero
 
Docker up and Running For Web Developers
Docker up and Running For Web DevelopersDocker up and Running For Web Developers
Docker up and Running For Web DevelopersBADR
 
Docker Up and Running for Web Developers
Docker Up and Running for Web DevelopersDocker Up and Running for Web Developers
Docker Up and Running for Web DevelopersAmr Fawzy
 
Docker
DockerDocker
Dockersubbul
 
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...Ambassador Labs
 
Tips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podmanTips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podmanThierry Gayet
 
Managing Docker containers
Managing Docker containersManaging Docker containers
Managing Docker containerssiuyin
 
Up and running with docker
Up and running with dockerUp and running with docker
Up and running with dockerMichelle Liu
 
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
Introduction to Docker for NodeJs developers at Node DC 2/26/2014Introduction to Docker for NodeJs developers at Node DC 2/26/2014
Introduction to Docker for NodeJs developers at Node DC 2/26/2014lenworthhenry
 

Similar to docker.pdf (20)

Docker workshop GDSC_CSSC
Docker workshop GDSC_CSSCDocker workshop GDSC_CSSC
Docker workshop GDSC_CSSC
 
Docker, LinuX Container
Docker, LinuX ContainerDocker, LinuX Container
Docker, LinuX Container
 
Powercoders · Docker · Fall 2021.pptx
Powercoders · Docker · Fall 2021.pptxPowercoders · Docker · Fall 2021.pptx
Powercoders · Docker · Fall 2021.pptx
 
DOCKER-PIAIC-SLIDES
DOCKER-PIAIC-SLIDESDOCKER-PIAIC-SLIDES
DOCKER-PIAIC-SLIDES
 
Docker
DockerDocker
Docker
 
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
 
How to _docker
How to _dockerHow to _docker
How to _docker
 
Containerization using docker and its applications
Containerization using docker and its applicationsContainerization using docker and its applications
Containerization using docker and its applications
 
Containerization using docker and its applications
Containerization using docker and its applicationsContainerization using docker and its applications
Containerization using docker and its applications
 
Let's dockerize
Let's dockerizeLet's dockerize
Let's dockerize
 
Introduction To Docker
Introduction To  DockerIntroduction To  Docker
Introduction To Docker
 
Learning Docker with Thomas
Learning Docker with ThomasLearning Docker with Thomas
Learning Docker with Thomas
 
Docker up and Running For Web Developers
Docker up and Running For Web DevelopersDocker up and Running For Web Developers
Docker up and Running For Web Developers
 
Docker Up and Running for Web Developers
Docker Up and Running for Web DevelopersDocker Up and Running for Web Developers
Docker Up and Running for Web Developers
 
Docker
DockerDocker
Docker
 
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
 
Tips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podmanTips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podman
 
Managing Docker containers
Managing Docker containersManaging Docker containers
Managing Docker containers
 
Up and running with docker
Up and running with dockerUp and running with docker
Up and running with docker
 
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
Introduction to Docker for NodeJs developers at Node DC 2/26/2014Introduction to Docker for NodeJs developers at Node DC 2/26/2014
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

docker.pdf

  • 2. Contents 2 Docker Getting started with using Docker CLI and working with containerized applications Docker CLI Understanding Docker and Containerized Applications. Dockerize your own application in a custom docker image Dockerizing an Application 1 2 3
  • 3. 1. Docker Understanding Docker and Containerized Applications.
  • 4. ● Isolates applications and allocates resources to run that application ● VMs can be shared as images ● Aren’t dependent on the Host OS ● Multiple VMs can be run simultaneously using a hypervisor Virtual Machines 4 Image Source: docker.com
  • 5. ● Standard unit of software ● Packages code and dependencies ● Can be shared as Docker Images ● Multiple containers can be run simultaneously ● Portable - Can be used with any OS ● Lightweight - Uses the host operating system ● Secure - Strong default isolation features ● Sometimes used with VMs Docker Containers 5 Image Source: docker.com
  • 6. ● Breaks large applications down into smaller executable components ● Easy to maintain and test ● Loosely coupled and can be deployed independently ● Can be combined with serverless architecture (AWS Fargate) Microservices 6 Image Source: hackernoon.com
  • 7. ● Develop applications that work on any OS ● Easy to share applications among teams ● Easy to scale across multiple servers ● Large applications can be broken into multiple containers - one for each microservice ● Great solution for Cloud Computing ● Big community and library of Docker Images Why Use Docker 7 Image Source: docker.com
  • 8. ● Removes Dependency on Infrastructure ● Allows developers to focus on application development ● Microservices can be decoupled with different cloud services ● Usually more cost effective ● Probably covered in more depth in a Cloud Computing class Serverless 8 Image Source: aws.amazon.com
  • 9. Which of the following is an advantage of using Docker Containers over Virtual Machines? a) Multiple containers can be run on the same machine b) Uses the Host OS c) Can be shared as images d) all of the above Clicker Quiz
  • 10. Which of the following is an advantage of using Docker Containers over Virtual Machines? a) Multiple containers can be run on the same machine b) Uses the Host OS c) Can be shared as images d) all of the above Clicker Quiz
  • 11. 2. Using Docker CLI Working with Gitlab to work in a DevOps environment with our existing Github Repositories
  • 12. Install Docker Engine Install Docker Engine • Docker Engine is available on a variety of Linux platforms, macOS and Windows 10 through Docker Desktop
  • 13. There are many publicly available images that we can use to work with Docker. The example below pulls a hello-world image using the docker pull command: Pull An Image 13 [ ~ ]docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d Status: Downloaded newer image for hello-world:latest docker.io/library/hello-world:latest [ ~ ]docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 12 months ago 13.3kB
  • 14. To create a container from an image we can use the docker create command Create A Container 14 [ ~ ]docker create hello-world 2ffd5f2c5a7562fbf1d7b89a14c11a52e5843dd7938f380a8cd53f3952da99de
  • 15. To run a container we can use the docker container start command to start a container. The -i runs the container interactively and allows us to see the output Run A Container 15 [ ~ ] docker container start -i 2ffd5f2c5a7562fbf1d7… Hello from Docker! This message shows that your installation appears to be working correctly.
  • 16. There is a shortcut for building a container from an image and running it with the docker run command. This will create a new container for an image and run it: Run An Image 16 [ ~ ] docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly.
  • 17. To see what images are already installed on your machine you can use the docker image ls command. We can see our hello-world image below: List Images 17 [ ~ ]docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest f63181f19b2f 13 hours ago 72.9MB hello-world latest bf756fb1ae65 12 months ago 13.3kB
  • 18. To list the containers that we have built, we can use the docker container ls command. The -a flag allows us to see both stopped and running containers. There are two containers below, one that was built with the docker create command and the other that was built with docker run: List Containers 18 [ ~ ]docker container ls –a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5017fd2b94c2 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago stoic_nobel 5f0cea57eacf ubuntu "bash" 10 minutes ago Exited (127) 8 minutes ago condescending_neumann 2ffd5f2c5a75 hello-world "/hello" 14 minutes ago Exited (0) 13 minutes ago hungry_mclaren
  • 19. Running containers interactively allows you to run commands inside the container if it supports it. We can use the openjdk image that we used before: Running Interactively 19 This allows us to execute java commands line by line in a Java shell [ ~ ]docker run -it openjdk Unable to find image 'openjdk:latest' locally latest: Pulling from library/openjdk a73adebe9317: Pull complete 8b73bcd34cfe: Pull complete 1227243b28c4: Pull complete Digest: sha256:7ada0d840136690ac1099ce3172fb02787bbed83462597e0e2c9472a0a63dea5 Status: Downloaded newer image for openjdk:latest Jan 21, 2021 4:48:58 PM java.util.prefs.FileSystemPreferences$1 run INFO: Created user preferences directory. | Welcome to JShell -- Version 15.0.2 | For an introduction type: /help intro jshell> System.out.println(”hello world"); Hello world
  • 20. To see what containers are currently running, we can use the docker ps command. This is useful when you are running containers in the background. List Running Processes 20 [ ~ ]docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 55e4a7c3ddcc openjdk "jshell" 11 seconds ago Up 10 seconds affectionate_kowalevski
  • 21. Interactive shell [ ~ ] docker run -it ubuntu bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 83ee3a23efb7: Pull complete db98fc6f11f0: Pull complete f611acd52c6c: Pull complete Digest: sha256:703218c0465075f4425e58fac086e09e1de5c340b12976ab9eb8ad26615c3715 Status: Downloaded newer image for ubuntu:latest root@5f0cea57eacf:/#
  • 22. How can you run a container? a) docker container start [ContainerID] b) docker run [ContainerID] c) docker run [ImageID] d) none of the above Clicker Quiz
  • 23. How can you run a container? a) docker container start [ContainerID] b) docker run [ContainerID] c) docker run [ImageID] d) none of the above Clicker Quiz
  • 24. 3. Dockerize An Application Dockerize your own application in a custom docker image
  • 25. Revisiting the Calculator TestRepo 25 Let’s dockerize the code in the repository we created in Lecture 6. It has a calculator class and two JUnit tests for add and subtract.
  • 26. Adding A Dockerfile 26 To create a custom docker image. We need to create a Dockerfile. The dockerfile specifies how our image should be built. Dockerfile
  • 27. Dockerfile Syntax 27 ● From - The base image to use ● Run - Runs commands when building the docker image ● Workdir - Specifies the directory that commands are run from ● User - Switches users ● Copy - Copies Files ● CMD - Runs commands when running the container Dockerfile
  • 28. Dockerfile Explained 28 1. Use the OpenJDK image to have a pre- configured java environment 2. Add a new user “ojdk” that we will be using for executing scripts 3. Create a directory that will contain our files and give permission to our user 4. Change the working directory to the directory we created 5. Copy the java and junit files 6. Switch to user ojdk 7. Compile all of our code 8. Copy files to the working directory and give permissions to ojdk 9. Run all tests Dockerfile
  • 29. To build a docker image using a Dockerfile we can use the docker image build command and provide it the directory where the Dockerfile exists. The --tag option allows us to name and tag the docker image. Build A Docker Image 29
  • 30. We can use the docker run command to run our image and we can see that our tests are being run: Run Our Custom Image 30
  • 31. We can use docker run -it to run our image interactively and open a bash shell in our working directory: Run Interactively 31
  • 32. Docker Compose File 32 Docker Compose files can be used to run multiple services at once and is great once you have many microservices as part of your application. For our project we create a single service calculator that ● is built using a custom Dockerfile ● tagged with the name calculator ● restarts unless it is stopped docker-compose.yml
  • 33. To run our docker compose file, we use the docker-compose up command. This builds all images and and runs and containers. Run Docker Compose 33
  • 34. The calculator container will keep restarting unless its stopped. To stop all services, we can use the docker-compose down command: Stop Containers 34
  • 35. To remove all unused docker resources, we can use the docker system prune command with the --all flag: Clean Up 35
  • 36. What’s the difference between the RUN and CMD commands in the Dockerfile? a) CMD is executed when images are being built while RUN is executed when containers are started b) RUN is executed when images are being built while CMD is executed when containers are started c) There is no difference between RUN and CMD d) There is no difference between RUN and CMD unless the container is started interactively Clicker Quiz
  • 37. What’s the difference between the RUN and CMD commands in the Dockerfile? a) CMD is executed when images are being built while RUN is executed when containers are started b) RUN is executed when images are being built while CMD is executed when containers are started c) There is no difference between RUN and CMD d) There is no difference between RUN and CMD unless the container is started interactively Clicker Quiz