Repository: Ameen-Alam/CNC-Docker Branch: master Commit: f49f1e31f762 Files: 29 Total size: 20.8 KB Directory structure: gitextract_jzzoj24z/ ├── 001-static/ │ ├── Dockerfile │ ├── README.md │ └── index.html ├── 002-static/ │ ├── Dockerfile │ └── index.html ├── 003-php/ │ ├── Dockerfile │ └── index.php ├── 004-python/ │ ├── Dockerfile │ ├── app.py │ └── readme.md ├── 005-ubuntu/ │ ├── Dockerfile │ ├── app.js │ ├── package.json │ └── readme.md ├── 006-nodejs/ │ ├── .dockerignore │ ├── Dockerfile │ ├── README.md │ ├── package.json │ └── server.js ├── 007-volumeMount/ │ └── readme.md ├── 008-mysql/ │ └── readme.md ├── 009-mssql/ │ └── readme.md ├── 010-mongoDB/ │ └── readme.md ├── Cheat Sheet/ │ └── README.md ├── Install-Docker/ │ ├── README.md │ └── installDocker.sh ├── README.md ├── _config.yml └── hands-on-command-journey.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: 001-static/Dockerfile ================================================ FROM nginx:alpine COPY . /usr/share/nginx/html ================================================ FILE: 001-static/README.md ================================================ *Instruction that is used to build an image. Use the docker image build command to create a new image using the instructions contained in the Dockerfile. This example creates a new Docker image called name:tag. Be sure to perform this command from within the directory containing the app code and Dockerfile.* #### Build Image > docker build -t <image_name>:<tag> <source_of_Dockerfile> `$ docker build -t static-web:latest .` #### Docker images list `$ docker image ls` #### Run Container > docker run -d --name <container_name> -p <host_port>:<container_port> <image_name>:<tag> `$ docker container run -d --name=web1 -p=8080:80 static-web:latest` #### For Test ` $ curl http://localhost:8080` *Open a web browser and navigate to the DNS name or IP address of the host that you are running the container from and point it to port 8080. You will see the following web page.* > http://localhost:8080 ================================================ FILE: 001-static/index.html ================================================ My First Web App

Hello World Docker

================================================ FILE: 002-static/Dockerfile ================================================ FROM nginx:latest COPY . /usr/share/nginx/html ================================================ FILE: 002-static/index.html ================================================ Hello world

Hello World

================================================ FILE: 003-php/Dockerfile ================================================ FROM php:apache COPY index.php /var/www/html EXPOSE 80 ================================================ FILE: 003-php/index.php ================================================ ================================================ FILE: 004-python/Dockerfile ================================================ # Use an official Python runtime as a parent image FROM python:2.7-slim WORKDIR /app ADD . /app RUN pip install --trusted-host pypi.python.org Flask ENV NAME World ENV BGNAME=purple EXPOSE 4000 CMD ["python", "app.py"] ================================================ FILE: 004-python/app.py ================================================ from flask import Flask import os import socket app = Flask(__name__) @app.route("/") def hello(): html = "

Hello {name}!

