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

Containerization p1 l2

Uploaded by

hamody.s21ultra2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Containerization p1 l2

Uploaded by

hamody.s21ultra2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Part 1, Lecture 1, Introduction

Containerization of Applications
Alexander Hultnér, 2022
Lecture 2
Examinerande kursmoment
Inlämning 1, Paketera mindre applikation (IG-G)
Inlämning 2, Större projekt (IG-G-VG)
Tenta (IG-G-VG)
CLI / Shell basics
Print current directory, pwd
Change directory, cd /path/to/directory/
List directory contents, ls
Create directory, mkdir directory_name
Create file, touch file_name
Move/rename file, mv old_name new_name
Copy file, cp file_name copy_name
Remove file, rm file_name
Remove directory (recursive), rm -r directory_name
Show file tree, tree
More: https://www.pluralsight.com/guides/beginner-linux-navigation-manual
CLI / Shell keyboard shortcuts
Prompt: Interactive command line
^ : CTRL

^D : Exit (shell, prompt)

^C : Close currently running program


CLI / Shell flags
Named flags, dash-dash, --file
Short flags, dash, -f
command --flag flag_argument command_arugment

command --flag="flag arugment" command_arg

command -f flag_argument command_arg


Docker Structure
Dockerfile
Specifications for how to build an image
Image
Build image from dockerfile
Container
Run container from images
Layers
Commands
Build image, docker build
Run a new container, docker run
See running containers, docker ps
Start existing container, docker start
See resource usage, docker stats
Rename container docker container rename
Stop a running container, docker stop
Remove container, docker rm
Find more options, --help -flag
Run and remove
Run a python2 container
Start a shell
Automatically remove container on exit
docker run --rm -it python:2 bash
Create a Dockerfile
# Comments start with a # in Dockerfiles
# Select a base image to build our image from
FROM python:3.10-slim

# Run a command when building the image


RUN pip install flask

# Set working directory with WORKDIR


WORKDIR /app/

# COPY files from local file system to docker image


COPY app/app.py app.py

# What to run when starting a container from image


CMD python app.py
Build a docker image
Docker images are built using dockerfiles.
docker build .
Will build an image with current pwd as context from Dockerfile
docker build --tag my-image . --file my.Dockerfile
Will build a docker image named "my-image" with . as context directory from
my.Dockerfile

docker build --tag img-2 app/src/ --file other.Dockerfile


Will build a docker image named "img-2" with app/src/ as context directory
from other.Dockerfile
Run/create container
A container is created from an image when running it
docker run my-image -p 80:80
Run my-image and map port 80 on my-image
docker run --name my-container my-image -p 80:80

docker container rename my-container new-name


Flask application, exercise 1
Build a small Flask application
Create a dockerfile for it
Run pip install Flask in the docker file
Copy over python file(s)
EXPOSE 12345 to expose port 12345

Start container
Python requirements
Virtual environments
Freeze requirements to file, pip freeze
Install from requirements file, pip install -r
Flask exercise, run existing app
Package and run a slightly larger Flask-application
Download src_p1l2.zip
Build a dockerfile based on python:3.9-slim
Set workdir
COPY over files

Install requirments file pip install -r requirements.txt


EXPOSE port 5000

Set start cmd to python run.py


Build image, run conatiner, visit site
Dockerfile, more
ARG and ENV
ARG , Build time arguments

ENV , Environment variables, runtime

https://vsupalov.com/docker-arg-env-variable-guide/
EXPOSE , port to expose
Use -p to map port, format: hostPort:containerPort
-P to auto-expose to random port

docker container port $(docker container list -q)


Dockerignore
.dockerignore

Exclude files from context


More docker commands
List images, docker image ls
Inspect image, docker image inspect
Save image to file, docker image save image_name -o out.tar
See log docker logs container_name
Attach running container, docker attach
See port mappings docker container port $(docker container list -q)
Flask application, exercise 3
Contiune working on your own Flask app from exercise 1
Create a requirements file like the sample app from exercise 2
Remove pip install Flask from your dockerfile
Install dependencies from requirements.txt instead
Build an image and run the container, make sure you can visit the site
Bonus: Add more pages your flask app
Bonus: Add use a another third-party python library in your flask app, add your
requirements and make sure it still works in docker.

You might also like