Docker is a tool that helps developers package and run applications in a consistent environment. Imagine you have a project that works perfectly on your computer, but when you share it with someone else, it doesn’t work on their machine. This can happen because their computer might have different software, libraries, or settings. Docker solves this problem by creating something called a "container," which is like a little box that contains everything your application needs to run, no matter where it is.
Docker is a powerful containerization platform that simplifies the process of developing, packaging, and deploying applications within lightweight, portable containers. Container is an isolated environment for running an application. To learn about docker in detail, here is a docker blog. Docker works in a CLIENT - SERVER (DOCKER ENGINE) architecture using REST API and shares OS kernel resources.
Install docker from : @docs.docker.com
$ docker run IMAGE
$ docker version (make sure both Client & Server Docker Engine is running)
$ touch Dockerfile
In Dockerfile
(all docker hub images):
FROM node:alpine (alpine is the linux distro)
COPY . /app (copy all files to docker fs /app)
CMD node /app/app.js
Here, we create and move into the /app
directory in the container. This helps organize the files and makes it easier to manage paths. The COPY
instruction copies files or directories from your host machine into the Docker image.
COPY . .
: This example copies everything from the current directory on your host machine (where the Dockerfile is located) into the working directory (/app) in the container. You can also specify individual files: COPY myfile.txt /app/myfile.txt
.
or
FROM node:alpine (alpine is the linux distro)
COPY . /app (copy all files to docker fs /app)
WORKDIR /app
CMD node app.js
To install software or dependencies, you can use the RUN
instruction. This instruction executes commands in the container.
RUN apt-get update && apt-get install -y python3 python3-pip
You can set environment variables in your Dockerfile using the ENV
instruction. These variables can be used by your application during runtime.
ENV PORT=8080
The EXPOSE
instruction is used to indicate the ports on which the container listens for connections. This doesn’t actually publish the port; it’s more of a documentation step. To publish the port, you would do so when running the container.
EXPOSE 8080
The CMD
or ENTRYPOINT
instructions specify what command to run when the container starts. CMD
is generally used to provide defaults that can be overridden, while ENTRYPOINT
is used to set the main command.
CMD ["python3", "app.py"]
To build docker image:
$ docker build -t image-name .
$ docker image ls
$ docker run -p 8080:80 image-name
$ docker pull username/image-name
Docker Containers:
$ sudo apt install docker.io docker-compose -y
$ docker pull centos
$ docker run -d -t --name skk centos
$ docker ps
$ docker exec -it skk bash
$ ls > $ exit
$ docker pull alpine
$ docker run -t -d --name saikia alpine
$ docker pull username/imagename:tag
$ docker run -t -d -p 80:80 --name skk username/imagename:tag (-p is for port mapping)
$ sudo docker run --name web -itd -p 8080:80 nginx
$ nano docker-compose.yaml
Docker Hub Images : |
TensorFlow Serving [image] |
PyTorch : [image] |
intel/dlstreamer : [image] |
mongo-express : [image] |
---|---|---|---|---|
MLflow [image] |
NGINX [image] |
Ubuntu : [image] |
kibana : [image] |
ros 2 : [image] |
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Instead of managing each container individually, Docker Compose lets you describe the entire environment in a single YAML file, making it easier to manage, scale, and orchestrate multiple containers that need to work together.
DOCKER COMPOSE : In docker-compose.yaml
:
version: "3"
services:
website:
image: nginx
ports:
- "8081:80"
restart: always
networks:
netname:
ipv4_address: 192.168.92.21
networks:
netname:
ipam:
driver: default
config:
- subnet: "192.168.92.0/24"
$ sudo docker-compose up -d
$ sudo docker-compose ps
$ sudo docker-compose stop
$ sudo docker-compose down
To get a clean workspace:
$ docker image rm imageid
$ docker image ls
$ docker image ls -q
$ docker container rm -f $(docker container ls -aq)
$ docker image rm -f $(docker image ls -aq)
DOCKER Networking:
$ sudo docker network ls
$ sudo docker inspect networkname
$ sudo docker exec -it imagename sh
$ sudo docker network create skkk
To dockerize an application, we just add a DOCKERFILE
to it and we get an IMAGE
. Resources : How to dockerize your Flask application, Dockerizing Flask with Postgres, Gunicorn, and Nginx
Udemy Classes : Docker & Kubernetes: The Practical Guide [2023 Edition], Docker Mastery: with Kubernetes +Swarm from a Docker Captain
Videos : Docker Tutorial for Beginners, Docker Tutorial for Beginners, Docker For Beginners: From Docker Desktop to Deployment, you need to learn Docker RIGHT NOW!! // Docker Containers 101, Docker Compose will BLOW your MIND!! (a tutorial), Docker networking is CRAZY!! (you NEED to learn it), Docker Containers and Kubernetes Fundamentals – Full Hands-On Course, Docker Compose Tutoria, you need to learn Kubernetes RIGHT NOW!!.
Resources : docker-hub, docker-cheatsheet.pdf, docker-cheatsheet-2.pdf.