0% found this document useful (0 votes)
59 views21 pages

04 Dockerfile

The document provides instructions for using Docker commands like docker images, docker search, docker pull, docker run, docker container ls and docker stop/start to pull and run Docker containers. It demonstrates how to run containers with volumes mounted, ports exposed, and names assigned. The examples show interacting with Alpine and PHP containers on the local file system and network.
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)
59 views21 pages

04 Dockerfile

The document provides instructions for using Docker commands like docker images, docker search, docker pull, docker run, docker container ls and docker stop/start to pull and run Docker containers. It demonstrates how to run containers with volumes mounted, ports exposed, and names assigned. The examples show interacting with Alpine and PHP containers on the local file system and network.
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/ 21

Dockerfile

dr Mihajlo Savić

1/21
Docker i fajl sistem

● Primjer sa dva sloja iz Docker dokumentacije


– Podržava do 128 slojeva u "lowerdir" sloju
● Svaki sloj jedinstven ID
– Nema potrebe za ponovnim skidanjem postojećih slojeva
– Zanemariv pad performansi - sve koristi slojeve
2/21
docker images
● Prikazuje listu slika (images) kontejnera
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sfm latest 27aa2b54d30c 12 days ago 430MB
sfm v2 27aa2b54d30c 12 days ago 430MB
alpine latest 6dbb9cc54074 3 weeks ago 5.61MB
...

