整理的一些常用指令
指令 | 解释 |
---|---|
docker version | #查看版本 |
docker search centos | #搜索可用 docker 镜像 |
systemctl start docker | #启动 |
systemctl daemon-reload | #守护进程重启 |
systemctl restart docker | #重启docker服务 |
systemctl stop docker | #关闭docker |
docker images | #查看当前 docker 所有镜像 |
docker pull centos | #下载镜像 |
cat centos.tar | docker import - centos6 |
docker export id > cenos6.tar | #Docker 导出镜像,可以直接导出压缩包 |
docker run centos echo “hello word” | #在 docker 容器中运行 hello world! |
docker run centos yum install ntpdate | #在容器中安装 ntpdate 的程序 |
docker ps -l | #命令获得最后一个容器的 id,docker ps -a 查看所有的容器。 |
docker commit 容器id centos:v1 | #运行 docker commit 提交刚修改的容器,大改建议用dockerfile进行 |
docker commit -a 作者名 -m “备注” 正运行的容器id 起个版本更新后的镜像名 | #一定要是正在运行的容器,然后ctrl+p+q进入后台运行,再提交修改 |
docker run -it imagesName:tag bash | #在容器里启动一个/bin/bash shell 环境,可以登录进入操作,其中-t 表示打开一个终端的意思,-i 表示可以交互输入。 |
docker run -d imagesName:tag /bin/bash | #-d 表示在后台启动,以 daemon 方式启动。 |
docker ps -a | #查看当前后台运行的容器 |
docker attach 容器id | #进入到后台运行的容器中去 |
docker run -d -p 80:80 -p 8022:22 centos:v2 | #解析:-p 指定容器启动后 docker 上运行的端口映射及容器里运行的端口,80:80,第一个 80 表示docker 系统上的 80,第二个 80 表示 docker 虚拟机里面的端口。用户默认访问本机 80 端口,自动映射到容器里面的 80 端口。 |
docker stop id | #关闭容器 |
docker start id | #启动某个容器 |
docker restart myrunoob | #重启某个容器 |
docker rm id | #删除容器,docker rmi images 删除镜像 |
docker save -o filename.tar filename | #将filename压缩为filename.tar output |
docker load -i filename.tar | #加载镜像 input |
docker帮助文档
A self-sufficient runtime for containers
docker Commands: | 解释 |
---|---|
builder | Manage builds |
checkpoint | Manage checkpoints |
config | Manage Docker configs |
container | Manage containers |
engine | Manage the docker engine |
image | Manage images |
manifest | Manage Docker image manifests and manifest lists |
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 |
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 |
deploy | Deploy a new stack or update an existing stack |
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 卷的管理
#docker-managed volume自管理卷
docker run -it --name <name> -v <mountdir> <image:version>
docker inspect -f {{.Mounts}} <imagename>
自管理卷需要在打镜像的时候就定义好挂载点(持久化存储)格式为
VOLUME dir
#bind-mount volume
docker run -it --name <name> -v <hostdir:volumedir > <image:version>
#union volume
docker run -it --name <name> --volumes-from <containName> <image:version>
docker 容器之间实现文件共享
方法一:使用nfs
方法二:通过宿主机同一个卷实现数据同步
docker run --name test01 -v /data:/var -d test:v1.0
docker run --name test02 -v /data:/var -d test:v1.0
方法三:使用union volume联合卷
不使用宿主机实现共享:在启动第二个容器时声明使用第一个容器的卷,需要第一个容器使用自管理卷(docker-managed volume)实现持久化存储。
docker run --anme test2 -d test:v2.0#默认test:v2.0打镜像已经实现了自管理卷
dicker run --name test22 -d --volumes -from test2 -d test:v2.0
这里创建的test22就是使用test2的卷,实现了容器共享
删除持久画卷
删除容器的时候默认不删除持久化卷,如果想删除持久化卷,就加个参数-v
docker rm -rf -v test2
存储驱动
docker info3#查看信息
如果不是overlay,建议更改为overlay,更改方式为: