Basic Knowledge
- Docker 是基于 Linux 内核功能的虚拟化技术,因此只能在 Linux 内核上运行
- 在 Windows 和 Mac 系统上需要借助传统虚拟机
- 同一台物理机上的所有 Docker 镜像共用一个 Linux 内核,因此重启 Docker 实例不需要重启物理机
- 但是 Docker 也因此不能虚拟不同版本的 Linux 内核
- Docker 会使用镜像创建实例,实例也即一个运行中的虚拟机,镜像则是该虚拟机的初始文件系统
- Docker 的镜像默认是只读的。为了保存修改后的镜像需要 commit
- Docker 主要能使用主机的 CPU、内存、硬盘资源,其他硬件如 USB、显卡等的使用并不方便
How to run
Docker 在每台物理主机上有唯一的守护进程,运行各种命令都是与这一后台进程打交道,因此运行中的主机不会因为运行命令的窗口被关闭而终止。
Docker 客户端的使用非常简单,命令基本格式为 docker command params
, 如 docker run ubuntu /bin/bash
. 运行 docker help
能得到命令列表,运行 docker command --help
能得到某个 command 的帮助页面。
以下为一些基本管理命令。
Run and Stop
# "run" command will create a new instance
# to start an existed instance, using "start"
# download a default ubuntu image, create a VM, run a command:
docker run ubuntu /bin/echo "hello world"
# interactive
# -t means open a term in container
# -i means interactive
docker run -it ubuntu /bin/bash
# background mode
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
# stop a container
docker stop container_id_or_name
# note: id can be the first N alphabet, but the name must be the full name.
# attach a container (get it's running shell)
docker attach id_or_name
# run a new shell on a container
docker exec -it id_or_name /bin/bash
# run a container, map it's port. you can get the port map by `docker ps`
docker run -dP IMAGE
# map container port 5000 to host port 5000
docker run -d -p 5000:5000 IMAGE
# mount local folder
docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash
# run a stoped container
docker start container_id_or_name
# interactive
docker start -i container_id_or_name
Management
# list running container
docker ps
# list all container
docker ps -a
# remove a CONTAINER (not a image)
docker rm id_or_name
# remove all CONTAINER (be careful!!!)
docker rm $(docker ps -a -q)
# list all images
docker images
# remove image. get image id from `docker images`
docker rmi image_id
# search image
docker search image
# pull image
docker pull image
# commit a image from a container
# "v2" is the new tag for the image
docker commit -m="has update" -a="username" container_id username/ubuntu:v2
# push a image
docker push image_id
# save a image to local
docker save -o <save image to path> <image name>
# load a image from local
docker load -i <path to image tar file>
Harbor
# login
docker login harbor.example.com
# input username and password
# create a repo on the harbor web UI
# tag a image
docker tag existed_image_name harbor.example.com/repo_name/name:version
# release
docker push harbor.example.com/repo_name/name:version
Dockerfile minimal example
# filename: Dockerfile
FROM ubuntu
WORKDIR /root
COPY init_docker.sh .
RUN /bin/bash init_docker.sh
# VOLUME local_source target # mount volume
# Filename init_docker.sh
echo "Init docker"
apt-get update
apt-get install -y git
git clone https://github.com/plus2047/ITPC.git
echo "Done init docker"
Put those two file in dir and run command in this dir,
docker build -t study-docker:v0.3 --progress=plain --no-cache .
Then you can run the docker image,
docker run -it study-docker:v0.3