Docker

# Run docker container. If the image is not installed, it will automatically download it from docker hub.
docker run ubuntu

# Run a command directly within the container. the process is short lived
docker run ubuntu {command}

# Run interactivetly 
docker run -i -t ubuntu /bin/bash
-i	# Interactive
-t 	# terminal

# Check Docker containers running (from the Host OS machine)
docker ps
docker ps -a	# shows all exited or not

# Run a container in detached mode. The processes will continue to live in the background
docker run -d {container}

# Stop a container
docker stop {container ID from docer ps}
docker stop --time {seconds before SIGKILL} {container}
# Sends the SIGSTOP followed by SIGKILL to the container

# Kill a container
docker kill {container}

# Give a container a name. Useful if running same image on the docker host
docker -d --name {name} {container}

# Restart docker container. You can use container_id or container_name
docker restart {container_id | container_name}

# Map port on Host OS to Docker Container
docker run -p 4567:4567 {container app}
# -p	# {host os port: docker container port}

# Pass anviroment variable to the docker image
docker run -e "HELLO=WORLD" ubuntu 
# To check:
docker run ubuntu /bin/bash -c export

# Check container log
docker logs -f {conatiner name or id}

# Docker restart policy
docker run -d --restart unless-stopped {container}

# Docker search images
docker search {name}

# List images we have on the Host OS
docker image

# Pull image from docker hub
docker pull {name}
docker pull {name}:{tag}

# Remove images
docker rmi {name}

# Create a an image. Not so practical way
1) docker run -i -t {ubuntu} /bin/bash		# Run the container interactively
2) apt install {software}; scripts etc..	# Install software, make directories, scripts etc...
3) docker commit -m "message"			        # Similar to github. Commit the changes and add message

# Create an image. The practical method using the docker file config
- Docker file are named: Dockerfile (Capital D and no space)
FROM		    # which base image to use usch as Ubuntu. Ex: FROM ubuntu
MAINTAINER	# Simple text from the maintainer. Ex: MAINTAINER squid <squid@something>
RUN		      # Run as command. Ex: RUN apt update && apt upgrade -y OR RUN mkdir squid
ADD		      # Adds a file to the docker image. Ex: ADD /files/script.py
WORKDIR		  # Working directory "absolute or relative". Ex: WORKDIR squid
ENTRYPOINT	# Defines the entrypoint to the app. Ex: ENTRYPOINT	["python3", "squid.py"] 
# Then you can run "docker run squid/container [argv]" (to the python code)
EXPOSE      # Exposes the docker container port. Ex: EXPOSE 8080	

# To build docker image
docker build . 			            	# using the . (dot) because the Dockerfile is in this directory
docker build -t squid/stuffname .	# Creates an image with name

Last updated