Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. This is a first introduction to Docker, Dockerfile and docker-compose with relative basic commands.
4. What is Docker?
What is a Container? A container is a completely isolated
environemnt.
It is a standard unit of software that packages up code and all its
dependencies so the application runs quickly and reliably from one
«The only independent container platform
that enables organizations to seamlessly build,
share and run any application,
anywhere—from hybrid cloud to the edge.»
5. What is a container?
• A container is a completely isolated environment
• it is a standard unit of software that packages up code and all its
dependencies so the application runs quickly and reliably from one
computing environment to another
• Every container has their own processes or services, their or
network interfaces
• They all share the same OS kernel (responsible for interacting with
the underlyng hardware)
6. Why Docker? What problems does it solve?
• Compatibility with the version of OS
• Compatibility between services and libraries
• Long setup time (ex: new developer in the team)
• Different Dev/Test/Prod environments
7. What can Docker do?
• Containerize Applications
• Run each service with its own dependencies in separate containers
10. Docker images
• A Docker image is a file, composed of multiple layers, used to
execute code in a Docker container
• An instance of an image is called a container
• Once the task is complete, the container exits
• A container only lives as long as
the process inside it is alive
11. How to install
Community Edition vs Enterprise Edition
Desktop version
Mac, Windows, Linux
Cloud providers
AWS & Azure
Server
Windows Server, CentOs, Fedora, Oracle Linux, Ubuntu,…
https://docs.docker.com/
First release made. Soon
available
15. Docker commands
docker ps
List only active containers
Ex: docker ps
docker ps --all
List all containers
Ex: docker ps --all | docker ps -a
16. Docker “run” command
The basic docker run command takes this form:
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
• The docker run command first creates a writeable container layer
over the specified image, and then starts it using the specified
command in foreground mode
• The docker run command must specify an image to derive the
container from
• With the docker run [OPTIONS] an operator can add to or override
the image defaults set by a developer
• Tag is used to specify the version (ex: redis:4.0)
https://docs.docker.com/engine/reference/run/
17. Docker “run” command
docker run --name [containerName] [imageName]
If you specify a name, you can use it when referencing the container
Ex: docker run --name myContainer ubuntu
Ex: docker run –-name myRedis redis:4.0
docker run [imageName] [commandName] [parameters]
Run a container from the imageName and execute the specified
command
Ex: docker run docker/whalesay cowsay ciao!
Ex: docker run ubuntu sleep 10
18. Docker “run” command
docker run -d [imageName]
Run a container in “detached” mode (a container runs in the
background of your terminal)
Ex: docker run -d ubuntu sleep 1500
docker run -it [imageName]
Run a container in “interactive” mode. –i for mapping the standard
input of your host to the docker container. –t basically makes the
container start look like a terminal connection session
Ex: docker run -it ubuntu
$ cd /home
19. Docker “run” command – port mapping
docker run –p [port]:[containerPort] [imageName]
Run a container with a particular port mapping
Ex: docker run –p 3307:3306 mysql
20. Docker “run” command – volume mapping
docker run –v [path]:[insideContainerPath] [imageName]
Run a container with a particular volume mapping
Ex: docker run –v /myFolder:/var/lib/mysql mysql
21. Other Docker commands
docker attach
Attach local standard input, output and errors streams to a running
container
Ex: docker attach 028e
docker exec [containerId] [command] [parameters]
Run a command in a running container
Ex: docker exec 27bj378 cat /logs/log.txt
22. Other Docker commands
docker stop [containerId]
Stop one or more running containers
Ex: docker stop 028d23e
docker rm [containerId]
Remove one or more containers
Ex: docker rm 27bj378
23. Other Docker commands
docker inspect [containerId/containerName]
Show all the container details in a JSON format
Ex: docker inspect myUbuntu
docker logs [containerId/containerName]
Show all the container logs
Ex: docker logs myUbuntu
25. Dockerfile and docker-compose
Dockerfile
A Dockerfile is a text document that
contains all the commands a user could
call on the command line to assemble an
image
Docker-compose
Compose is a tool for defining and
running multi-container Docker
applications
26. Dockerfile
Using docker build users can create an automated build that executes
several command-line instructions in succession.
FORMAT
Here is the format of the Dockerfile:
# Comment
INSTRUCTION arguments
27. Dockerfile instructions
FROM [imageName]
The FROM instruction initializes a new build stage and sets the Base
Image for subsequent instructions
RUN […]
The RUN instruction will execute any commands in a new layer on top
of the current image and commit the results
EXPOSE […]
The EXPOSE instruction informs Docker that the container listens on
the specified network ports at runtime
28. Dockerfile instructions
ADD [src] [dest]
The ADD instruction copies new files, directories or remote file URLs
from <src> and adds them to the filesystem of the image at the
path <dest>
COPY [src] [dest] (preferred)
The COPY instruction copies new files or directories from <src> and
adds them to the filesystem of the container at the path <dest>. Same
as 'ADD', but without the tar and remote URL handling.
ENV [key]=[value]
The ENV instruction sets the environment variable <key> to the
value <value>
29. Dockerfile instructions
CMD [command] (also json array format supported)
The main purpose of a CMD is to provide defaults for an executing
container.
These defaults can include an executable, or they can omit the
executable, in which case you must specify
an ENTRYPOINT instruction as well.
If a Dockerfile has multiple CMDs, it only applies the instructions from
the last one.
ENTRYPOINT [command]
The ENTRYPOINT specifies a command that will always be executed
when the container starts.
The CMD specifies arguments that will be fed to the ENTRYPOINT.
30. Dockerfile example
FROM ubuntu
RUN apt-get update
RUN apt-get -y install wget
RUN apt-get -y install sudo
RUN cd /
RUN sudo wget http://www.domain.com/file.jpg
# arguments
CMD ["15"]
ENTRYPOINT ["sleep"]
In console
$ docker build . –t [ImageName]
32. Docker compose
• Compose is a tool for defining and running multi-container
Docker applications.
• With Compose, you use a YAML file to configure your application’s
services.
• Then, with a single command, you create and start all the services
from your configuration.
Using Compose is basically a three-step process:
1. Define your app’s environment with a Dockerfile so it can be
reproduced anywhere.
2. Define the services that make up your app in docker-
compose.yml so they can be run together in an isolated
environment.
3. Run docker-compose up and Compose starts and runs your
33. Docker compose
docker-compose up
Builds, (re)creates, starts, and attaches to containers for a service.
docker-compose down
Stops containers and removes containers, networks, volumes, and
images created by up.
https://docs.docker.com/compose/reference/overview/
34. Docker compose keywords
services
The beginning of a docker-compose file
image
Specify the image to start the container from.
Can either be a repository/tag or a partial image ID.
build
Configuration options that are applied at build time.
35. Docker compose keywords
volumes
Mount host paths or named volumes, specified as sub-options to a
service.
If you want to reuse a volume across multiple services, then define a
named volume in the top-level volumes key.
links
Express dependency between services.
ports
Expose ports.
https://docs.docker.com/compose/compose-file/compose-file-v3/
Cosa succede con kernel differenti (es: windows e linux) se spostiamo I container da una parte all’altra? Widnows 10 ha una linux machine intermedia
Prima gli sviluppatori inviavano I war + I file di configurazione ai sistemisti e spesso c’erano problemi nel deploy per via di qualche dettaglio sfuggito. Ora si può inviare il war del Progetto con il dockerfile che contiene al suo interno tutte le configurazioni e non ci sono quindi più errori
Ogni virtual machine ha il suo Sistema Operativo e quindi un alto consumo di risorse. Gigabyte vs megabyteVelocità di caricamentoDocker però ha meno isolamento (il kernel è condiviso) mentre le virtual machine sono completamente isolate e possono girare facilmente su più sistemi operativiIn grossi sistemi si usa una soluzione ibrida con più container in una macchina virtuale per sfruttuare i vantaggi di entrambe le soluzioni
Le immagini sono template da cui creare più container.