Skip to main content

Intro to Docker 入门与介绍

01 Overview

Why use Docker?

Docker wants to make it really easy and straight forward for run software or install in any computer as in webserver as well without worrying about all bunch of setup or dependencies. Docker 希望让运行软件或在任何计算机(如网络服务器)中安装变得非常简单和直接,而无需担心所有设置或依赖项。

How does Docker work?

Advantages of Docker containers

02 Playground

Hello world

Running the run command with the -it flags attaches us to an interactive tty in the container. Now we can run as many commands in the container as we want. Take some time to run your favorite commands.

  • -it: This is a combination of two flags. -i stands for "interactive mode(交互模式),which ensures that the container's standard input (STDIN) stays open. -t allocates a pseudo-terminal (tty), which allows interacting with the container via the terminal. 交互模式与虚拟终端
  • sh: This is command executed within the container, which starts a shell. 在容器中运行的命令
docker run -it busybox sh

ls
# bin dev etc home lib lib64 proc root sys tmp usr var

ping google.com

# PING google.com (142.250.217.142): 56 data bytes
# 64 bytes from 142.250.217.142: seq=0 ttl=36 time=16.311 ms
# 64 bytes from 142.250.217.142: seq=1 ttl=36 time=16.544 ms

echo hello-world
# hello-world

List images

  • 列表包含了 仓库名, 标签, 镜像 ID, 创建时间 以及 SIZE
  • 镜像 ID 则是镜像的唯一标识,一个镜像可以对应多个 TAG
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 5f515359c7f8 5 days ago 183 MB
nginx latest 05a60462f8ba 5 days ago 181 MB
mongo 3.2 fe9198c04d62 5 days ago 342 MB
<none> <none> 00285df0df87 5 days ago 342 MB
ubuntu 18.04 329ed837d508 3 days ago 63.3MB
ubuntu bionic 329ed837d508 3 days ago 63.3MB

Custom Docker

mkdir redis-image

cd redis-image

# create a Dockerfile
...

# build the image
docker build .
# Dockerfile content

# download base image 基础镜像
# will check local first, and then fetch from docker hub
FROM alpine

# Download and install dependecies
RUN apk add -update redis

# Tell the image what to do when it starts as a container
CMD ["redis-server"]

03

Reference