Docker Notes
Building && Running APP
Contents
docker-container
> mkdir nginx-webapp
> cd nginx-webapp
> vi webapp.html
> <html><body>...first app..</body></html>
> vi nginx.conf #server conf file..
server{
root /www;
}
> vi Dockerfile # dockerfile is blueprint of our image
FROM nginx:latest #or version
ADD webapp.html /www/
ADD nginx.conf /etc/nginx/conf.d/default.conf
#EXPOSE 80
#EXPOSE 443
#CMD nginx -g "deamon off;" #to start nginx server
to start & test
>docker build . -t webapp-oasis:latest
>docker run webapp-oasis
>docker ps #to see docker running images..
to run as a background process
>docker run -d -p 84:80 webapp-oasis:latest #docker [deamon(background process)] [port] [hostport]:[containerport] [docker-image]:version
to test go to container ip address, and test:
http://192.168.1.153:84/webapp.html
accessing to container shell
> docker run -it ubuntu:latest /bin/bash (enter)
> [ubuntu@shell] >
> exit (to exit)
you can use all linux commands..
to remove interactive image:
> docker run -it --rm ubuntu:latest /bin/bash (enter)
> exit (to exit)
to see the running ubuntu process:
> docker ps -a | grep ubuntu
accessing to container shell - 2
> docker run -it ubuntu:latest /bin/sh (enter)
> [ubuntu@shell] > whoami
> exit (to exit)
accessing to running container shell
> docker ps #list process and pick up one
> docker exec -it my_http /bin/bash
> docker ls
> exit (to exit)
to run php from docker image..
> docker run -it --rm php:latest /bin/bash #if exist, remove first..
run a php script mounted in the container using the volume option
> docker run -it --rm -v "$(pwd)/challenge.php:/home/challenge.php" php:latest /bin/bash
> docker run -it --rm [mount] "$(pwd)/[source_file]:[target_address]" php:latest /bin/bash
installing software to a Docker Image...
> vi Dockerfile
FROM ubuntu:latest
# apt-get update : checks program updates
# apt-get install -y : ask yes/no for installation for new update
# --no-install-recommends vim : don't install lib update for vim
RUN apt-get update && apt-get install -y --no-install-recommends vim && apt-get clean
(save & exit)
> docker built . -t ubuntu-vim # it updates ubuntu and install vim..
> docker images
> docker run -it --rm ubuntu-vim /bin/bash # run
access container console logs ..
> docker logs -h # shows log options
> docker logs -f --tail 10 readmine
to see apache logs..
> docker run -d -p httpd
> docker ps
> docker logs -f xxxxxxx_images
docker container healthcheck
> CURL -I "http://localhost:8080" # it show header info about address
> CURL -I -f "http://localhost:8080"
> CURL -I "http://localhost:8080" || exit 1
#how to check in docker image
> vi Dockerfile
FROM httpd:latest
RUN apt-get update && apt-get install -y --no-install-recommends curl && apt-get clean
EXPOSE 80
# --interval=15s : every 15s check
# --retries=5 : if fail, 5 times retry
# --start-period=30s : when server run, 30s later start to check
# CMD : run that commands after CMD
HEALTHCHECK --interval=15s --retries=5 --timeout=30s --start-period=30s CMD curl -I -f "http://localhost:80" || exit 1
(save & exit)
> docker built . -t httpd-oasis
#docker run -d -p --name [new-image-alias] [docker-image]
> docker run -d -p --name http-my-oasis http-oasis
> docker ps #we will see healtcheck..
> docker logs -f httpd-my-oasis #to see the logs..
linking docker containers...
> docker run -d --name resuse-httpd httpd
# docker run -it --link "[container-name or id]:[alias]" php-apache-wget /bin/bash
> docker run -it --link "reuse-httpd:web" php-apache-wget /bin/bash
> wget "http://web:80"
> cat index.html
create a MySql(mysql) container linked to a phpMyAdmin(adminer) container
> docker run --name our-mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:latest # for details, go to docker mysql pages..
> docker run --link "our-mysql:db" -p 8080:8080 adminer
# goto browser
http://192.168.1.34:8080
docker-compose
docker compose :
install docker compose .. > docker-compose -v > ls
> vi docker-compose.yml version '3.1'
- services: containers
services :
tomcat1: #dockercomp_tomcat1 image:tomcat:lates ports: - 1002:80 - 1003:8080 - 1013:8080
tomcat2: #dockercomp_tomcat2 image:tomcat:lates ports: - 1002:80 - 1003:8080 - 1013:8080
environment:
TEST_ENV_VAR: test1234
- save & exit
> docker-compose up (enter) > docker ps #you see all images up
> docker exec -it dockercomp_tomcat2_1 /bin/bash
- to test environmentvariable
> echo $TEST_ENV_VAR #you'll see result
mysql && phpmyadmin with docker-compose.yml
> vi docker-compose.yml version '3.1' services:
db: image:mysql:latest restart:always environment: MYSQL_ROOT_PASSWORD:password adminer: image:adminer:latest restart:always ports: -8080:8080
(save&&exit)
> docker-compose up -d #background process > docker ps
check it via browser ..
docker networks : - all of the containers easily communicate with each other - networks communicate with IP's
> docker network ls # to see networks , bridge means : isolated networks
> docker network create --help
> \docker network create oasis_test_network > docker network ls > docker network prune #to remove unused networks > docker ps
lets create a network
> docker run -d nginx > docker-machine ssh default > vi docker-compose.yml db:
image:mysql:latest restart:always environment: MYSQL_ROOT_PASSWORD:password networks: -backend
adminer:
image:adminer:latest restart:always ports: -8080:8080 networks: -frontend
networks:
frontend: #ıf you dont give anything, it will be bridge driver:bridge backend: driver:bridge
default: name:oasis_test_network1
(save&exit)
> docker-compose down #stop first >docker-compose up
to use specific network
> docker network connect -h #for help > docker network connect [network_name] [docker_image] > docker network connect dockercompnetworks_frontend oasis_new_image
to inspect
> docker network inspect dockercompnetworks_frontend # you can see attached container as well
>docker-compose up -d #as a background process
Docker Compose Scaling Healing
> docker-compose up -d --scale nginx_4 (enter)
for loadbalance .. > docker run --rm -it --network scalevol_default ubuntu-dev bash
test > curl nginx/hostname #(enter) > curl nginx/hostname #(enter) > curl nginx/hostname #(enter)
it will show always different id..(load loadbalance) with auto healing
monitoring healty container : willfarrell/autoheal ..
> docker ps -f health=healthy > docker ps -f health=unhealthy -q #to list unhealthy images id.. > docker restart $(docker ps -f health=unhealthy -q) #to restart unhealthy process
..
deploy an apache web server(httpd) using compose, then scale it to 3
> vi docker-compose-httpd.yml version: '3.1'
services:
web: image: httpd:latest restart: always
(save&exit)
- notes: don't forget to put space after ": "!!
- > docker-compose -p [project-name] [file-save:-f] [filename]
> docker-compose -p challenge -f docker-compose-httpd.yml up -d #to deploy single instance
> docker-compose -p challenge -f docker-compose-httpd.yml up -d --scale web=3 #it starts 3 instances > docker ps
helpfull Docker-Compose Features
> docker-compose pull #updates images defined in a docker-compose file. > docker-compose restart #.. > docker-compose pause > docker-compose unpause > docker-compose start / stop
> docker-compose -h #shows available commands > docker -h #shows available commands
> docker-compose top > docker-compose logs -f > docker-compose logs -f httpd
> docker events > docker-compose events
> docker-compose rm > docker-compose ls
docker hub / registry
Standard syntax : <registry>/<namespace>/<repo-name>:<tag> i.e.: docker.io/library/openjdk:8-jdk
> docker login docker.io #to login docker hub #picks up / pull images from dockerhub > docker tag alpine:edge rasimsen/alpine:5mb-linux #tag local images to push dockerhub > docker push rasimsen/alpine:5mb-linux # it has been sent to dockerhub, check from hub.docker.com
> docker logout #to logout
docker hub registry alternatives
- self-hosted alternatives
** Generic private docker registry ** Portus
- Other hosted alternatives
** Quay.oi ** Azure container registry ** Amazon elastic container registry ** Google container registry ** Private docker registry ** Artifactory
Docker Swarm
Essentially it is a cluster of docker hosts that work together to deploy containers
The coordination involved between the docker hosts is most often referred to as orchestration
usefull containers
Alpine Linux
Alpine Linux is a Linux distribution built around musl libc and BusyBox. The image is only 5 MB in size and has access to a package repository that is much more complete than other BusyBox based images. This makes Alpine Linux a great image base for utilities and even production applications. Read more about Alpine Linux here and you can see how their mantra fits in right at home with Docker images.
https://hub.docker.com/_/alpine/
You need to keep in mind that Docker is still running inside a VM. The system paths are still relative to the VM, and not to your Mac. Only some folders are already mounted from your Mac into the VM. You can get an overview by running this command:
$ docker run --rm -it -v /:/vm-root alpine:edge ls -l /vm-root
How to use this image
Use like you would any other base image:
FROM alpine:3.7
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]
This example has a virtual image size of only 36.8MB. Compare that to our good friend Ubuntu:
FROM ubuntu:18.04
RUN apt-get update \
&& apt-get install -y --no-install-recommends mysql-client \
&& rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["mysql"]
This yields us a virtual image size of about 145MB image.