2024-06-12
Getting Started with Docker
Docker basics and some of the best practices to keep in mind
Dockers a tool that allows devs to deploy their apps easily in a container to run on any hostOS. The benefit of docker is that it packages an app with all ist dependencies into a container.
Containers are highly efficient in terms of mem usage unlike VMs.
VMs
Provides isolation but has high resource overhead and slow boot times.
Containers
Lightweight as they share OS, can be easily deployed.
This mechanism decouples the app from the environment in which they actually run allowing devs to easily deploy into public cloud, private data center or even in their own laptop reagrdless of whichOS they use.
docker deamon is the heart and manages. Similar to k8s api server. docker client is used to interact with the deamon. images are the blueprints. containers run the actual app. docker hub is the registry. Similar to AWS ECR.
Installation
Install docker based on theOS you have.
Once you install docker, you can test if it is installed correctly by running:
Pull
[registry-url]/[namespace]/[image]:[tag]
only downloads those layers that haven't been cached locally
Run
run the image in an interactive mode
exit with CTRL+D
you can ssh into running containers
docker exec -ti <container_id> bash
listing all containers
useful variant is to use -a
to run with GPUs pass
--gpus=all
Volumes
persists data beyond container lifecycle
add -v HOST_PATH:CONTAINER_PATH
to docker run command
for example, map current dir to /opt/local
Clean up
Dockerfile
It's a text file that has all the commands
We start with specifying our base image:
We can pass ARG
for example
We can also use ENV which persists within the docker image like ENV DEBIAN_FRONTEND=noninteractive
installing dependencies
next, copy files
expose the port
Best practices
-
Use multi-stage builds to create leaner and more secure images
-
Order commands properly
-
Use smaller base images like python:3.8-slim
-
Use COPY (copy local files from the docker host to the image) over ADD (downloading external files)
-
Cache python packages using
--mount=type=cache,target=/root/.cache/pip
-
Run only one process per container
-
Use ENTRYPOINT instead of CMD
-
Use a
.dockerignore
file -
Use
docker scout
to scan image for vulnerabilities