0x00:介紹
Docker 屬於 Linux 容器的一種封裝,提供簡單易用的容器使用接口。它是目前最流行的 Linux 容器解決方案。Docker 將應用程序與該程序的依賴,打包在一個文件裏面。運行這個文件,就會生成一個虛擬容器。程序在這個虛擬容器裏運行,就好像在真實的物理機上運行一樣。有了 Docker,就不用擔心環境問題。 虛擬機和Docker不同,虛擬機是模擬了硬件和環境,安裝時需要選擇硬件屬性等,而Docker 只有環境,其硬件是使用主機的硬件,比虛擬機更加方便快捷。
0x01:安裝
Docker安裝有很多教程,這裏我放一個教程
https://baijiahao.baidu.com/s?id=1592846051245987256&wfr=spider&for=pc
0x02:基本使用
如果你的docker安裝完畢,在命令行中輸入 docker 顯示瞭如下信息代表安裝成功
root@Thunder_J-virtual-machine:~/桌面# docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/Thunder_J/.docker")
-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 "/home/Thunder_J/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/Thunder_J/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/Thunder_J/.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
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.
Doker信息查詢
操作 | 命令 | 說明 |
---|---|---|
版本 | docker version | 查看docker版本 |
信息 | docker info | 查看詳細的docker信息 |
歷史 | docker history 容器名或id | 查看docker修改歷史 |
關於 docker 的查找
- 可以在hub.docker.com上查找需要下載的docker
- 輸入命令docker search xxx,AUTOMATED表示是否是官方提供的,ok表示是官方認證,STARS表示收藏人數,下面我搜索與pwn有關的docker顯示如下
root@Thunder_J-virtual-machine:~/桌面# docker search pwn
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
superkojiman/pwnbox Docker container with tools for binary rever… 6 [OK]
pwntools/pwntools CTF framework and exploit development library 4 [OK]
skysider/pwndocker a docker environment for pwn 4 [OK]
grazfather/pwndock Pwning docker 3
stefanscherer/pwned-passwords Docker image to search for pwned passwords 3
pwntr/samba-alpine Simple and lightweight Samba docker containe… 2 [OK]
edibledinos/pwnbook Create a container which runs pwnypack's pwn… 1 [OK]
gedigi/pwntainer A Docker container for pwning 1 [OK]
pwntr/nzbget-alpine A simple and super lightweight nzbget docker… 0 [OK]
pwntus/visualbox-node 0
pwntr/headphones-alpine A simple and super lightweight headphones do… 0 [OK]
moreapps/pwn-hook pwn-hook 0
edibledinos/pwnypack Docker image to run pwnypack shell. 0 [OK]
pwntr/minimserver-alpine A simple and super lightweight MinimServer D… 0 [OK]
robertlarsen/pwntools 0
pwnwb/busybox 0
lflare/pwnbox 0
ryarnyah/docker-pwndbg docker-pwndbg 0 [OK]
bsidespdx2018/pwn200 0
pwntr/acd_cli-alpine A simple and lightweight acd_cli docker cont… 0 [OK]
iotsec/pwnable pwnable image for the iotsec box 0
glarsen/apex-pwn-ret2lost 0
torque59/pwnlabs A kubernetes pwn cluster 0
bsidespdx2018/pwn300 0
n0b0dyvn/pwnctf Docker Images for CTF play. Include gdb peda… 0
systemclt命令
操作 | 命令 | 說明 |
---|---|---|
查看 | systemctl status docker | 查看docker狀態 |
關閉 | systemctl stop docker | 關閉docker |
開啓 | systemctl start docker | 開啓docker |
重啓 | systemctl restart docker | 重啓docker |
開機自動啓動 | systemctl enable docker | 設置開機自動啓動 |
測試運行
輸入 docker run hello-world 命令下載hello-world鏡像並運行
root@Thunder_J-virtual-machine:~/桌面# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
上面是通過在docker官方網站下載的鏡像,建議註冊一個阿里雲的賬號(dev.aliyun.com),在阿里雲容器鏡像服務中找到自己專屬的加速器地址並更改路徑中的daemon.json文件爲自己的加速器地址,這樣下載容器速度會快很多
vi /etc/docker/daemon.json
xxx 修改爲你的專屬加速地址
{
"registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
鏡像操作
操作 | 命令 | 說明 |
---|---|---|
拉取 | docker pull xxx:tag | 拉取鏡像,tag爲版本信息,如果不指定版本默認爲latest |
查看 | docker images | 查看本地鏡像 |
元信息 | docker inspect 容器id | 獲取容器的元信息 |
刪除 | docker rmi 鏡像id或鏡像名 | 刪除指定的本地鏡像,加-f表示強制刪除 |
查看鏡像
root@Thunder_J-virtual-machine:~/桌面# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 4ab4c602aa5e 6 months ago 1.84kB
刪除指定的本地鏡像
root@Thunder_J-virtual-machine:~/桌面# docker rmi 4ab4c602aa5e
Error response from daemon: conflict: unable to delete 4ab4c602aa5e (must be forced) - image is being used by stopped container 2c8d8795bd2e
root@Thunder_J-virtual-machine:~/桌面# docker rmi -f 4ab4c602aa5e
Untagged: hello-world:latest
Untagged: hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Deleted: sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f
容器操作
操作 | 命令 | 說明 |
---|---|---|
運行 | docker run --name 容器名 -i -t -p 主機端口:容器端口 -d 鏡像id或鏡像名稱:tag | --name指定容器名,名稱自定義,如果不指定則會自動命名;-i 以交互模式運行;-t 分配一個僞終端,通常組合使用 -it;-p 將主機端口映射到容器端口;-d 表示後臺運行 |
列表 | docker ps -a -q | 查看正在運行的容器,-a 表示顯示所有容器,-q 表示只顯示容器id |
啓動 | docker start 容器id或容器名稱 | 啓動容器 |
停止 | docker stop 容器id或容器名稱 | 停止正在運行的容器 |
刪除 | docker rm -f 容器id或容器名稱 | 刪除容器,-f 表示強制刪除 |
全部刪除 | docker rm -f $(docker ps -aq) | 刪除全部容器 |
日誌 | docker logs 容器id或容器名稱 | 獲取容器的日誌 |
在容器中執行 | docker exec -it 容器id或容器名稱 /bin/bash | 進入正在運行的容器中並開啓一個交互模式的終端,可以在容器中執行操作 |
拷貝文件 | docker cp 主機中的文件路徑 容器id或容器名稱:容器路徑 | 拷貝主機中的文件到容器中,當然也可以從容器中拷貝文件到主機,將路徑改一下即可 |
這裏我從網上隨便下載了一個鏡像,下面演示如何運行這個容器
root@Thuder_J-virtual-machine:~/桌面# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu stack1 d03f80550adf 4 months ago 467MB
root@Thuder_J-virtual-machine:~/桌面# docker run -it d03f80550adf
pwn@4c75f4b58012:/$ ls
bin boot dev etc home lib lib32 lib64 media mnt opt proc root run sbin srv sys tmp usr var
pwn@4c75f4b58012:/$ whoami
pwn
更新鏡像
操作 | 命令 | 說明 |
---|---|---|
更新鏡像 | docker commit -m="描述消息" -a="作者" 容器id或容器名 鏡像名:tag | 更新鏡像內容 |
構建鏡像 | docker build | 構建鏡像,需要創建 DockerFile 文件 |
通過命令修改docker中內容,這裏以tomcat爲例,我們修改其主頁面
步驟
- 根據基礎鏡像創建容器
docker run --name mytomcat -p 8080:8080 -d tomcat
- 修改容器
docker exec -it bcd08edac78d /bin/bash
cd webapps/ROOT
rm -f index.jsp
echo welcome to tomcat > index.html
exit
- 提交爲新鏡像
docker commit -m="修改默認索引頁" -a="Thunder_J" 容器id或容器名 bcd08edac78d itany/tomcat:v1
- 使用新鏡像運行容器
docker run --name tomca_v1 -p 8080:8080 -d itany/tomcat:v1
Dockerfile
Dockerfile 是一個用來構建docker鏡像的文件,Dockerfile 是一個包涵創建鏡像所有命令的文本文件,最終會生成一個新的鏡像,第一條指令必須是FROM,使用Docker build命令可以根據Dockerfile內容創建鏡像,達到上面更新鏡像的目的
步驟
- 創建Dockerfile文件
vi Dockerfile
內容如下,指令必須大寫,後面必須接參數
#基礎鏡像
FROM tomcat
#作者
MAINTAINER Thunder_J
#執行命令
RUN rm -f /usr/local/tomacat/webapps/ROOT/index.jsp
RUN echo "Welcome to tomcat!" > /usr/local/tomacat/webapps/ROOT/index.html
- 構建新鏡像
docker build語法:docker build -f Dockerfile文件的路徑 -t 鏡像名:tag 命令執行上下文(.代表當前目標)
docker build -f Dockerfile -t itany/tomcat:v2 .
- 使用新鏡像運行容器
docker run --name tomca_v2 -p 9999:8080 -d itany/tomcat:v2
常用指令
指令 | 解釋 |
---|---|
FROM | 指定基礎鏡像,即當前新鏡像是基於哪個鏡像的 |
MAINTAINER | 指定作者 |
RUN | 指定構建過程中要運行的命令 |
ENV | 設置環境變量 |
WORKDIR | 指定默認的工作目錄,即進入容器後默認進入的目錄 |
VOLUME | 創建掛載點,用於數據共享和持久化 |
CMD | 指定容器啓動時運行的命令,與RUN不同,這些命令不是在鏡像構建過程中執行的 |
ENTRYPOINT | 指定容器啓動時運行的命令,與CMD不同 |
COPY | 拷貝文件/目錄到鏡像中 |
ADD | 拷貝文件到鏡像中且會自動解壓縮 |
EXPOSE | 指定對外暴露的端口 |
CMD 和 ENTRYPOINT 區別
- CMD
在Dockerfile中可以有多條CMD指令,但只有最後一條生效,CMD會被docker run後的參數覆蓋 - ENTRYPOINT
docker run後的參數會和ENTRYPOINT形成新的組合指令