RedTeam
Others
Docker
Docker

What is Docker

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.


Commands

General

# General
- sudo dockerd                      ---> Start docker
- sudo usermod -aG docker USERNAME  ---> Ading user to docker group, enable user to run docker stuff without beeing root (-a = Primary group | -G = Secondary group) - REBOOT
- docker container ls               ---> Show the docker process
- docker container ls -a            ---> Show all the docker process (Show history)
- docker images                     ---> Show a list of all the docker installed
- docker images ls                  ---> Show current images running
- docker images history NAME:TAG    ---> Show the history of an image
- docker search ubuntu (EX)         ---> Search for docker images
- docker inspect CONTAINER | jq     ---> See the configs
	- | jq ' .[] | | .ContainerConfig'   ---> See only the configuration
	Useful to find the CMD commands, etc...


# Get & Run Docker files
- https://hub.docker.com/           ---> Docker images (Best way to start)

- docker pull NAMEOFIMAGE           ---> Download the image to the computer
- docker run hello-world:latest     ---> Run the latest version of Hello-World
- docker run hello-world:VERSION    ---> Run specific version of Hello-World

- docker run -it alpine             ---> -i = Interactive & -t = TTY Terminal
- docker run -dit alpine            ---> -d = detach (background)
	- Hostname = Container ID
	- Add modification in the container if desired

- docker container attach ContID    ---> Connect to the detach container (when exit, container dead)
- docker container exec -it ContID sh ---> Connect to the detach container (when exit, container alive)

- docker container run -d nginx     ---> Won't alow you to curl it (since we have not precise the open ports)
- docker container run -d -p 80:80nginx  ---> -p=open port hostport:containerport (now possible to curl)

# Container edits
- docker attach NAME                ---> Connect back to the container 
- docker stop NAME                  ---> Stop container
- docker rm NAME                    ---> Remove the container

# Docker create new image from initial image
- docker container commit ConName NewImageName:TAG   ---> Create a new image from container (including changes added to the inital container
- docker image save ContainerNAME:TAG > Name.tar     ---> Save an image has tar
- docker image load < Name.tar                       ---> Load images tar in docker

# Edit container files
- sudo apt-get nano
- nano files
or
- docker container cp Hostfile.x ContainerID:DESTINATION ---> Copy files from the local machine to a container


# Other Container commands
- docker container prune              ---> Remove container not used
- docker container logs ContainerID   ---> Get lorgs of a container

Docker Automation

# Automate Docker container (Very powerfull)
- sudo apt install docker.io docker-compose
- nano docker-compose.yaml          ---> create docker configuration file to manage containers
	#CHECK CONTAINER DOCUMENTATION
	version: '3.7'
	services:
	  portainer:
	    container_name: DOCKERNAME
	    image: DOCKERFILENAME:VERSION (VERSION IS OPTIONAL DEPENDING WHAT YOU WANT TO INSTALL)
	    restart: 'always'           ---> Make it restart by default
	    ports:
	      - target: 'PORT1'
	        published: 'PORT1'
	        protocol: tcp
	      - target: 'PORT2'
	        published: 'PORT2'
	        protocol: tcp
	    volumes:                    ---> Provide Persistent storage
	      - type: bind
	        source: /var/run/docker.sock
	        target: /var/run/docker.sock
	      - type: bin
	        source: /srv/DOCKERNAME
	        target: /data/

- sudo mkdir /srv/DOCKERNMAE        ---> Create the directory to host the persistence data
- docker-compose up --datach        ---> Launch the configuration file in background