Docker

Docker
BuildKit (since 18.09+) — Enable by DOCKER_BUILDKIT=1

v20.10.6 — 2021.04.12

  • Dockerfile — Recipe to build Image
  • Image — All softwares we need to run container
  • Container — Instance of started an image
  • Compose — Recipe to build multiple containers

docker -> make environment as an image (server, redis, database, etc.) -> image host on docker hub

docker build
docker build -f Dockerfile.postgres .

A tool for defining and running multi-container Docker applications.

Dockerfile Reference

# Comment
INSTRUCTION arguments

Available Instructions:

  • WORKDIR — Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
  • FROM — specify the parent image you’re building from.
  • FROM ... AS ...
  • ENV — sets env var, will be available for all subsequent instructions, will be persisted in the final image
  • RUN
  • ADD — Adds new files, directories or remote file URLs from specified source and adds them to the filesystem of the image at the path specified destination.
    ADD Gemfile Gemfile.lock package.json yarn.lock /home/app/your-app-name/
    
  • CMD
  • ENTRYPOINT — specify a script to run
  • EXPOSE — expose a port
  • COPY — Copies new files, directories or remote file URLs from specified source and adds them to the filesystem of the image at the path specified destination.
    COPY Gemfile Gemfile.lock package.json yarn.lock /home/app/your-app-name/
    
  • ARG — Defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag. Not persisted in the final image:
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get update && apt-get install -y ...
    
  • VOLUMN — Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers.

Dockerfile tells docker how to build an image to run your application. Must begin with a FROM instruction.

Here is an example Dockerfile for Ruby on Rails application:

FROM

$ docker run -it -v $(pwd):/app -w /app rubylang/ruby:3.0.0-preview1-bionic bash
Unable to find image 'rubylang/ruby:3.0.0-preview1-bionic' locally
3.0.0-preview1-bionic: Pulling from rubylang/ruby
f08d8e2a3ba1: Pull complete
3baa9cb2483b: Pull complete
94e5ff4c0b15: Pull complete
1860925334f9: Pull complete
434ea235d88d: Pull complete
f570e67a5e0c: Pull complete
ae0609335980: Pull complete
8d17f446c58f: Pull complete
14b29e11e63e: Pull complete
8bc4734d3ba1: Pull complete
Digest: sha256:04a9656eb5a0ffb41017de92a78963286819a984e39d842e67387bf1a417dd17
Status: Downloaded newer image for rubylang/ruby:3.0.0-preview1-bionic
root@8aba56178b02:/app# irb
irb(main):001:0> RUBY_VERSION
=> "3.0.0"