Docker 經常使用命令

命令行的組成結構:mysql

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

 

運行git

啓動一個ubuntu 容器sql

docker run -i -t ubuntu /bin/bash

後臺運行(-d)、並暴露端口(-p)docker

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

從容器中拷貝文件出來ubuntu

docker cp 7bb0e258aefe:/etc/debian_version .

拷貝7bb0e258aefe中的/etc/debian_version到當前目錄下。centos

注意:只要7bb0e258aefe沒有被刪除,文件命名空間就還在,能夠放心的把exit狀態的container的文件拷貝出來緩存

 

運行一個新容器,同時爲它命名、端口映射、文件夾映射。以redmine鏡像爲例tomcat

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

一個容器鏈接到另外一個容器bash

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

容器鏈接到mmysql容器,並將mmysql容器重命名爲db。這樣,sonar容器就能夠使用db的相關的環境變量了。ssh

 

查看

查看全部鏡像

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/

相關文章
相關標籤/搜索