Containerization.
Docker introduction
Agenda
• Containerization and virtualization
• Install Centos
• Install Docker
• Download images
• Images and containers
• Building Docker Images with Dockerfiles
Containerization&
Virtualization
Containerization and virtualization
Containerization and virtualization
Containerization and virtualization
Containerization and virtualization
Containerization and virtualization
Containerization and virtualization
Install Centos
Install Centos
[Link]
Install Centos
Install Centos
Setup Centos
Click ESC
# Type
Linux text
Set Host
Set Region
Set HDD
Set Network
Setup Centos
disable iptables, selinux
# service firewalld stop
systemctl stop [Link]
# disable firewalld
systemctl disable firewalld
# See status
systemctl status firewalld
Setup Centos
# disable SELinux
setenforce 0
# Edit file
vi /etc/sysconfig/selinux
# change SELINUX=enforcing
# to SELINUX=disable
Connect from Windows
Download PuTTY
[Link]
Install Docker
Install Docker
# update repository epel
yum -y install epel-release
# check
yum –y provides docker
# install docker (from Centos Repository)
yum –y install docker
# Install from docker repo !!!
curl -fsSL [Link] | sh
-f, --fail Fail silently (no output at all) on HTTP errors
-S, --show-error Show error even when -s is used
-L, --location Follow redirects
-s, --silent Silent mode
Install Docker
# start docker
systemctl start docker
# add to auto-start
systemctl enable docker
Download
images
Download images
# download image from docker hub
docker pull <image name>
# download last version centos 7
docker pull centos
# download linux+java
docker pull openjdk
Download images
[Link]
# download linux+openjdk from docker hub
docker pull openjdk:8-jdk
Download images
Images&Containers
Images and containers
Images and containers
# view images list
docker images
# container list (-a all, without -a only running)
docker ps -a
# run/create new container
docker run -it alpine
# create and executing container (and download)
docker run --name java8 -t -i openjdk:8-jdk
-t Allocate a pseudo-tty
-I Keep STDIN open even if not attached
Images and containers
Images and containers
# create/running new container from image
# 8080 inside port in container
# 8282 port on linux machine
docker run --name dashrest8 -t -i -p 8282:8080 openjdk:8-jdk
# Type in windows browser ([Link] - linux machine)
[Link]
# check by curl
curl -i [Link]
Copy files
scp (secure copy) command in Linux system
pscp (putty secure copy) command is an SCP protocol implementation where we
can transfer and copy files and folders securely over a network with the SSH
connection
# copy file to linux (use scp command)
[Link] [Link] root@[Link]:/root/[Link]
# Copy file to container
docker cp /root/[Link] dashrest8:/[Link]
Images and containers
# start existing container
docker start <id/name>
# view running containers
docker ps
# run bash on a running container
docker exec -it dashrest8 bash
# stop container
docker stop <id/name>
Check by curl
# login
curl -i -X POST --data "name=admin&password=qwerty"
[Link]
# update token lifetime
curl -i -X PUT -d "token=PVJ7UC90KIA5WSKK0WFFYDVA3SHPS8SB&time=750000"
[Link]
# get token lifetime
curl -i -X GET [Link]
Images and containers
# create new image
# docker commit -m “message" -a “author" <id> <new_name>
docker commit 2e52 imagedashrest8
# delete container
docker rm <id/name>
# delete image
docker rmi <id/name>
Building
Docker Images
with Dockerfiles
Dockerfiles
Dockerfile – the file describes the software that will be packaged into an image.
Create Dockerfile
# Create a new directory
mkdir mydockerbuild
# Copy the [Link] file to the folder
cp [Link] mydockerbuild/
# This directory serves as the "context" for the build.
# Enter the created directory.
cd mydockerbuild
# Create a Dockerfile in the directory
touch Dockerfile
Dockerfile
Add the following lines
# Base image import
# FROM – Select the base image to build the new image on top of
FROM openjdk: 8-jdk
# Import '[Link]' into '/ server /' folder
# ADD – Defines files to copy from the Host file system onto the Container
ADD [Link] /server/
# Set the working directory to '/server/’
# WORKDIR – Define the default working directory for the command
# defined in the "ENTRYPOINT" or "CMD" instructions
WORKDIR /server/
Dockerfile
#CMD – This is the command that will run when the Container starts
# CMD java -jar [Link]
CMD ["java", "-jar", "[Link]"]
# Expose port 8080
EXPOSE 8080
#ENTRYPOINT – Sets the default application used
# every time a Container is created from the Image.
Build image
# build the new image using the command docker build <path>
# space dot !!!
docker build -t dashrestimage .
# list of local images on the system
docker images
# run the command
docker run --name dashrest8 -t -i -p 8282:8080 dashrestimage
The option -p 8282:8080 exposes the Container port 8080 as the Host port 8282
to the world
Dockerfile
FROM openjdk:8-jdk
ADD [Link] /server/
WORKDIR /server/
EXPOSE 8080
CMD ["java", "-jar", "[Link]"]
Dockerfile. Example
# The line below states we will base our new image on the Latest Official Ubuntu
FROM ubuntu:latest
# Identify the maintainer of an image
LABEL maintainer="myname@[Link]"
# Update the image to the latest packages
RUN apt-get update && apt-get upgrade -y
# Install NGINX to test.
RUN apt-get install nginx -y
# Expose port 80
EXPOSE 80
# Last is the actual command to start up NGINX within our Container
CMD ["nginx", "-g", "daemon off;"]
Dockerfile commands
# ADD – Defines files to copy from the Host file system onto the Container
ADD ./local/[Link] /etc/service/[Link]
# CMD – This is the command that will run when the Container starts
CMD ["nginx", "-g", "daemon off;"]
CMD Hello World
# COPY - copies files and folders to the container.
COPY . ./app
# ENTRYPOINT – Sets the default application used every time a Container is
created from the Image. If used in conjunction with CMD, you can remove the
application and just define the arguments there
ENTRYPOINT ["python", "./app/my_script.py", "my_var"]
Dockerfile commands
# ENV – Set/modify the environment variables within Containers created from
the Image.
# EXPOSE – Define which Container ports to expose
EXPOSE 80
# FROM – Select the base image to build the new image on top of
FROM ubuntu:latest
# LABEL maintainer – Optional field to let you identify yourself as the maintainer
of this image.
LABEL maintainer=someone@[Link]"
Dockerfile commands
#RUN – Specify commands to make changes to your Image and subsequently the
Containers started from this Image.
RUN apt-get update && apt-get upgrade -y && apt-get install -y nginx && rm -rf
/var/lib/apt/lists/*
#USER – Define the default User all commands will be run as within any
Container created from your Image.
USER docker
Dockerfile commands
#VOLUME – Creates a mount point within the Container linking it back to file
systems accessible by the Docker Host.
VOLUME /var/log
#WORKDIR – Define the default working directory for the command defined in
the “ENTRYPOINT” or “CMD” instructions
WORKDIR /home
References
Reference documentation
[Link]
Dockerhub
[Link]
Orientation and setup
[Link]
Docker overview
[Link]
Docker 101 Tutorial
[Link]