Docker学习笔记
Docker是什么?
docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。
Docker能干什么?
web应用的自动化打包和发布;
自动化测试和持续集成、发布;
在服务型环境中部署和调整数据库或其他的后台应用;
Docker的常用命令
帮助命令
docker version # 显示 docker 的版本信息
docker info # 显示 docker 更详细的系统信息
docker 命令 --help # 帮助命令
镜像命令
docker images
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
列出镜像
Options:
-a, --all 显示所有镜像
--digests 显示摘要
-f, --filter filter 根据提供的条件过滤输出
--format string 使用GO模板打印镜像
--no-trunc 不截断输出
-q, --quiet 仅显示镜像ID
docker search 镜像
Usage: docker search [OPTIONS] TERM
在 Docker Hub 中搜索镜像
Options:
-f, --filter filter 根据提供的条件过滤输出
--format string Pretty-print search using a Go template
--limit int 最大搜索条数 (default 25)
--no-trunc 不截断输出
docker pull 镜像:tag版本
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags 下载库中所有的镜像
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
docker rmi 镜像ID
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force 强制删除镜像
--no-prune Do not delete untagged parents
# 删除单个镜像
docker rmi -f 镜像ID
# 删除多个镜像
docker rmi -f 镜像ID 镜像ID
# 删除全部镜像
docker rmi -f $(docker images -qa)
容器命令
注意:有了镜像才可以创建容器
docker run
新建容器并启动
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--name string # 为容器指定一个名称(Assign a name to the container)
-d, --detach # 在后台运行容器并返回容器ID(Run container in background and print container ID)
-it # 使用交互方式运行,进入容器查看内容
-p # 指定容器的端口 -p 端口:端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口 # 常用
-p 容器端口
容器端口
-P # 随机指定端口
# 启动容器并进入其中
$docker run -it 4e /bin/bash
docker ps
列出容器
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all # 显示所有容器,包含所有状态(Show all containers (default shows just running))
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest # 显示最新创建的容器,包含所有状态(Show the latest created container (includes all states))
--no-trunc Don't truncate output
-q, --quiet # 仅显示容器的ID(Only display container IDs)
-s, --size # 显示文件总大小(Display total file sizes)
退出容器
exit #停止并推出
ctrl + P + Q # 不停止推出
docker rm
删除容器
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force # 强制删除容器,包含正在运行的(Force the removal of a running container (uses SIGKILL))
-l, --link Remove the specified link
-v, --volumes # 删除与容器管理的匿名卷(Remove anonymous volumes associated with the container)
# 删除指定容器
docker rm -f 容器ID
# 删除全部容器
docker rm -f $(docker ps -aq)
docker ps -a -q | xargs docker rm
启动和停止容器
docker start 容器ID # 启动容器
docker restart 容器ID # 重启容器
docker stop 容器ID # 停止容器
docker kill 容器ID # 杀掉容器
常用其他命令
后台启动容器
$ docker run -d 镜像
# 容器运行后如果发现自己没有提供服务,没有前台应用,会自动停止
查看日志
$ docker logs
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow # 跟踪日志输出(Follow log output)
--since string # 显示指定时间以后的日志 (e.g. 2013-01-02T13:23:37Z)
-n, --tail string # 从最底部计算显示日志的条数 (default "all")
-t, --timestamps # 显示时间(Show timestamps)
# 显示 10 条日志
$ docker logs -tf --tail 10 4e
查看容器中进程
docker top 容器ID
查看容器的信息
docker inspect
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
进入当前正在运行的容器
docker exec
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
# 测试
$ docker run -it 4e /bin/bash
# 退出
ctrl + p + q
# 重新进入
$ docker exec -it 044 /bin/bash
docker attch
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
docker exec # 进入容器后开启一个新的终端,可以在里面进行操作(常用)
docker attach # 进入容器正在执行的终端,不会开启新的进程
从容器内拷贝文件到主机上
docker cp 容器ID:容器内路径 主机内路径
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
在容器和本地主机间复制文件或文件夹
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
提交容器到镜像
docker commit
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
根据容器的更改创建新的镜像
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause 提交期间暂停容器(default true)
容器数据卷
挂载目录
docker run -v 主机目录:容器目录 容器ID
匿名和具名挂载
匿名挂载
docker run -v 容器内目录 容器ID
具名挂载
docker run -v 卷名:容器内目录 容器ID
docker volume
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
拓展:
docker run -v 主机目录:容器目录:ro 镜像ID
docker run -v 主机目录:容器目录:rw 镜像ID
ro # read only 只读 容器内无法改写
rw # read write 可读写 (默认)
DockerFile
介绍
dockerfile 是构建docker 镜像的文件,命令参数脚本!
步骤:
1.编写一个 dockerfile 文件;
2.docker build 构建一个镜像;
3.docker run 运行镜像;
4.docker push 发布镜像到 docker hub 或私有仓库;
构建过程
基础知识
1.每个保留关键字都必须是大写字母;
2.执行从上到下顺序执行;
3.# 为注释;
4.每个指令都会创建提交一个新的镜像层。,并提交。
DockerFile指令
FROM # 基础镜像,一切从这里开始构建
MAINTAINER # 镜像作者信息。姓名+邮箱
RUN # 镜像构建时需要运行的命令
ADD # 添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 保留端口配置
CMD # 指定这个容器启动时需要运行的命令,只有最后一个生效,可被替代
ENTRYPOINT # 指定这个容器启动时需要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 DockerFile ,这个时候会运行 ONBUILD 命令,触发指令
COPY # 类似 ADD , 将文件拷贝到镜像中,
ENV # 构建的时候设置环境变量
构建镜像
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
-o, --output stringArray Output destination (format: type=local,dest=path)
--platform string Set platform if server is multi-platform capable
--progress string Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--secret stringArray Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret
--ssh stringArray SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format:default|<id>[=<socket>|<key>[,<key>]])
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.