Docker学习(一)
November 19, 2019 Docker 访问: 28 次
之前也了解过一些关于docker
的知识,但是学的也是太浅了,决定要系统的学习一下。
参考文献
介绍
介绍的话我这里就不再多嘴了,网上资料很多的
组成
简单的来说,docker由镜像、容器、仓库
镜像:包含了一个完整的操作系统。查看本地镜像命令:docker images
容器:它是真正提供服务,消耗资源的东西。
仓库:用于存放镜像。类似于github
比较
和虚拟机相比,docker
更快,性能更好,更省资源。
命令给指令
➜ ~ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
常用的命令
docker images
:查看本地的镜像文件
docker search
:搜索指定名字的镜像文件
docker ps
:列出运行的docker
docker rm
:删除指定的容器
docker rmi
:删除指定的镜像文件
docker stop
:停止正在运行的docker
docker start
:开启已经停止的docker
docker restart
:重启docker
获取镜像
docker search
:查找指定名字的镜像文件
docker pull
:将镜像文件下载到本地
Docker ps 命令
ps命令用来列出容器
CONTAINER ID
:docker
容器ID
IMAGE
:docker
容器所使用的镜像
COMMAND
:启动容器时运行的命令
CREATED
:容器的创建时间
PORTS
:端口和宿主机映射关系和网络传输协议的类型
NAMES
:docker
容器的name
STATUS
:docker
的状态
* created
(已创建)
* restarting
(重启中)
* running
(运行中)
* removing
(迁移中)
* paused
(暂停)
* exited
(停止)
* dead
(死亡)
该命令由其下面参数:
-a : 列出所有的容器信息,包括未正在运行 eg:docker ps -a
--filter : 根据条件过滤显示内容 eg:docker ps --filter "[name/status/ancestor/ancestor]=ctf_pwn"
--format : 格式化输入容器的信息 eg:docker ps --format "docker is {{.Names}}",占位符下面已列出
-l : 输入最近创建的容器
-n : 列出最近创建的n个容器
-q : 只显示容器的ID号
-s :显示总的容器大小
占位符 | 描述 |
---|---|
.ID | 容器ID |
.Image | 容器所使用镜像的name |
.Command | ... |
.Ports | ... |
.Status | ... |
.Size | ... |
.Names | ... |
.Networks | ... |
Docker run命令
该命令是创建一个新的容器并运行一个命令
docker run [options] image
OPTIONS说明:(直接从菜鸟教程上搬过来了)
- -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-P: 随机端口映射,容器内部端口随机映射到主机的高端口
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="nginx-lb": 为容器指定一个名称;
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
-h "mars": 指定容器的hostname;
-e username="ritchie": 设置环境变量;
--env-file=[]: 从指定文件读入环境变量;
--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
-m :设置容器使用内存最大值;
--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
--link=[]: 添加链接到另一个容器;
--expose=[]: 开放一个端口或一组端口;
--volume , -v: 绑定一个卷
commit建立镜像
大多数不用这个,因为这个缺点很多