Docker Custom Image

  • Developer –> Define Dockerfile –> Creates Docker Image

Dockerfile

  • Dockerfile is used to create custom docker image
  • It’s a text document that contains all the instructions users provide to assemble an image.
  • Instructions specify what to do when building the image.
  • Can have –> runtime env, libraries, environment variables, and configuration files.

Build Image

  • Create Dockerfile file
    • Write all the commands
# Dockerfile

# step 1: Base image
FROM alpine
# alpine is an OS, min docker image, based on Alpine Linux
# Its small, simple and secure

# step 2: Install a software
RUN apk add binutils

# step 2.5: Configure that software

# step 3: Set default commands
  • Build docker image from Dockerfile
docker build .

Dockerfile commands

# Dockerfile

FROM ubuntu
MAINTAINER Amrit Prasad
RUN apt-get update
CMD ['echo', 'Hello World... from my first docker image']
  • FROM –> Base image
  • RUN –> Executes while build
  • CMD –> Executes when container is created

Install mongo

# step 1: Base image
FROM alpine

# step 2: Install a software
# Alpine doesn't know where to find mongo, So add these
# GEt here: http://dl-cdn.alpinelinux.org/alpine/
RUN echo 'https://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories
RUN echo 'https://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories
RUN apk add mongodb=3.4.4-r0

# step 2.5: Configure that software
VOLUME ["/data/db"]
WORKDIR /data
EXPOSE 27017

# step 3: Set default commands
CMD ["mongod"]
  • Build docker image from Dockerfile
# No image name
docker build .

# With image name and tag
docker build -t mymongo .

# Start/Run --> Any
docker run mymongo 
docker start mymongo

# Get mongo shell --> In new terminal
docker exec -it mymongo sh
mongo
show dbs

Try Node App

  • Create a small node app
    • npm install express
# package.json
{
  "name": "mynode",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1"
  }
}
  • Create index.js
    • npm start –> visit localhost:8001
const express = require('express');
const app = express();

app.get("/", (req, res) => {
    res.send("<h1> Home Page</h1>");
})

app.listen(8001, () =>{
    console.log("App is running at 8001")
})
  • Create Docker Image
FROM node:alpine

WORKDIR /usr/nodeapp

# COPY my-dir docker-dir
COPY ./ ./

RUN npm install

# npm start
CMD ["npm", "start"]
  • Build and Run
    • visit localhost:8002
docker build -t nodeapp .

# Docker port is not linked to my-external port
docker run nodeapp

# docker run -p my-port:docker-port nodeapp
docker run -p 8002:8001 nodeapp

Nginx

  • Create small HTML page
    • index.html, xyz.css
# Dockerfile
FROM nginx

# It copies all the files from curr-dir to nginx-dir
copy . /usr/share/nginx/html
  • Build and run
docker build -t myweb .

docker run -p 8080:80 myweb

# visit localhost:8080/

Dynamic linking local and docker folder

  • Directly hosting from my local folder
    • Not for production
docker run -p 8080:80 -v $(pwd):/usr/share/nginx/html myweb

# Check --> no changes made in docker folder
docker run -p 8080:80
  • On change detect rebuild the container

More

# Dockerfile

FROM python:3.7-alpine
* Base Image
* alpine -- light version of py

MAINTAINER London App Developer Ltd
* Optional  -- 

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
* Copy req.txt from project to Docker image

RUN apk add --update --no-cache postgresql-client jpeg-dev
* apk --> package manage that comes with alpine
* add -->add package
* --update --> update registry before update
* --no-cache --> Don't store this files & packages(Coz make container small)
* jpeg-dev
  * Req for `Pillow`

RUN apk add --update --no-cache --virtual .tmp-build-deps \
      gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
* install temp required dependencies that are needed to install req.txt
* musl-dev zlib zlib-dev --> for `Pillow`
* Other for --> `psycopg2`

RUN pip install -r /requirements.txt
* Take req.txt and install it

RUN apk del .tmp-build-deps
* Remove those temp files

RUN mkdir /app
* Make a empty dir

WORKDIR /app
* Switch it as default/currrent dir

COPY ./app /app
* Copy our project to docker image

RUN adduser -D user
* Create user to run-only(-D) the project
USER user
  * Switch to user