Get started with Docker Part 1 & 2

Dodkcer Get Started 隨手記。

Part 1: Orientation and setup

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.

The use of Linux containers to deploy applications is called containerization.

Images and containers

An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.

A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process).

Container vs VM

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

看起來 container 是 process level 的。

a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

用 Docker 可以把 application 需要的環境跟 code 等等包在 image 裡,以此 image 來開發跟 deploy,解決 application 的 system dependency 問題。因為整個環境都包在 Docker 裡了,application 可以在任何有 Docker 環境的地方執行(從 local 到 cloud 等等)。相較 VM,container 比較 light-weight 而且只有 application 需要的環境。

Part 2: Containers

hierarchy 由高到低:

  • Stack:define the interactions of all the services
  • Services:defines how containers behave in production
  • Container:a runtime instance of an image


Dockerfile defines what goes on in the environment inside your container.

container 的網路跟 disk drive 是 virtualized、跟外界隔開的,所以要做 port mapping 以及指定哪些檔案要 copy 進 container。

Dockerfile 指令 [Ref]:

  • FROM 指定 parent image
  • WORKDIP 指定 container 內的 working dir
  • COPY copy 檔案到 container 裡
  • RUN 執行指令
    • The RUN instruction will execute any commands in a new layer on top of the current image and commit the results.

  • EXPOSE 表示 container 會 listen 哪些 port
    • docker run -p 做 port mapping 不同,不會真的 publish port
    • The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published.

    • 寫不寫 EXPOSE 都可以 publish port,EXPOSE 是讓使用 image 的人知道 container 會 listen 哪個 port,好知道怎麼 publish port。
  • ENV 設定環境變數
  • CMD container 開起來時要執行的指令


  • 在 container 裡拿 hostname 會拿到 container ID

更多 docker run

  • -p <host port>:<container port> publish port,做 host port 跟 container port 的 mapping。[Ref]
  • -d 以 detach mode 執行 container,就是背景執行啦~

基本 docker run 使用 Ref

Share image

A registry is a collection of repositories, and a repository is a collection of images

docker 預設使用的 registry 是 Docker Hub

tag 是用來給 image 版本的方式。

  1. 幫 image 加上 registry 的 repository tag:
$ docker tag <image> <username>/<repository>:<tag>
  1. publish image:
$ docker push <username>/<repository>:<tag>
  1. pull and run image from remote repository:
$ docker run <username>/<repository>:<tag>

如果 local 不存在 image,Docker 會從 repository pull image 下來再執行。