3/21
docker search
# docker search php
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
php While designed for web development, the PHP … 5902 [OK]
phpmyadmin/phpmyadmin A web interface for MySQL and MariaDB. 1126 [OK]
adminer Database management in a single PHP file. 547 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 214 [OK]
webdevops/php-nginx Nginx with PHP-FPM 197 [OK]
php-zendserver Zend Server - the integrated PHP application… 193 [OK]
webdevops/php-apache-dev PHP with Apache for Development (eg. with xd… 141 [OK]
webdevops/php-apache Apache with PHP-FPM (based on webdevops/php) 117 [OK]
bitnami/php-fpm Bitnami PHP-FPM Docker Image 112 [OK]
phpunit/phpunit PHPUnit is a programmer-oriented testing fra… 82 [OK]
nazarpc/phpmyadmin phpMyAdmin as Docker container, based on off… 61 [OK]
phpipam/phpipam-www phpIPAM is an open-source web IP address man… 50
thecodingmachine/php General-purpose ultra-configurable PHP images 35 [OK]
circleci/php CircleCI images for PHP 33
erikdubbelboer/phpredisadmin Simple web interface to manage Redis databas… 28 [OK]
chialab/php Adding common PHP extensions to some of the … 27 [OK]
bitnami/phpmyadmin Bitnami Docker Image for phpMyAdmin 24 [OK]
devilbox/php-fpm PHP-FPM Docker images based on original PHP … 22

4/21
docker search
# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14818 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2024 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 813 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 185
linuxserver/nginx An Nginx container, brought to you by LinuxS… 143
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 123 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 106 [OK]
bitnami/nginx Bitnami nginx Docker Image 95 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 94 [OK]
jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 89 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 67 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 53 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 51
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 33
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 21 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 17
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13

5/21
docker pull
# docker search alpine

NAME DESCRIPTION STARS OFFICIAL AUTOMATED

alpine A minimal Docker image based on Alpine Linux… 7397 [OK]

# docker pull alpine

Using default tag: latest

latest: Pulling from library/alpine

540db60ca938: Pull complete

Digest: sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f

Status: Downloaded newer image for alpine:latest

docker.io/library/alpine:latest

# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

alpine latest 6dbb9cc54074 3 weeks ago 5.61MB

6/21
docker run
# uname -a
Linux sophia 5.4.0-66-generic #74-Ubuntu SMP Wed Jan 27 22:54:38 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
# docker run -it alpine
/ # uname -a
Linux c822ab1ed787 5.4.0-66-generic #74-Ubuntu SMP Wed Jan 27 22:54:38 UTC 2023 x86_64 Linux
/ # exit
# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c822ab1ed787 alpine "/bin/sh" 34 seconds ago Exited (0) 9 seconds ago kind_sanderson
# docker container rm c822ab1ed787
c822ab1ed787
# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# docker run -it --rm alpine
/ # exit
# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#

7/21
docker run
# docker run php -r 'echo "3+5=". 3+5 ."\n";'
3+5=8
# docker run -it alpine
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
540db60ca938: Pull complete
Digest: sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f
Status: Downloaded newer image for alpine:latest
/ # ip addr ls
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
169: eth0@if170: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit

8/21
docker i izmjene fajl sistema
# docker run -it alpine
/ # ls -l /root
total 0
/ # mkdir /root/evome
/ # ls -l /root
drwxr-xr-x 2 root root 4096 Apr 28 21:26 evome
/ # exit
# docker run -it alpine
/ # ls -l /root
total 0
# docker run -it -v $(pwd):/proba alpine
/ # mkdir /proba/da_vidimo
/ # ls -l /proba
drwxr-xr-x 2 root root 4096 Apr 28 21:32 da_vidimo
/ # exit
# docker run -it -v $(pwd):/proba alpine
/ # ls -l /proba
drwxr-xr-x 2 root root 4096 Apr 28 21:32 da_vidimo
/ # exit
# ls -l
drwxr-xr-x 2 root root 4096 Apr 28 23:32 da_vidimo

9/21
docker, pristup mreži i imena kontejnera
# docker run -v $(pwd):/srv -p 8080:8080 php -S 0.0.0.0:8080 -t /srv
# docker run -v /neki/drugi/direktorijum:/srv -p 8081:8080 php -S 0.0.0.0:8080 -t /srv
# docker run -v $(pwd):/srv -d --name=php8081 -p 8081:8080 php -S 0.0.0.0:8080 -t /srv
0623c12eb56b8a1b73432c3439cc26498e625b3f259f31325b033e06a82833d6
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0623c12eb56b php "docker-php-entrypoi…" 4 seconds ago Up 3 seconds 0.0.0.0:8081->8080/tcp php8081
# docker stop php8081
php8081
# docker run -v $(pwd):/srv -d --name=php8081 -p 8081:8080 php -S 0.0.0.0:8080 -t /srv
docker: Error response from daemon: Conflict. The container name "/php8081" is already in use by container
"0623c12eb56b8a1b73432c3439cc26498e625b3f259f31325b033e06a82833d6". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
# docker start php8081
php8081
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0623c12eb56b php "docker-php-entrypoi…" 4 minutes ago Up 5 seconds 0.0.0.0:8081->8080/tcp php8081
# docker stop php8081
php8081
# docker rm php8081
php8081

10/21
Dockerfile
● Dockerfile je fajl koji definiše korake neophodne za
pravljenje slike kontejnera.
● Osnovne ključne riječi unutar Dockerfile:
– FROM definiše sliku iz koje izvodimo našu sliku za kontejner
– MAINTAINER definiše ime i prezime, kao i email osobe
zadužene za održavanje
– RUN izvršava komandu
– ADD dodaje (kopira) fajlove iz host fajl sistema u sliku za
kontejner (npr. naša aplikacija, itd)
– ENV podešava environment varijable
11/21
Dockerfile
● Korisne za automatizaciju jer ne želimo da kucamo iste stvari u
komandnoj liniji bez potrebe
– EXPOSE omogućava pristup određenim mrežnim portovima
– VOLUME omogućava pristup dijelovima fajl sistema hosta iz kontejnera
– LABEL labela za docker image
● Pokretanje programa pri pokretanju kontejnera
– CMD definiše default komandu (i parametre) koja će biti izvršena pri
pokretanju kontejnera (korisnik je može promijeniti pri pozivu)
– ENTRYPOINT slično kao CMD ali nije dozvoljen override
– WORKDIR direktorijum u kojem će se CMD izvršavati
– USER korisnični nalog pod kojim će se izvršavati

12/21
Dockerfile
FROM php:latest
MAINTAINER Mihajlo Savic [email protected]

RUN apt-get update


RUN apt-get -y upgrade
RUN apt-get install -y imagemagick

13/21
docker build
# docker build -t "phpmagick:latest" .

Sending build context to Docker daemon 3.072kB


Step 1/5 : FROM php:latest
---> d88df285a5d9
Step 2/5 : MAINTAINER Mihajlo Savic [email protected]
---> Running in 7dcfd025b93a
Removing intermediate container 7dcfd025b93a
---> 652da5fb0197
Step 3/5 : RUN apt-get -y update
---> Running in 083aa1808645
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
...
mnogo linija i dešavanja
...
Processing triggers for libc-bin (2.28-10) ...
Removing intermediate container e91dda2aab90
---> 327dc5b8a5f6
Successfully built 327dc5b8a5f6
Successfully tagged phpmagick:latest

14/21
docker build
FROM phpmagick:latest
MAINTAINER Mihajlo Savic [email protected]
RUN apt-get install -y wget

# docker build -t "phpmagickwget:v1" .


Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM phpmagick:latest
---> 327dc5b8a5f6
Step 2/5 : MAINTAINER Mihajlo Savic [email protected]
---> Running in 8867a468817d
Removing intermediate container 8867a468817d
---> 4b2e434c6296
...
mnogo linija i dešavanja
...
Processing triggers for libc-bin (2.28-10) ...
Removing intermediate container 589eec908453
---> fc887bfc694b
Successfully built fc887bfc694b
Successfully tagged phpmagickwget:v1

15/21
docker build
# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

phpmagickwget v1 fc887bfc694b 13 seconds ago 556MB

phpmagick latest 327dc5b8a5f6 4 minutes ago 553MB

16/21
docker history
# docker history fc887bfc694b

IMAGE CREATED CREATED BY SIZE COMMENT

fc887bfc694b 7 minutes ago /bin/sh -c apt-get install -y wget 2.6MB

4b2e434c6296 7 minutes ago /bin/sh -c #(nop) MAINTAINER Mihajlo Savic … 0B

327dc5b8a5f6 11 minutes ago /bin/sh -c apt-get install -y imagemagick 108MB

1fa6678de09a 12 minutes ago /bin/sh -c apt-get -y upgrade 5.01MB

32f4115755d6 12 minutes ago /bin/sh -c apt-get update 17.6MB

652da5fb0197 12 minutes ago /bin/sh -c #(nop) MAINTAINER Mihajlo Savic … 0B

d88df285a5d9 3 weeks ago /bin/sh -c #(nop) CMD ["php" "-a"] 0B

<missing> 3 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-php-e… 0B

...

<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:c855b3c65f5ba94d5… 69.3MB

17/21
docker alati
alias dive="docker run -ti --rm -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive"

dive phpmagickwget:v1

alias dfimage="docker run -v /var/run/docker.sock:/var/run/docker.sock --rm alpine/dfimage"

dfimage -sV=1.36 phpmagickwget:v1

docker run --rm mplatform/mquery eclipse-temurin:17-jre-alpine

Image: eclipse-temurin:17-jre-alpine (digest:


sha256:f59c1acc26975859545eabb2051f4b9a41d5ef278aad9dfe42bdb0aff5611613)
* Manifest List: Yes (Image type: application/vnd.docker.distribution.manifest.list.v2+json)
* Supported platforms:
- linux/amd64

18/21
Multi-stage build
● Klasično koristimo dva (ili više) kontejnera
– Jedan za build
– (Jedan za testiranje)
– Jedan za izvršavanje
● Zašto?
– Jer ne želimo da nam u fazi izvršavanja bude prisutan kompletan
SDK i pomoćni alati (i testovi)
● Napravimo tri Dockerfile fajla
– Može. Nije problem, jedino što nije elegantno.
– Mi volimo da smo elegantni ;)
19/21
Multi-stage Dockerfile
FROM eclipse-temurin:17-jdk-jammy as build
WORKDIR /app
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
COPY ./src ./src
RUN ./mvnw clean install -Dmaven.test.skip=true

FROM eclipse-temurin:17-jre-alpine as runtime


EXPOSE 8080
WORKDIR /app
COPY --from=build /app/target/app.jar /app
ENTRYPOINT ["java","-jar","app.jar"]

20/21
Hvala na pažnji

PITANJA?

21/21

You might also like