Docker, Dockerfile, and Docker-Compose (2020 ready!)
Learn Docker, Containers vs. Images, Dockerfile, Docker-Compose
With Practical Hands-On Exercises!
https://www.udemy.com/course/docker-and-docker-compose-hands-on-by-example/
Thomas Wiesner. Teaching over 60,000 Students about Development (https://www.facebook.com/tomscourses/)
- You will learn to use Docker with Hands-On Demos and Exercises
- Know the most important Docker-Run Flags and Everyday Use-Cases
- Be able to read, understand and write your own Dockerfiles
- Read and write your own docker-compose yaml files
- Understand the difference between Host-Volume and Named-Volume mounting
- Improve real-world docker-compose yaml files\
- Part 1 and 2: Introduction and install docker to desktop
- Part 3: Docker run and docker commands
- Part 4: Dockerfile and docker commands
- Part 5: Docker-compose. Volumes and docker-compose + dockerfile
- Part 6: Networks and docker, docker-compose
- Part 7: Examples
For example "MariaDB doc and env-variable": https://hub.docker.com/_/mariadb
https://docs.docker.com/compose/
For example "Quickstart: Compose and WordPress": https://docs.docker.com/compose/wordpress/
or in DocherHub: https://hub.docker.com/_/wordpress
dockerfile best practices: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
# show all containers
docker ps -a
# build image with tag (with Dockerfile)
docker build -t myphpapp .
# run docker-container from image ubuntu
docker run -it ubuntu /bin/bash
# start docker-container when it stops
docker start container_name
# attach docker-container (like exec + bash)
docker attach container_name
# stop docker-container
docker stop container_name
# remove docker-container
docker rm container_name
# remove image
docker rmi image_name_or_tag
# Run container from image 'ubuntu' in interactive mode (-it)
# with attached volume 'my-vol' (-v)
# with name linux1, detached
# and remove the container when it will stop (--rm)
docker run -v my-vol:/my-data --rm -it -d --name linux1 ubuntu /bin/bash
# run docker-container from image ubuntu with shared directory with host machine
docker run --rm -v ${PWD}:/myvol ubuntu /bin/bash -c "ls -lha > /myvol/myfile.txt"
# create container from image with port mapping and volume mointing
docker run -d --rm -p 8080:80 -v ${PWD}:/var/www/html php:7.4-apache
# create volume with name 'my-vol'
docker volume create --name my-vol
# show all volumes
docker volume ls
# remove volume with name 'my-vol'
docker volume rm my-vol
# create network and start container which use this network
docker network create simple-network
docker run --rm -d --name my-webserver --network simple-network httpd
# run docker container with port mapping
docker run -p 8080:8000 myphpapp:web
# run container with port mapping and name
docker run --name myphp-apache -p 8080:80 myphpapp:apache
# show all networks
docker network ls
# remove volume with name 'my-vol'
docker network rm my-network
# remove all networks not used by at least one container (now)
docker network prune
# start container/s and detach
docker-compose up -d
# build/rebuild container
docker-compose up --build
# list of started containers
docker-compose ps
# stop and remove containers and networks
docker-compose down
# build images without cache
docker-compose build --no-cache service1 service2
# set image from dockerhub
FROM php:7.4-apache
RUN apt-get -y update \
&& apt-get install -y libicu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
# set file|catalog wich we use in web-server
COPY index.php /var/www/html
# use port
EXPOSE 8000
# after container start
CMD ["php", "-S", "0.0.0.0:8000"]
version: '3.7'
services:
web:
image: nginx:alpine
container_name: udemy-web
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- 8090:80
networks:
- app1_net
- app2_net
app1:
image: httpd:latest
container_name: udemy-app1
networks:
- app1_net
app2:
image: httpd:latest
container_name: udemy-app2
networks:
- app2_net
networks:
app1_net:
app2_net:
# EXAMPLE 1: (Lesson3.3-Shared-Volume-Mounting)
# unpackage some archive throught container and remove it
# https://hub.docker.com/r/klutchell/rar
# flag -w for working directory (like cd)
docker run --rm -v ${PWD}:/files -w /files klutchell/rar a /files/myrar.rar /files/myfile.txt
# EXAMPLE 2: (Part3-Docker-Run/Lesson3.4-PHP-with-volume-mounting)
# PHP-with-volume-mounting
# create container from php-image
docker run -it --rm -v ${PWD}:/my-files -w /my-files --name php-script php:7.4-cli /bin/bash
# create php-file from console of container:
echo '<?php echo "ml test";' > index.php
# exit from container and try to run:
docker run -it --rm -v ${PWD}:/my-files -w /my-files --name php-script php:7.4-cli php index.php
# EXAMPLE 3:
- register or login in https://hub.docker.com/
- login to docker-hub in your pc [bash]:
docker login
- create Dockerfile:
FROM alpine RUN apk update && apk add curl; ENTRYPOINT ["curl"]
- in bash:
docker run mycurl google.com
- re-tag image:
docker tag mycurl maxlenash/mycurl:latest
- push image to docker-hub:
docker push maxlenash/mycurl:latest
- that's all. Now we can execute:
docker run --rm maxlenash/mycurl google.com
- remove unnecessary:
docker rmi mycurl maxlenash/mycurl
At the end of course you'll have certificate like that:
That git-repository created by Maxim Gavrilenko https://www.linkedin.com/in/maxim-gavrilenko-16693934