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. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production. Docker allows you to package an application with all of its dependencies into a standardized unit for software development.
- released in 2013
- once-in-a-decade shift in infrastructure
- 90’s - Mainframe to PC
- 00’s - Baremetal to Virtual
- 10’s -Datacenter to Cloud (AWS)
- Now - Host to Container
- Created with developers in mind
- Predictions: by 2020, more than 50% of global organizations will be running containers in production
- Containers reduce complexity (run the same way)
- 80% maintenance, 20% inovation before Docker
- PayPal migrated 700+ apps -> 50% dev productivity boost -> 66% cost reduction
- Just a piece of the puzzle, all tooling options found on Cloud Native Interactive Landscape
Docker Engine is a client-server application with these major components:
- A server which is a type of long-running program called a daemon process (the
dockerd
command). - A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
- A command line interface (CLI) client (the docker command).
Source: Docker.com
Source: Docker.com
image
:- app binaries and dependencies
- metadata about the image data and how to run the image
- NOT a complete OS. No kernel, no kernel modules (eg: drivers)
- small as one file
- big as Ubuntu
container
:- they are just processes
- limited to what resource they can access
- exit when process stops
Each instruction creates one layer:
-
FROM <image> [AS <name>]
- this is the foundation of our image, the parent image. Recommended to use a minimum distribution likealpine
, eg:node:13.0.1-alpine
~ 105MB (36.13 MB compressed)ruby:2.6.5-alpine
~ 50MB (24.15 MB compressed)
Other images:
- Alpine
alpine:latest
~ 2.66 MB - Ubuntu
ubuntu:xenial
~ 42.11 MB - MySQL
mysql:latest
~ 127.82 MB
-
ENV <key> <value>
- used when building and running a containerENV MY_NAME John Doe ENV MY_DOG Rex The Dog ENV MY_CAT fluffy
-
WORKDIR /path/to/workdir
- sets the working directory for anyRUN
,CMD
,ENTRYPOINT
,COPY
andADD
instructions that follow -
COPY [--chown=<user>:<group>] <src>... <dest>
- copies new files or directories from<src>
and adds them to the filesystem of the container at the path<dest>
- Tip: if you’re copying in local files to your Docker image, always use
COPY
because it’s more explicit
- Tip: if you’re copying in local files to your Docker image, always use
-
ADD [--chown=<user>:<group>] <src>... <dest>
- copies new files, directories or remote file URLs from<src>
and adds them to the filesystem of the image at the path<dest>
- Tip: use it when you wan't to use a URL instead of a local file or extract a tar file from the source directly into the destination
-
RUN <command>
- shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows -
EXPOSE <port> [<port>/<protocol>...]
- informs Docker that the container listens on the specified network ports at runtime -
CMD
- has three forms:CMD ["executable","param1","param2"]
(exec form, this is the preferred form)CMD ["param1","param2"]
(as default parameters to ENTRYPOINT)CMD command param1 param2 (shell form)
- 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.
-
ENTRYPOINT
has two forms:ENTRYPOINT ["executable", "param1", "param2"]
(exec form, preferred)ENTRYPOINT command param1 param2 (shell form)
- allows you to configure a container that will run as an executable, eg:
docker run -i -t --rm -p 80:80 nginx
-
Why?
- configure relationships between containers
- save our docker container run settings in easy-to-read file
- create one-liner developer environment startups
-
CLI tool comes with Docker for Windows/Mac
-
Not a production-grade tool but ideal for local development and test
-
Two most common commands are
docker-compose up
# setup volumes/networks and start all containersdocker-compose down
# stop all containers and remove cont/vol/net
-
If all your projects had a Dockerfile and docker-compose.yml then "new developer onboarding" would be:
git clone github.com/some/software
docker-compose up
Prerequisite:
- Docker installed locally
- Clone this repo
Then start with:
cd workspace
code .
- In the root folder each project create the required files:
Dockerfile
- a recipe for creating an image.dockerignore
- prevents unwanted files and folders to be added to the docker image (eg:node_modules/
,dist/
)docker-compose.yml
- configure relationships between containers`
docker swarm init
docker stack deploy -c wolfpack-infra/wolfpack-stack.yml demo
docker service ls
docker stack rm demo
docker system prune -a
> Total reclaimed space: 11.81GB