This presentation was the foundation of our Docker Essentials workshop hosted by CloudHero CEO & founder Andrei Manea for the Innovation Labs team on the 23rd of July 2020.
This presentation covers the following topics:
-Getting started with containers
-A bit of history about orchestration
-Introduction to services (what they are, how to create and scale them).
To find out more about this topic, check https://cloudhero.io/
3. Workshop Agenda
● Getting started with containers
○ How to build an image
■ Base images
■ Dockerfile
■ Docker build
■ Docker commit
○ Docker tags
○ Push / Pull image
○ Docker Volumes
4. Workshop Agenda
● A bit of history about orchestration
● Introduction to services
○ What is a service
○ Creating services
○ Scaling Services
○ Rolling Updates
5. Prerequisites
● Docker Running on your Laptop
○ Docker for Mac / Windows
● Git
○ git clone
https://github.com/andrei821/orchestration-workshop.git
15. Key Takeaways
● Containers are NOT VM’s or mini VM’s or Servers
○ So they DO NOT BOOT
● Containers are just Containers
○ Packs of binaries and libraries that are executed on a
shared kernel in their own name space.
● They provide great portability and some extra layer of security
17. The marketplace for validated software
and tools available in Docker format for
businesses and publishers
• Easy search and deploy
• Trusted and compliant
• https://store.docker.com
The Docker Store
24. The Dockerfile
FROM ubuntu:trusty
MAINTAINER Andrei Manea <andrei@cloudhero.io>
# Install base packages
RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get -yq install
curl
apache2
Source: https://github.com/cloud-hero/apache-php
INSTRUCTION statement
25. Building an Image
Building an image allows you to create your own custom
images that you can use and share with others.
26. Building an Image
git https://github.com/cloud-hero/apache-php.git docker-demo
cd docker-demo
docker build -t cloudhero/apache-php .
docker images ls
Source: https://github.com/cloud-hero/apache-php
29. Committing an Image
docker run -ti ubuntu bash
root@9923jj4a9 apt update && apt install nginx-full
---
docker ps -a
docker commit -m “Added Nginx” -a “Andrei Manea” 9923jj4a9
nginx-ubuntu:latest
docker images ls
30. Settings Tags on an Image
Tags (or image names) can be used to organise and find
images easier.
31. Settings Tags on an Image
docker tag 9923jj4a9 my-hub-id/nginx-ubuntu:latest
32. Push an Images to Docker Hub / Store
Once you’ve built or created a new image you can push it to
Docker Hub using the docker push command.
This allows you to share it with others, either publicly, or push it
into a private repository.
33. Push an Images to Docker Hub / Store
docker push my-hub-id/nginx-ubuntu:latest
35. Managing Containers
We can list all the running container.
docker container ls
And then all the container existing on the host.
docker container ls -a
From this list, get the id of the container in which we installed the figlet package and restart the
container using the ‘start’ command.
docker container start CONTAINER_ID
Run an interactive shell in this container. We will use the exec command to do so.
docker container exec -ti CONTAINER_ID bash
figlet Still Here!
exit
36. Managing Containers
We can list all the running container.
docker container ls
And then all the container existing on the host.
docker container ls -a
From this list, get the id of the container that you want to delete.
docker container rm CONTAINER_ID (or force a running container with `rm -f`)
Mass deletion of containers.
docker container ls -aq
Docker container rm `docker container ls -aq`
38. Data persistency without a Volume
Let’s run an interactive shell within an alpine container named c1.
docker container run --name c1 -ti alpine sh
We will create the /data folder and a dummy hello.txt file in it.
mkdir /data && cd /data && touch hello.txt
We will then check how the read-write layer (container layer) is accessible from the host.
Let exit the container first
exit
Let’s inspect our container in order to get the location of the container’s layer. We can use the
inspect command and then scroll into the output until the GraphDriver key, like the following.
docker container inspect -f "{{ json .GraphDriver }}" c1 | python -m
json.tool
39. Data persistency without a Volume
ls /graph/overlay2/[YOUR_ID]/diff/data
docker container rm c1
It seems the folder defined in the UpperDir above does not exist anymore. Do
you confirm that ? Try running the ls command again and see the results.
40. Data persistency with a Volume
Defining a volume in a Dockerfile
FROM alpine
VOLUME ["/data"]
ENTRYPOINT ["/bin/sh"]
Let’s build an image from this Dockerfile.
docker image build -t img1 .
docker container run --name c2 -ti img1
We should then end up in a shell within the container. From there, we will go into /data and create a
hello.txt file.
cd /data
touch hello.txt
ls
41. Data persistency with a Volume
Let’s create a container from the alpine image, we’ll use the -d option so it runs in background
and also define a volume on /data as we’ve done previously. In order the PID 1 process remains
active, we use the following command that pings Google DNS and log the output in a file within
the /data folder.
ping 8.8.8.8 > /data/ping.txt
The container is ran that way:
docker container run --name c3 -d -v /data alpine sh -c 'ping 8.8.8.8 >
/data/ping.txt'
42. Data Volume API
The volume API introduced in Docker 1.9 enables to perform operations on volume very easily.
First have a look at the commands available in the volume API.
docker volume --help
We will start with the create command, and create a volume named html.
docker volume create --name html
If we list the existing volume, our html volume should be the only one.
docker volume ls
The output should be something like
DRIVER VOLUME NAME
[other previously created volumes]
local html
docker container run --name www -d -p 8080:80 -v html:/usr/share/nginx/html nginx
44. The Docker Network Command
The docker network command is the main command for configuring and managing
container networks. Run the docker network command from the first terminal.
docker network
Run a docker network ls command to view existing container networks on the current
Docker host.
docker network ls
Every clean installation of Docker comes with a pre-built network called bridge. Verify this
with the docker network ls.
All networks created with the bridge driver are based on a Linux bridge (a.k.a. a virtual
switch).
48. Docker service is a part of Docker’s native approach for container
orchestration
49. ● transition from deploying containers individually on a single host, to deploying
complex multi-container apps on many machines.
● a distributed platform, independent from infrastructure, that stays online through
the entire lifetime of your application, surviving hardware failure and software
updates.
Container Orchestration is:
50. docker swarm init --advertise-addr
$(hostname -i)
Copy the join command (watch out for newlines) output
and paste it in the other terminal.
Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
51. Type the below command in the first terminal:
docker node ls
That last line will show you a list of all the nodes, something like this:
ID HOSTNAME STATUS
AVAILABILITY MANAGER STATUS
kytp4gq5mrvmdbb0qpifdxeiv * node1 Ready Active
Leader
lz1j4d6290j8lityk4w0cxls5 node2 Ready Active
If you try to execute an administrative command in a non-leader node
worker, you’ll get an error. Try it here:
docker node ls
Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
52. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
docker service create --name hello -p 80:80
cloudhero/apache-php
53. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
Set your browser to:
http://localhost:80
54. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
Scale Up:
docker service scale hello=3
Let’s check our service status:
docker service ps hello
55. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
Set your browser to:
http://localhost:80
And refresh multiple times:
56. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
Scale Down:
docker service scale hello=2
Let’s check our service status:
docker service ps hello
57. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
Set your browser to:
http://localhost:80
And refresh multiple times:
58. Get a Docker
Swarm Cluster
Create Services
Test
Update
Scale
Scalable and Highly Available Hello-World With Docker
Update Publisher Port
docker service update --publish-add 81:80
hello
docker services ls
Set your browser to:
http://localhost:81