0% found this document useful (0 votes)
8 views

Docker Example

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Docker Example

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

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
https://www.centos.org/download/
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 firewalld.service

# 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
https://www.putty.org/
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 https://get.docker.com/ | 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
https://hub.docker.com/

# 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 (192.168.153.131 - linux machine)


http://192.168.153.131:8282/tokenlifetime

# check by curl
curl -i http://192.168.153.131:8282/tokenlifetime
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)


pscp.exe dashrest.jar [email protected]:/root/dashrest.jar

# Copy file to container


docker cp /root/dashrest.jar dashrest8:/dashrest.jar
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"
http://192.168.153.131:8282/login

# update token lifetime


curl -i -X PUT -d "token=PVJ7UC90KIA5WSKK0WFFYDVA3SHPS8SB&time=750000"
http://192.168.153.131:8282/tokenlifetime

# get token lifetime


curl -i -X GET http://192.168.153.131:8282/tokenlifetime
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 dashrest.jar file to the folder


cp dashrest.jar 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 'dashrest.jar' into '/ server /' folder


# ADD – Defines files to copy from the Host file system onto the Container
ADD dashrest.jar /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 dashrest.jar
CMD ["java", "-jar", "dashrest.jar"]

# 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 dashrest.jar /server/

WORKDIR /server/

EXPOSE 8080

CMD ["java", "-jar", "dashrest.jar"]


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="[email protected]"

# 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/config.file /etc/service/config.file

# 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 [email protected]"
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
https://docs.docker.com/reference/

Dockerhub
https://hub.docker.com/

Orientation and setup


https://docs.docker.com/get-started/

Docker overview
https://docs.docker.com/get-started/overview/

Docker 101 Tutorial


https://www.docker.com/101-tutorial

You might also like