Docker

What is Docker ?

devops_techaid71
Written by shohal

Docker

Docker is a platform for packaging, deploying, and running applications.

Docker applications run in containers that can be used on any system: a developer’s laptop, systems on-premises, or in the cloud.

Containerization

Containerization is a technology that’s been around for a long time, but it’s seen new life with Docker. It packages applications as images that contain everything needed to run them: code, runtime environment, libraries, and configuration. Images run in containers, which are discrete processes that take up only as many resources as any other executable.

Docker Overview

Container Vs Virtual Machine

Container Vs Image

Container is a running instance of a image

Setup and Install Docker

sudo apt-get update

sudo apt-get install docker.io

sudo systemctl enable docker

sudo groupadd docker

sudo usermod -aG docker $USER

newgrp docker

docker –version 

Docker Commands

docker –version

docker  images

docker ps

docker ps -a

docker run -d -p 80:80 nginx

docker stop container_id

docker rm container_id

docker rmi image_id

Difference Between COPY and ADD

COPY and ADD are both Dockerfile instructions that serve similar purposes. They let you copy files from a specific location into a Docker image.

COPY <src> <dest>

ADD   <src> <dest>

ADD is when you want to extract a local tar file into a specific directory in your Docker image. This is exactly what the Alpine image does with ADD rootfs.tar.gz /.

Docker Images

nano Dockerfile

FROM python:3.6.4-slim

WORKDIR /app

COPY /app /app

RUN pip install -r /app/requirements.txt

CMD python /app/main.py

EXPOSE 5000

Docker Image Build & Run

docker build -t pythonapp .

docker run -d -p 80:5000 –name pythonapp pythonapp

ip a

Browse or curl http://192.168.0.117

Multistage Docker Build and Run

### STAGE 1: Build ###

FROM node:8.9-alpine AS build

WORKDIR /usr/src/app

COPY . /usr/src/app

COPY ./package*.json /usr/src/app

RUN npm install

RUN npm run build

### STAGE 2: Run ###

FROM nginx:1.17.1-alpine

ADD https://raw.githubusercontent.com/shawon100/Angular-CRUD/master/nginx.conf /etc/nginx/nginx.conf

COPY –from=build /usr/src/app/dist/crud /usr/share/nginx/html

Docker Run

docker build -t angularapp .

docker run -d -p 80:80 –name angularapp angularapp

ip a

Browse or curl http://192.168.0.117

Docker Container Commands

docker start container_id

  docker logs -f container_id

  docker logs –since=2h -f container_id

  docker exec -it container_id bash

Docker Compose

version: “2”

services:

  app:

    build: ./app

    links:

      – db

    ports:

      – “5000:5000”

  db:

    image: mysql:5.7

    ports:

      – “32000:3306”

    environment:

      MYSQL_ROOT_PASSWORD: root

    volumes:

      – ./db:/docker-entrypoint-initdb.d/:ro

Docker Compose

We are using two services, one is a container which exposes the REST API (app), and one contains the database (db).

build: specifies the directory which contains the Dockerfile containing the instructions for building this service

links: links this service to another container. This will also allow us to use the name of the service instead of having to find the ip of the database container, and express a dependency which will determine the order of start up of the container

ports: mapping of <Host>:<Container> ports.

image: Like the FROM instruction from the Dockerfile. Instead of writing a new Dockerfile, we are using an existing image from a repository. It’s important to specify the version — if your installed mysql client is not of the same version problems may occur.

environment: add environment variables. The specified variable is required for this image, and as its name suggests, configures the password for the root user of MySQL in this container. More variables are specified here.

ports: Since I already have a running mysql instance on my host using this port, I am mapping it to a different one. Notice that the mapping is only from host to container, so our app service container will still use port 3306 to connect to the database.

volumes: since we want the container to be initialized with our schema, we wire the directory containing our init.sql script to the entry point for this container, which by the image’s specification runs all .sql scripts in the given directory.

Docker Compose

docker-compose up

docker-compose up -d

docker-compose rm

MySQL DB Commands

mysql –host=127.0.0.1 –port=32000 -u root -p

show databases;

use knights;

show tables;

select * from favorite_colors;

For More : https://techaid71.xyz/devops/

About the author

shohal

Leave a Comment