docker container overview
Post on 22-Apr-2022
18 Views
Preview:
TRANSCRIPT
Docker Container OverviewMizan Rahman
CIS280: Running and Scaling Applications using Docker and Kubernetes
Outline
● Overview of Docker Container
● Running Hello World in Docker
● Running a Docker Container in Detached Mode
▪ Building a Docker Image with a Dockerfile
▪ Package an Application Inside a Container
CIS280: Running and Scaling Applications using Docker and Kubernetes
CIS280: Running and Scaling Applications using Docker and Kubernetes
Processes
Process Process ProcessProcess
code
Process
Process
Another
Computer
Another OSAnother OS
Another OSAnother OS
Another
Computer
Another
Computer
Another
Computer
Docker EngineDocker
Engine
Process
Exe
Docker
Image
CIS280: Running and Scaling Applications using Docker and Kubernetes
Docker Container Key Conceptscode
System lib
System tools
Dockerfile
Settings
Docker
build
Docker
run
Docker
run
What is a Container?
• 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 in user space:
▪ code,
▪ runtime,
▪ system tools,
▪ system libraries and
▪ settings.
https://www.docker.com/resources/what-container
CIS280: Running and Scaling Applications using Docker and Kubernetes
Container Advantage
• Docker containers that run on Docker Engine:
• Standard: Docker created the industry standard for containers, so
they could be portable anywhere
• Lightweight: Containers share the machine’s OS system kernel
and therefore do not require an OS per application, driving higher
server efficiencies and reducing server and licensing costs
• Secure: Applications are safer in containers and Docker provides
the strongest default isolation capabilities in the industry
CIS280: Running and Scaling Applications using Docker and Kubernetes
CIS280: Running and Scaling Applications using Docker and Kubernetes
Docker architecture
Docker Hub (Image Repo)
CIS280: Running and Scaling Applications using Docker and Kubernetes
• Please create a docker hub account if you do not have already.
• https://hub.docker.com/signup
Running Hello World in Docker• docker run hello-world
• docker ps
• docker container ls
• docker container --help
• docker ps -a
• docker images
• (stored in:/var/lib/docker/image/overlay2/imaged/content)
• ls /var/lib/docker/image/overlay2/imagedb/content/sha256
• docker info
• (Docker root dir: /var/lib/docker)
• ls /var/lib/docker
• ls /var/lib/docker/containers
• docker logs hello-world
• docker inspect hello-world
CIS280: Running and Scaling Applications using Docker and Kubernetes
Port Mapping (Host:Container)
Port
80
Port
8080
eth1 (192.168.1.25)
Nginx Container
http.server Bridge
CentOS VM
Windows /Mac/linux host OS (192.168.1.20)
docker run -p 8080:80
CIS280: Running and Scaling Applications using Docker and Kubernetes
Running a Docker Container in
Detached Mode• docker run -d -p 8080:80 --name nginxcontainer nginx
• #Open in the browser http://192.168.1.25:8080/ (eth1 ip addr of MasterVM)
• docker ps
• # Get to bash (Bourne Again Shell)
• docker exec -it nginxcontainer /bin/bash
• Or you can Get to the sh (Bourne shell)
docker exec -it nginxcontainer /bin/sh
• ls
• pwd
• cat /usr/share/nginx/html/index.html
• exit
• docker ps
• docker stop nginxcontainer
• docker rm nginxcontainer
-d : Detached, Run container in the background
-a=[] : Attach to `STDIN`, `STDOUT`, `STDERR`
-t : Allocate a pseudo-tty (TeleTYpewriter)
-i : Keep STDIN open even if not attached
-p : Publish a container’s port(s) to the host
-m : Memory limit
CIS280: Running and Scaling Applications using Docker and Kubernetes
Building a Docker Image with a Dockerfile
• Create the following text file named Dockerfile in an empty working
directory:
• mkdir hello-app
• cd hello-app
• vi Dockerfile
FROM alpine
CMD echo Hello CIS280 !
• docker build -t hello-app . # Provide a name and tag
• docker images #This will get the image id
• docker run hello-app # Start and run the container
• docker ps -a # Container exited as it is finished it purpose
• docker rm hello-app # Removed the stopped container
• docker container prune # Delete any stopped container
CIS280: Running and Scaling Applications using Docker and Kubernetes
Package an Application Inside a Container
#Package an Application Inside a Container
#crate a index.html for a webserver.
cd ..
mkdir webserver
cd webserver
echo "<html><h1>Hello CIS280</h1></html>" > index.html
vi Dockerfile
#copy following in to the Dockerfile
FROM centos:latest
RUN yum -y install httpd
COPY index.html /var/www/html/
CMD ["usr/sbin/httpd", "-D", "FOREGROUND"]
EXPOSE 80
Build and Run the Application • #Build docker image
• docker build -t webserver:v1 .
• docker images
• docker run -d -p 8080:80 --name webserver webserver:v1
• #Open in the browser http://192.168.1.25:8080/ (eth1 ip addr of MasterVM)
• docker exec -it webserver /bin/bash
• cat /var/www/html/index.html
• #or incase image has issue you can run and get the to the container
• docker run -it webserver /bin/bash
• docker stop webserver
• docker rm webserver
• docker container prune
Dockerfile Commands• #Dockerfile Commands
• #Get base image (or From scratch)
• FROM centos:latest
• # Set the working directory.
• WORKDIR /var/www/html/
• #Get executed during the build of the image
• RUN yum -y install httpd
• #expose the containter port
• EXPOSE 80
• #copy local files into the container wroking directory
• COPY index.html .
• #executed inside the container
• CMD ["usr/sbin/httpd", "-D", "FOREGROUND"]
https://docs.docker.com/engine/reference/builder/
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
CIS280: Running and Scaling Applications
using Docker and Kubernetes
top related