Create custom Docker Image
- Custom Docker image can be created in two ways:
- Container
- Dockerfile
Container
Create image with ubuntu + apache + git + java
docker run -itd ubuntu
docker exec -it <containedID> /bin/bash
# inside docker
apt-get update
apt-get install apache2
which apache2
apt install git
apt-get install openjdk-11-jdk -y
exit
docker exec <containedID> which apache2
docker commit -m "ubuntu+apache+git+java" -a "Rajesh Kumar" <containedID> containerName
Dockerfile
- Dockerfile is used to
create custom docker image - It’s a
text documentthat 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.
Developer --> Define Dockerfile --> Creates Docker Image
3.1 Build Image
Codes: https://github.com/amrit94/Docker/tree/master/01-level1/01-test-dockerfile
Create Dockerfile file and write all the commands
# Dockerfile --> filename
# Step 1: Base image
FROM alpine
# alpine is an OS, min docker image, based on Alpine Linux
# Its small, simple and secure
# Step 2: Executes while build and Install a software
RUN apk add binutils
# Step 2.5: Configure that software
# Step 3: Executes when container is created
# Set default commands
CMD ["echo", "Hello World... from my first docker image"]
Build and run docker image from Dockerfile
docker build -t ap/dockerfile:hi .
docker run --name test1 ap/dockerfile:hi
# Hello World... from my first docker image
Remove the image
docker rmi -f <id>
Concept of caching in docker
Node project for docker
Codes(Using Node base-image): https://github.com/amrit94/Docker/tree/master/01-level1/02-node-express-js
Codes(Using Ubuntu base-image): https://github.com/amrit94/Docker/tree/master/01-level1/03-ubuntu-express-js
Alt
Update Dockerfile CMD ["node", "index.js"] to CMD ["npm", "start"]
Update package.json - from this
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
to this
"scripts": {
"start": "node index.js"
}
Performance in Docker
Install mongo
Create Dockerfile file and write all the commands
# 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 .
docker run mymongo
# Get mongo shell --> In new terminal
docker exec -it 3c4c643b3ec0 sh
mongo
show dbs
3.7 Flask Python App
Create flask app
# app.py
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return f'Hello Flask'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Create Dockerfile
# DockerFile
FROM amritkr6/python-3.6.15:v1
WORKDIR /app
# Copy the requirements file and install dependencies
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY app.py app.py
# Expose the port the app runs on
EXPOSE 5000
# Command to run the application
CMD ["python", "app.py"]
Create requirements.txt file
# requirements.txt
Flask
Build and Run –> visit localhost:5000
docker build -t flask-app .
docker run -p 5000:5000 flask-app
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
Previously if we make any changes in project dir/files, each time have to build and start the image
After linking the project dir as docker volume, rebuild is not required
docker run -it -v <local_dir>:<docker_dir> <Image> bash
docker run -it -v $(pwd):/root/app amritkr6/python-3.6.15:v1 bash
- 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
Create Custom docker volume
docker volume create custom_vol
docker volume ls | grep cus
docker run -it --rm -v custom_voll:/server ubuntu
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