Docker常用命令及镜像构建


安装

常用命令

  • images:列出本地主机上的镜像,如docker images
  • search :查找镜像,如docker search vulhub
  • pull:拉取镜像,如docker pull vulfocus/shiro-cve_2016_4437
  • run:运行镜像,如docker run vulfocus/shiro-cve_2016_4437
  • ps:查看启动的容器和镜像,如docker ps
  • stop:结束镜像,如docker stop ad6dea89cf07
  • rm:删除容器,如docker rm ad6dea89cf07,docker rm $(docker ps -a -q)
  • rmi:删除镜像,如docker rmi vulfocus/shiro-cve_2016_4437
  • tag:重命名,如docker tag ad6dea89cf07 infiltrator/test:latest

配置远程API

  1. 在/etc/default/docker中添加DOCKER_OPTS="-H tcp://127.0.0.1:2375"
  2. 在/lib/systemd/system/docker.service中添加
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://127.0.0.1:2375
#ExecStart=/usr/bin/dockerd -H fd://
  1. 修改daemon.json的配置
#监听0.0.0.0不安全
{
  "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}

安装vulnhub环境

示例

  • docker run -td -p 本机映射端口:镜像映射端口 -d 镜像名称:镜像版本号,如docker run -p 8080:8080 -p 8009:8009 vulfocus/shiro-cve_2016_4437 /bin/bash
  • 进入已运行的容器:docker exec -it 容器ID /bin/sh
  • docker exec --name debian_baota /bin/sh(开启新终端)
  • docker attach debian_baota

Dockerfile生成镜像

  • docker login --username infiltrator:登录docker
  • Dockerfile
#使用基础镜像ubuntu:latest
FROM ubuntu:latest

#指定镜像创建者信息
MAINTAINER infiltrator

#切换镜像的目录,进入/usr目录(基础镜像是一个linux系统,可参照linux查看相应目录)
#WORKDIR /usr

#在/usr/下创建java目录,用来存放jdk
RUN apt-get update
#RUN apt-get install net-tools -y
#RUN apt-get install vim -y
RUN apt-get install gcc -y
RUN apt-get install make -y
RUN apt-get install ssh -y
RUN apt-get install cron -y
RUN apt-get install wget -y
RUN apt-get install apache2 -y
RUN apt-get install php -y
RUN apt-get install libapache2-mod-php -y

WORKDIR /usr/share/
RUN wget http://download.redis.io/releases/redis-2.8.17.tar.gz
RUN tar zxf redis-2.8.17.tar.gz
RUN rm redis-2.8.17.tar.gz
RUN cd redis-2.8.17 && make

RUN echo 'PermitRootLogin yes' >>/etc/ssh/sshd_config
RUN echo 'RSAAuthentication yes' >>/etc/ssh/sshd_config

#设置容器启动时执行的操作
CMD bash /root/start.sh && bash
  • docker build -t infiltrator/redis_unauth_access:latest .:构建镜像到当前路径
  • docker push infiltrator/redis_unauth_access:latest:上传

基于已有镜像生成

  • docker pull ubuntu:下载官方镜像
  • 运行并进入容器:docker run -it ubuntu /bin/bash
  • 安装程序
  • 开机启动脚本/root/service
#!/bin/bash
service ssh start
service apache2 start
/etc/init.d/cron start
/usr/share/redis-2.8.17/src/redis-server
  • docker commit -m "Redis Unauthorized Access Vulnerability Experiment" -a "infiltrator" 0b2616b0e5a8 infiltrator/redis_unauth_access:latest
  • Dockerfile
FROM infiltrator/redis_unauth_access_vulnhost:latest
CMD /root/service && bash
  • docker build -t infiltrator/redis_unauth_access_vulnhost:stable .

Docker运行GUI程序

精简镜像

  • docker export -o oracle.tar oracle
  • sudo docker import orcl.tar infiltrator/oracle_sqlinject_lab

查看容器IP

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [容器ID或名称]

声明:Hack All Sec的博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Docker常用命令及镜像构建


Hacker perspective for security