Skip to content

Docker

DOCKER

**Docker is a software container platform that allows you to create, distribute and run applications in isolated environments. This means that you can package applications with all their dependencies and configurations in a container that can be easily moved from one machine to another, regardless of operating system or hardware configuration.

Some of the advantages when it comes to hacking using Docker are:

  • Isolation: Docker containers are isolated from each other, meaning that if an application within a container is compromised, the rest of the system will not be affected.
  • Portability**: Docker containers can be easily moved from one system to another, making them ideal for deploying vulnerable environments for hacking practices.
  • Reproducibility: Docker containers can be configured accurately and reproducibly, which is important in hacking to be able to recreate attack scenarios.

docker file

A Dockerfile file is composed of several sections, each of which begins with a keyword in shift, followed by one or more arguments.

Some of the most common sections in a Dockerfile are:

  • FROM: used to specify the base image from which the new image will be built.
  • RUN**: used to execute commands inside the container, such as installing packages or configuring the environment.
  • COPY**: used to copy files from the host system to inside the archive.
  • CMD**: used to specify the command to be executed when the archive is started.

In addition to these sections, other instructions for configuring the environment, installing additional packages, exposing network ports and more can also be included.

Port Forwarding and mounts

Port forwarding, also known as port forwarding, allows us to redirect network traffic from a specific port on the host to a specific port in the container. This will allow us to access services running inside the container from the outside.

To use port forwarding, the "-p" or "-publish" option is used in the "docker run" command. This option is used to specify port forwarding and can be used in several ways. For example, if you want to redirect port 80 of the host to port 8080 of the container, you can use the following syntax:

docker run -p 80:8080 my_image.

This will redirect any incoming traffic on port 80 of the host to port 8080 of the container. If you wish to specify a protocol other than the default TCP protocol, you can use the "-p" option with a different format. For example, if you want to redirect port 53 of the host to port 53 of the container using the UDP protocol, you can use the following syntax:

docker run -p 53:53/udp my_image.

Mounts**, on the other hand, allow us to share a directory or file between the host system and the container. This will allow us to persist information between container runs and share data between different containers.

To use the mounts, the "-v" or "-volume" option is used in the "docker run" command. This option is used to specify the mount and can be used in several ways. For example, if you want to mount the "/home/user/data" directory of the host to the "/data" directory of the container, you can use the following syntax:

docker run -v /home/user/data:/data my_image.

This will mount the "/home/user/data" directory of the host into the "/data" directory of the container. If you wish to specify an additional option, such as mounting the directory in read-only mode, you can use the "-v" option with a different format. For example, if you want to mount the directory in read-only mode, you can use the following syntax:

docker run -v /home/user/data:/data:ro my_image.

docker compose

Docker Compose is a container orchestration tool that allows you to define and run multi-container applications easily and efficiently. With Docker Compose, we can describe the different services that make up our application in a YAML file and then use a single command to run and manage all these services in a coordinated manner.

In other words, Docker Compose allows us to define and configure multiple containers in a single YAML file, which simplifies the management and coordination of multiple containers in a single application. This is especially useful for complex applications that require the interaction of several different services, as Docker Compose allows you to easily define and configure the connection and communication between these services.

  • downloads docker

    sudo apt install docker.io   # install
    which docker                 # verify the installation
    

  • install dependencies with docker database

    docker pull debian:latest 
    

  • start the demon of docker

    service docekr start
    

  • dockerfile → an archive with coding of the dockerfile

    FROM ubuntu:latest   # the base for create (the last version of ubuntu)
    
    MAINTAINER R3D4L1T   # info content
    

  • for every change in images make

    docker build -t my_file_image .    # search a file in actual directory with name dockerfile for running, -t asignate a name at archive, point is the place of the dockerfile
    

  • upload image

    docker run -dit --name myconteiner my_file_image   # -d for working in backgroud, -i  interactive image for connection,-t for add a terminal, --name add a name at container, my_file_image is the image for create the container
    

  • show the images and volume of dockers

    docker images
    docker volume ls
    

  • verify if the container is running

    docker ps -a    # ps ofr show the running container, -a show all containers
    

  • connection with container

    docker exec -it mycontainer bash    # exec for execute, mycontainer is the name of container, bash for show a console interactive
    

  • install all dependencies

    apt update
    apt install net-tools -y
    apt install iputils-ping -y
    

  • modify image

    FROM ubuntu:latest   # the base for create (the last version of ubuntu)
    
    MAINTAINER R3D4L1T   # info content
    
    RUN apt update && apt install -y net-tools \     # the \ is for add in netx lines more tools
        iputils-ping \
        curl \
        git  \
        nano \
        . 
        .
        .
    

  • update with version 2

    docker build -t my_file_image:v2 . 
    

  • create an new container with v2 image

    docker run -dit --name myNewContainer my_file_image:v2 
    

  • stop a container

    docker stop CONTAINER ID     
    

  • delete container

    docker rm CONTAINER ID --force
    docker rm $(docker -ps -a -q) --force    # delete all
    docker rm $(docker volume ls -q)          # delete all volumes
    

  • delete images

    docker rmi IMAGE ID 
    docker rmi $(docker image -q)
    

  • show all container ID

    docker -ps -a -q
    

port forwarding and mount

  • create dockerfile

    FROM ubuntu:latest
    
    MAINTAINER  R3D4L1T
    
    ENV DEBIAN_FRONTEND noninteractive   # quit the interactive mode
    
    RUN apt update && apt install -y net-tools \ 
        iputils-ping \ 
        curl \
        git \
        nano \ 
        apache2 \
        php 
    
    
    EXPOSE 80               # expose the port 80 for usage in webpage
    
    ENTRYPOINT service apache2 start && /bin/bash     # execute when start container
    

  • create a image with docker image previus

    docker build -t webserver .
    

  • create a docker container

    docker run -dit -p 80:80 --name mywebserver webserver  # 80:80 for that port 80 virtual = 80 of psycal machine
    

  • verify configurations

    docker port mywebserver    # should show 80/tcp -> 0.0.0.0:80 
    

  • show logs

    docker logs CONTAINER ID       # show the history of logs  
    docker logs CONTAINER ID -f    # show in real time the changed in the container
    

mounts

for syncronize files in the psycal machine with Docker container this is in the creation of the container

docker run -dit -p 80:80 -v /PATH_whenIs_myFile/: /PATH_inDockerContainer/ --name mywebserver webserver    # the PATH only Directory without name of file

  • other shape of to do this is:
    FROM ubuntu:latest
    
    MAINTAINER  R3D4L1T
    
    ENV DEBIAN_FRONTEND noninteractive   # quit the interactive mode
    
    RUN apt update && apt install -y net-tools \ 
        iputils-ping \ 
        curl \
        git \
        nano \ 
        apache2 \
        php 
    
    COPY file.txt /PATHofthecontainer
    
    EXPOSE 80               # expose the port 80 for usage in webpage
    
    ENTRYPOINT service apache2 start && /bin/bash     # execute when start container
    

docker-Compose

  • clone a project of github but if is an subcarpet of poject in github run the next code

    svn checkout https://github.com/vulhub/vulhub/trunk/kibana/CVE-2018-17246   #the previous folders have been removed because they were the largest folders of the file that interests us and has been replace by "trunk"
    

  • for run the file You are in the same directory

    docker-compose up -d 
    

  • for show the logs

    docker-compose logs