Docker 常用命令

命令行的组成结构:

Commands:
 
attach    Attach to a running container
build     Build a container from a Dockerfile
commit    Create a new image from a container's changes
diff      Inspect changes on a container's filesystem
export    Stream the contents of a container as a tar archive
history   Show the history of an image
images    List images
import    Create a new filesystem image from the contents of a tarball
info      Display system-wide information
insert    Insert a file in an image
inspect   Return low-level information on a container
kill      Kill a running container
login     Register or Login to the Docker registry server
logs      Fetch the logs of a container
port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
ps        List containers
pull      Pull an image or a repository from the Docker registry server
push      Push an image or a repository to the Docker registry server
restart   Restart a running container
rm        Remove a container
rmi       Remove an image
run       Run a command in a new container
search    Search for an image in the Docker index
start     Start a stopped container
stop      Stop a running container
tag       Tag an image into a repository
version   Show the Docker version information
wait      Block until a container stops, then print its exit code

运行

启动一个ubuntu 容器

docker run -i -t ubuntu /bin/bash

后台运行(-d)、并暴露端口(-p)

docker run -d -p 127.0.0.1:33301:22 centos6-ssh

从容器中拷贝文件出来

docker cp 7bb0e258aefe:/etc/debian_version .

拷贝7bb0e258aefe中的/etc/debian_version到当前目录下。

注意:只要7bb0e258aefe没有被删除,文件命名空间就还在,可以放心的把exit状态的container的文件拷贝出来

运行一个新容器,同时为它命名、端口映射、文件夹映射。以redmine镜像为例

docker run --name redmine -p 9003:80 -p 9023:22 -d -v /var/redmine/files:/redmine/files -v /var/redmine/mysql:/var/lib/mysql sameersbn/redmine

一个容器连接到另一个容器

docker run -i -t --name sonar -d -link mmysql:db tpires/sonar-server sonar

容器连接到mmysql容器,并将mmysql容器重命名为db。这样,sonar容器就可以使用db的相关的环境变量了。

查看

查看所有镜像

docker images

查看容器的root用户密码

docker logs <容器名orID> 2>&1 | grep '^User: ' | tail -n1

因为docker容器启动时的root用户的密码是随机分配的。所以,通过这种方式就可以得到redmine容器的root用户的密码了

查看容器日志

docker logs -f <容器名orID>

查看正在运行的容器

docker ps
docker ps -l 显示最后一次创建的容器,包括未运行的 docker ps
-a 为查看所有的容器,包括已经停止的。

查看容器的输出

# 启动top命令,后台运行
$ ID=$(sudo docker run -d ubuntu /usr/bin/top -b)
# 获取正在running的container的输出
$ sudo docker attach $ID
top - 02:05:52 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.2%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    373572k total,   355560k used,    18012k free,    27872k buffers
Swap:   786428k total,        0k used,   786428k free,   221740k cached
^C$
$ sudo docker stop $ID

 查看容器内IP

docker inspect 941cb1f723e8 | grep IPAddress

 登陆容器

docker exec -it 1b05c125a0df /bin/bash

库操作

拉取镜像

docker pull <镜像名:tag>
docker pull sameersbn/redmine:latest

当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。

机器A

docker save busybox-1 > /home/save.tar

使用scp将save.tar拷到机器B上,然后:

docker load < /home/save.tar

构建

构建自己的镜像

docker build -t <镜像名> <Dockerfile路径>

如Dockerfile在当前路径:

docker build -t xx/gitlab .

构建镜像不使用缓存

# . 指当前目录Dockerfile所在位置
docker build -no-cache -t saint/tomcat7 .

停止

停止所有容器

docker stop $(docker ps -a -q)

停止、启动、杀死一个容器

docker stop <容器名orID>
docker start <容器名orID>
docker kill <容器名orID>

删除

删除单个容器

docker rm <容器名orID>

删除所有容器

docker rm $(docker ps -a -q)

删除指定镜像

docker rmi <image id>

删除id为<none>的镜像

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

删除所有镜像

docker rmi $(docker images -q)

修改提交

#将一个容器固化为一个新的镜像,后面的repo:tag可选
docker commit e0758 myubuntu/sohu

提交到私有仓库

docker tag 6e244 10.58.9.84:5000/ubuntu_ssh
docker push 10.58.9.84:5000/ubuntu_ssh

查询private registry

curl -X GET http://10.58.9.84:5000/v1/search?q=

http://fossies.org/dox/docker-1.5.0/

原文地址:https://www.cnblogs.com/saintaxl/p/3987286.html