1. 获取镜像:
docker pull [options] name[:tag|@digest]
docker pull ubuntu # 未指定tag时, 使用最新的镜像 :latest
docker pull java:8-jdk
docker images # 查看本地的镜像
2. 创建并运行容器:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
COMMAND为创建容器后在容器中立即执行的命令.
常用参数:
OPTIONS | 描述 |
---|---|
–attach , -a | Attach to STDIN, STDOUT or STDERR |
–cpus | Number of CPUs |
–detach, -d | Run container in background and print container ID |
–gpus | GPU devices to add to the container (‘all’ to pass all GPUs) |
–memory, -m | Memory limit |
–rm | Automatically remove the container when it exits |
–tty , -t | Allocate a pseudo-TTY |
–volume , -v | Bind mount a volume. docker run -v /path/on/host:/path/in/container mycontainer:tag |
–workdir , -w | Working directory inside the container |
–name | Assign a name to the container (就不用通过打容器的id来启动删除容器了…) |
docker run -it ubuntu /bin/bash
# 查看所有容器
docker ps -a
# 启动一个已经关闭的容器
docker start container-name/container-id # 用id和name都行, docer的基本所有命令name和id都能互换
# 进入一个后台运行容器
docker exec -it container-id /bin/bash # 从容器终端退出, 容器不停止
docker attach container-id # 从容器终端退出时, 容器会停止
3. 导出、导入和删除容器:
1. 导出本地的容器:
docker export container-id > file-name
2. 从文件导入创建镜像:
docker import file-name [img-name[:tag]]
# 或者这种方式也行:
cat ubuntu.tar | docker import - test/ubuntu:v1 # 这里的 - 是linux里的用法, 代表标准输出的内容(由管道而来)
3. 删除容器:
docker rm container-id
--force , -f | Force the removal of a running container (uses SIGKILL) |
---|---|
--link , -l | Remove the specified link |
--volumes , -v | Remove anonymous volumes associated with the container |
4. 镜像管理:
1. 查看本地镜像:
docker images
2. 查找镜像:
docker search httpd
3. 删除镜像:
docker rm hello-world # 指定镜像名即可
4. 创建镜像:
1. 从已经创建的容器中更新镜像, 并提交镜像
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
docker commit -m "update image" -a "liangliang" container-id liang/ubuntu:v2 # 最后要指定创建的镜像名字
Name, shorthand | Default | Description |
---|---|---|
--author , -a | Author (e.g., “John Hannibal Smith hannibal@a-team.com”) | |
--change , -c | Apply Dockerfile instruction to the created image | |
--message , -m | Commit message | |
--pause , -p | true | Pause container during commit |
2. 使用Dockerfile指令创建新的镜像
docker build -t liang/ubuntu .
# -t 指定镜像名
# . 指定Dockerfile位置, 为当前位置
Dockerfile是docker使用过程中非常重要的一环, 后续会单独记笔记