什么是Docker?
[TOC]
关于Docker。
What is docker?
1.1 Docker Images
1.2 Container Isolation
container id 可以简写前几个字母 只要唯一即可。
1.3 Terminology
-
Images - the file system and configuration of our application.
-
Containers - Running instances of Dokcer images. Containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel wiht other containers, and run as isolated process in user space on the host OS.
docker container ls
a list of running containers. -
Docker caemon - the background service running on the host that manages building, running and distributing Docker containers.
-
Docker client - command line tool that allows the user to interact with the Docker daemon.
-
Docker Store - a directory of all availiable Docker images.
1.4 Image Creation
running an interative shell in a ubuntu container
docker container run -ti ubuntu bash
新建一个文件 index.js
var os = require("os");
var hostname = os.hostname();
console.log("hello from " + hostname);
建立一个Dockerfile文件
FROM alpine
RUN apk update && apk add nodejs
COPY . /app
WORKDIR /app
CMD ["node","index.js"]
构建镜像
docker image build -t hello:v0.1 .
启动容器
docker container run hello:v0.1
what just happend?
-
FROM alpine
specifies a base image to pull FROM alpine image we used in earlier labs. -
RUN apk update && apk add nodejs
RUN two commands(apk update and apk add) inside that container which installs the Node.js server. -
COPY . /app
COPY files from our dircetory in to the container. e.g. index.js we just created. -
WORKDIR /app
WORKDIR specify the WORKDIR the directory the container should use when it starts up. -
And finally, we gave our container a command(CMD) to run when the container starts.
With Dockerfile we can specify preceise commands to run for everyone who uses this container. Other users do not have to build the container themselves once you push your container up to a repository (which we will cover later) or even know what commands are used.
Terminology
-
Layers - A Docker image is built up a series of layers.
-
Dockerfile - A text file contains all the commands, in order, needed to build a given image.
-
Volumes - A special Docker container layer that allows data to persist and be shared separately from container itself.