Hostname: {hostname}
" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), bgname=os.getenv("BGNAME", "orange")) if __name__ == "__main__": app.run(host='0.0.0.0', port=4000) ================================================ FILE: 004-python/readme.md ================================================ docker build -t mypyweb . docker run --name webapp -p 8080:4000 mypyweb docker run --name webapp -p 8080:4000 -e NAME="Dude" -e BGNAME="blue" mypyweb #### Share an image docker login docker tag mypyweb ameenalam/python-example:1.0 docker push uqutub/python-example:1.0 docker run -p 8080:4000 --name webapp -e NAME="Docker Hub" ameenalam/python-example:1.0 ================================================ FILE: 005-ubuntu/Dockerfile ================================================ FROM ubuntu:16.04 RUN apt-get update RUN apt-get upgrade -y RUN apt-get dist-upgrade -y RUN apt-get install curl htop git zip nano ncdu build-essential chrpath libssl-dev libxft-dev pkg-config glib2.0-dev libexpat1-dev gobject-introspection python-gi-dev apt-transport-https libgirepository1.0-dev libtiff5-dev libjpeg-turbo8-dev libgsf-1-dev fail2ban nginx -y # Install Node.js RUN curl -sL https://deb.nodesource.com/setup_8.x | bash RUN apt-get install --yes nodejs RUN node -v RUN npm -v RUN npm i -g nodemon RUN nodemon -v # Cleanup RUN apt-get update && apt-get upgrade -y && apt-get autoremove -y # Node App COPY . /app WORKDIR /app # Install app dependencies RUN npm install # Binds to port 3000 EXPOSE 3000 # Defines your runtime(define default command) # These commands unlike RUN (they are carried out in the construction of the container) are run when the container CMD ["node", "app.js"] ================================================ FILE: 005-ubuntu/app.js ================================================ const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => res.send('Hello World! NodeJS')); app.listen(port, () => console.log(`Example app listening on port ${port}!`)); ================================================ FILE: 005-ubuntu/package.json ================================================ { "name": "demojsapp", "version": "1.0.0", "description": "demo nodejs app in ubuntu", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://github.com/ameen-alam/CNC-Docker.git" }, "author": "Ameen Alam", "license": "ISC", "dependencies": { "express": "^4.17.0" } } ================================================ FILE: 005-ubuntu/readme.md ================================================ ```sh $ docker run -it ameen /bin/bash ``` ================================================ FILE: 006-nodejs/.dockerignore ================================================ node_modules npm-debug.log ================================================ FILE: 006-nodejs/Dockerfile ================================================ FROM node:8 # Create app directory WORKDIR /usr/src/app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 8080 CMD [ "npm", "start" ] ================================================ FILE: 006-nodejs/README.md ================================================ *Instruction that is used to build an image. Use the docker image build command to create a new image using the instructions contained in the Dockerfile. This example creates a new Docker image called name:tag. Be sure to perform this command from within the directory containing the app code and Dockerfile.* #### Build Image > docker build -t <image_name>:<tag> <source_of_Dockerfile> `$ docker build -t node-app:latest .` #### Docker images list `$ docker image ls` #### Run Container > docker run -d --name <container_name> -p <host_port>:<container_port> <image_name>:<tag> `$ docker container run -d --name=web1 -p=9000:8080 node-app:latest` #### For Test ` $ curl http://localhost:9000` *Open a web browser and navigate to the DNS name or IP address of the host that you are running the container from and point it to port 9000. You will see the following web page.* > http://localhost:9000 ================================================ FILE: 006-nodejs/package.json ================================================ { "name": "docker_web_app", "version": "1.0.0", "description": "Node.js on Docker", "author": "Ameen ALam ", "main": "server.js", "scripts": { "start": "node server.js" }, "dependencies": { "express": "^4.16.1" } } ================================================ FILE: 006-nodejs/server.js ================================================ 'use strict'; const express = require('express'); // Constants const PORT = 8080; const HOST = '0.0.0.0'; // App const app = express(); app.get('/', (req, res) => { res.send('Hello world\n'); }); app.listen(PORT, HOST); console.log(`Running on http://${HOST}:${PORT}`); ================================================ FILE: 007-volumeMount/readme.md ================================================ ```sh $ docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx:alpine ``` When we execute this command line, we see Docker download the Nginx image and then start the container. We used four command line options to run this container: - -v /full/path/to/html/directory:/usr/share/nginx/html:ro maps the directory holding our web page to the required location in the image. The ro field instructs Docker to mount it in read-only mode. It’s best to pass Docker the full paths when specifying host directories. - -p 8080:80 maps network service port 80 in the container to 8080 on our host system. - -d detaches the container from our command line session. we don’t want to interact with this container. - nginx is the name of the image. ================================================ FILE: 008-mysql/readme.md ================================================ ```sh $ docker run -p 3306:3306 --name mysqlserver -e MYSQL_ROOT_PASSWORD=root -d -v /home/mysql-data:/var/lib/mysql mysql ``` #### Listing existing mounts ```sh $ docker inspect mysqlserver ``` ================================================ FILE: 009-mssql/readme.md ================================================ ```sh $ docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=mypassword" -e "MSSQL_PID=Express" -p 1433:1433 -v /path/your/myfolder:/var/opt/mssql --name HIMS-WEB -d microsoft/mssql-server-linux:latest ``` ================================================ FILE: 010-mongoDB/readme.md ================================================ ### The docker run -d -p 27017:27107 -v ~/data:/data/db mongo does 3 main things: ```sh $ docker run -d -p 27017:27107 -v ~/data:/data/db mongo ``` - -d tells docker to run the container as a daemon, which is the mode that'll you want to use for server containers. - -p 27017:27107 maps the port 27017 of the container to the port 27017 of the host. The syntax is -p HOST_PORT:CONTAINER_PORT. - -v ~/data:/data/db maps the /data/db directory of the container to the ~/data directory on the host. This is called a data volume, the principal mechanism to import and export data with your docker container. ================================================ FILE: Cheat Sheet/README.md ================================================ ### Cheat Sheet | Command | Description | | ------ | ------ | | docker pull <image-name>:<tag> | Pulls the image from the docker hub | | docker build -t <image-name>:<tag> . | Builds the image from the Dockerfile with the mentioned name and tag | | docker image ls | Shows the list of the images present on your system. short-hand 'docker images' | | docker container ls | Displays the only running containers. short-hand 'docker ps' | | docker container ls -a | Displays all the containers present on your system. short-hand 'docker ps -a' | | docker inspect <image name>:<tag> | Shows the detailed information about the image in JSON format. | | docker history <image name>:<tag> | Used to inspect the layers of the image. | | docker tag <source-image>:<tag> <username/new-image-name>:<tag> | Create a tag of the new image that refers to source image. | |docker push user/<image-name>:<tag> | Push an image to a registry | | docker image rm <image name>:<tag> | Remove the image. short-hand 'docker rmi <image name>:<tag>' | | docker run --name <container_name> -p <host_port : container_port> -d <image_name> | Create the container with the specified name and assign the specified port from the image. | | docker run --name <container-name> -it -p <host_port : container_port> <image-name>:<tag> sh | To run a container from an image in an interactive mode. Press Ctrl + pq it will detach terminal and leave container running in background. | | docker exec -it <container_name> sh | To go in the running container shell. Write exit to detach the terminal | | docker stop container_name | It will stop the running container. | | docker start container_name | Start the stopped container | | docker rm container_name | Remove the container. | | docker logs container_name | fetch the logs of the container | | docker volume create my-vol | Create your Volume for Persistent Data | | docker volumes ls | List down the volumes | | docker volume inspect my-vol | inspect the volumes | | docker volumes rm my-vol | remove volume | | docker run -d --name mycont -v my-vol:/usr/share/nginx/html nginx:latest | start container with -v flag (volume mount) | | docker run -d --name devtest --mount source=my-vol,target=/usr/share/nginx/html nginx:latest | start container with --mount flag | | docker run -d -it --name devtest -v "$(pwd)"/myfolder:/app nginx:latest | start container with bind mounts and -v flag | | docker run -d -it --name devtest --mount type=bind,source="$(pwd)"/myfolder,target=/app nginx:latest | start container with bind mounts and -mount flag | ================================================ FILE: Install-Docker/README.md ================================================ # Install Docker with Linux *There are two ways of installing it. The official docker way is a bit more recent.* ##### 1st way.. ### A) Official Ubuntu Repositories `$ sudo apt-get install docker.io` > and after verify the installation `$ docker -v` `$ sudo docker run hello-world` ------------------------ ##### 2nd way ### (B) Official Docker Way **(1) Set up the docker repository and Install Docker CE** > Updating the apt package index `$ sudo apt-get update` > Installing packages to allow apt to use a repository over HTTPS: `$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common` > Adding Docker’s official key GPG `$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -` > Verifying that you now have the key with fingerprint: `$ sudo apt-key fingerprint 0EBFCD88` > sub-command below returns the name of your Ubuntu distribution `$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"` > Updating the apt package index `$ sudo apt-get update` > Installing the latest version of Docker CE and containerd `$ sudo apt-get -y install docker-ce docker-ce-cli containerd.io` > and after verify the installation `$ docker -v` `$ sudo docker run hello-world` Docker install by source https://docs.docker.com/engine/install/binaries/ https://download.docker.com/linux/static/stable/x86_64/ https://docs.docker.com/compose/install/ ###### Thankyou. ================================================ FILE: Install-Docker/installDocker.sh ================================================ #!/bin/sh echo -------------------------------------------- echo Installing Script, Docker created by: Uqutub echo -------------------------------------------- echo Updating the apt package index: echo -------------------------------------------- sudo apt-get update echo -------------------------------------------- echo Installing packages to allow apt to use a repository over HTTPS: echo -------------------------------------------- sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common echo -------------------------------------------- echo Adding Docker’s official key GPG echo -------------------------------------------- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - echo -------------------------------------------- echo Verifying that you now have the key with fingerprint: echo -------------------------------------------- sudo apt-key fingerprint 0EBFCD88 echo -------------------------------------------- echo sub-command below returns the name of your Ubuntu distribution: echo -------------------------------------------- sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" echo -------------------------------------------- echo Updating the apt package index: echo -------------------------------------------- sudo apt-get update echo -------------------------------------------- echo Installing the latest version of Docker CE and containerd, echo -------------------------------------------- sudo apt-get -y install docker-ce docker-ce-cli containerd.io ================================================ FILE: README.md ================================================ # Deploy Application using Docker with Nginx [Docker Deep Dive Book PDF](/Docker%20Deep%20Dive.pdf) #### Docker Tutorial [Docker for beginners tutorial](https://www.youtube.com/playlist?list=PLRnB5kp_ilgbsl8hJwhQXmSos8Ua9Tc_d) [https://github.com/Ameen-Alam/CNC-Docker.git](https://github.com/Ameen-Alam/CNC-Docker.git) Clone the sample app from GitHub. `$ git clone https://github.com/Ameen-Alam/CNC-Docker.git` **The clone operation creates a new directory called CNC-Docker . Change directory into CNC-Docker and list its contents.** `$ cd CNC-Docker` > Available Multiple Image Directories (001-static --to-- 010-mongoDB) `$ cd 001-static` __two way to run a container__ -------------------------------- ### Start The Ops Perspective *If you are working from a freshly installed Docker host it will have no images and will look like the output above. Getting images onto your Docker host is called “pulling”. If you are following along with Linux, pull the ameenalam/nodeapp:latest.* #### To pull an image > docker pull <image_name>:<tag> `$ docker image pull ameenalam/nodeapp:latest` #### Docker images list `$ docker image ls` #### Run Container > docker run -d --name <container_name> -p <host_port>:<container_port> <image_name>:<tag> `$ docker container run -d --name=web2 -p=7000:8080 ameenalam/nodeapp:latest` #### For Test ` $ curl http://localhost:7000` *Open a web browser and navigate to the DNS name or IP address of the host that you are running the container from and point it to port 7000. You will see the following web page.* http://localhost:7000/ ----------------------------------- # Push images to Docker Cloud *Docker Cloud uses Docker Hub as its native registry for storing both public and private repositories. Once you push your images to Docker Hub, they are available in Docker Cloud.* __In order to get you started, let us get you a Docker ID.__ https://hub.docker.com/ #### Docker login > Log in to Docker Cloud using the docker login command. `$ docker login` #### Tag Image > Tag your image using docker tag. `$ docker tag image_name username/image_name` #### Push your image to Docker Hub > Push your image to Docker Hub using docker push (making the same replacements as in the previous step). `$ docker push username/image_name` *Check that the image you just pushed appears in Docker Cloud. Go to Docker Cloud and navigate to the Repositories tab and confirm that your image appears in this list.* Facebook Profile: https://www.facebook.com/SheikhAmeenAlam ###### Thankyou. ================================================ FILE: _config.yml ================================================ theme: jekyll-theme-slate ================================================ FILE: hands-on-command-journey.md ================================================ # Hands-on Command Journey ## 1) Core Docker commands (the “must know” list) ### Help + version ```bash docker --help docker version docker info ``` ### Images ```bash docker images docker pull python:3.12-slim docker rmi docker image prune ``` ### Containers (run/stop/logs/exec) ```bash docker ps docker ps -a docker run hello-world docker run --name mybox -it ubuntu:24.04 bash docker stop mybox docker start mybox docker restart mybox docker rm mybox docker logs docker logs -f docker exec -it bash ``` ### Ports + env + naming ```bash docker run --name api -p 8000:8000 -e ENV=dev python:3.12-slim ``` ### Volumes (persistency) ```bash docker volume ls docker volume create appdata docker volume inspect appdata docker run -v appdata:/data -it ubuntu:24.04 bash docker volume rm appdata docker volume prune ``` ### Bind mounts (dev workflow) ```bash docker run -it --rm -v "%cd%":/app -w /app python:3.12-slim bash # (Linux/Mac: -v "$(pwd)":/app) ``` ### Networks (service-to-service) ```bash docker network ls docker network create appnet docker network inspect appnet docker run -d --name redis --network appnet redis:7 docker run -it --rm --network appnet redis:7 redis-cli -h redis ping ``` ### Cleanup (important for students’ laptops 😄) ```bash docker system df docker system prune docker system prune -a --volumes ``` --- ## 2) Dockerfile example: FastAPI + uv (production-ready pattern) > Assumption: you run FastAPI with **uvicorn** (common). If you truly meant “uv” the Python package manager, tell me and I’ll adjust. ### `Dockerfile` (multi-stage, small-ish, prod) ```dockerfile # ---- base stage ---- FROM python:3.12-slim AS base WORKDIR /app ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 # System deps (adjust if you need build tools) RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ && rm -rf /var/lib/apt/lists/* # ---- deps stage ---- FROM base AS deps COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # ---- runtime stage ---- FROM base AS runtime COPY --from=deps /usr/local /usr/local COPY . . EXPOSE 8000 CMD ["uvicorn", "app.main:app", "--host=0.0.0.0", "--port=8000"] ``` **Build + run** ```bash docker build -t fastapi-app:prod . docker run --rm -p 8000:8000 fastapi-app:prod ``` --- ## 3) Dev image vs Prod image (what to teach) ### Development container (hot reload + bind mount) Run (no Dockerfile required, but you can still use one): ```bash docker run --rm -it \ -p 8000:8000 \ -v "%cd%":/app -w /app \ python:3.12-slim \ bash -lc "pip install -r requirements.txt && uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload" ``` **Teaching point:** dev = bind mount + reload + bigger image acceptable. ### Production container (immutable + no reload) * Copy code into image * No bind mounts * Use pinned deps * Add healthchecks (optional) * Run as non-root (advanced best practice) --- ## 4) Next.js Dockerfile (production) + dev command ### Prod `Dockerfile` (Next.js) ```dockerfile FROM node:20-alpine AS deps WORKDIR /app COPY package*.json ./ RUN npm ci FROM node:20-alpine AS build WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . RUN npm run build FROM node:20-alpine AS runtime WORKDIR /app ENV NODE_ENV=production COPY --from=build /app ./ EXPOSE 3000 CMD ["npm", "run", "start"] ``` Dev run (bind mount): ```bash docker run --rm -it -p 3000:3000 -v "%cd%":/app -w /app node:20-alpine sh -lc "npm i && npm run dev" ``` --- ## 5) Docker Compose starter (FastAPI + Next.js + Postgres + Redis) `docker-compose.yml` ```yaml services: db: image: postgres:16 environment: POSTGRES_USER: app POSTGRES_PASSWORD: app POSTGRES_DB: appdb volumes: - pgdata:/var/lib/postgresql/data ports: - "5432:5432" redis: image: redis:7 ports: - "6379:6379" api: build: ./api environment: DATABASE_URL: postgresql://app:app@db:5432/appdb REDIS_URL: redis://redis:6379/0 ports: - "8000:8000" depends_on: - db - redis web: build: ./web ports: - "3000:3000" depends_on: - api volumes: pgdata: ``` Compose commands: ```bash docker compose up -d --build docker compose ps docker compose logs -f api docker compose down docker compose down -v ``` --- ## 6) AI Agents SDK + background tasks + persistency (how to explain in Docker terms) * **Background tasks** (FastAPI BackgroundTasks / Celery / RQ): run as **separate container** (a “worker”) or same container (not recommended for scale). * **Persistency**: use **volumes** for DB, vector DB, file storage, etc. * **Networking**: containers talk by **service name** in compose (`db`, `redis`, `api`). If you want a clean “agents + worker” pattern: * `api` container (HTTP) * `worker` container (process jobs) * `redis` (queue) * `db` (state)