Skip to content

Instantly share code, notes, and snippets.

@PrakadAlpha
Last active September 14, 2022 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrakadAlpha/e0a0af7c348f81577dc5560c69fdd97f to your computer and use it in GitHub Desktop.
Save PrakadAlpha/e0a0af7c348f81577dc5560c69fdd97f to your computer and use it in GitHub Desktop.
An intro to Docker
What is Docker?
- Docker is used for containarization.
- Docker runs the specific software or in this case an image without being installed in the system
- Docker is like mini containers of os running softwares inspite of any Operating system
- It helps to run multiple servers and softwares and many more..
What it Does?
- It isolates(Namespace) the resources[Processes, HardDrive, Users, InterProcessComm, Network] available from the system
- The namespaces resources are controlled by the ControlGroups[Memory, CpuUsage, NetworkBandwidth, HD i/o]
Docker Image:
- It contains Software Snapshot and Startup command
build run
DockerFile ------------> Docker Image ----------> Docker Container
Command Cheat Sheet::
=> docker run <imageName> <Overide Cmd> ===> Starting a container
=> docker ps ===> Listing the containers
=> docker ps -a ===> Listing history of containers
=> docker create <ContainerName> ===> Combining both is the run command
=> docker start -a(attaching the output) <ContainerName> ===/\
=> docker system prune ===> Remove everything related to docker
=> docker logs <containerId> ===> Check the log
=> docker stop <containerId> ===> Sends 'SIGTERM' termination signal to the process and cleanup and normal stopping process
=> docker kill <containerId> ===> Kills process without additional ops
=> docker exec -i(input => stdin) -t(output => stdout) <containerId> <command> ===> Running command inside a container ===> docker exec -it <id> redis-cli
=> docker run -it <image> sh ===> Create,Start,OverideCommand,RunShell at the start
=> docker rm <container> ====> Remove a container
=> docker rmi <image> =====> Remove an imgae
=> docker images =====> Show all the images
=> docker pull <image> =====> Only update the container with latest version without running
=> docker run -p <accessPort>:<containerPort> <container> => Port Mapping
=> docker run -d <container> =====> Run the container in the background
=> docker attach <containerId> =======> Attach the terminal back again
=> docker run <image>:<version> ======> Get the specified version
=> docker run -v <backupPath>:<containerPath> <image> ====> Run the volume of the container in different disk path
=> docker inspect <container> =======> Get details of the container in JSON format
=> docker logs <container> =========> Get the container logs
=> docker run -e <envVariables> <container> =====> Set the environment varibles
Flags
-t Name
-f Path
-it Interactive
-d Detached
Docker Image:
=> Create a DockerFile(INSTRUCTIONS and ARGUMENTS)
=> Write the steps needed to run the application
=> docker build DockerFile -t <tagName> ====> Building the image from the file
=> docker push <tagName> =====> To publish the image in cloud
=> FROM is the firstline(always) - Base Image
=> Docker preserves the cache and provides rollback for steps in DockerFile
=> ENTRYPOINT => For appending the arguments in the command
Eg: FROM Ubuntu
ENTRYPOINT ['sleep']
CMD ['5'] ===> default value
docker run <container> 10 ===> pass the sleep value as the entrypoint argument and it gets appended
docker run --entrypoint sleep2.0 <container> 10 ===> To change the entrypoint value
docker -t name -f path . currentLocation
Basic variables
FROM - Os for the container
RUN - Running cmd for installation (Executes at build time)
CMD - (Ignored if docker run with params)(Only last CMD is executed)
ENTRYPOINT - (Appends the packages from the command) --> docker run <img-name> git httpd telnet tree
WORKDIR - Change working directory
COPY -
ADD -
EXPOSE - Exposing the port for host
ENV -
Docker Network:
=> Docker has a built in DNS server for mapping the container name with default of 172 series
=> Docker uses network namespaces within the container (virtual ethernet pair)
=> docker network ls ====> View all the network in the container
Dokcer Storage:
=> Structure:-
|--/var/lib/docker
|- /auf
|- /containers
|- /image
|- /volume
=> docker volume create <vol_name> ====> create a volume as above structure
=> docker volume ls ====> listing the volumes
=> docker inspect <volume hash / name> ====> Check the metadata of the volume
=> Volume Mount and Bind Mount
=> docker run -v </with full path>:/var/lib/<image> ==> Bind mount
=> docker run / --mount type=bind, source="path", target="path" <container>
Anonymous Volumes --> docker run --name <cont-name> -v /data01 nginx /bin/bash
Named Volumes ------> docker run --name <cont-name> -v <vol-name>:/data01 nginx /bin/bash
Host/Bind Volumes ---> mkdir /data/docker_data ==> docker run --name <cont-name> -v /data/docker_data:/data01 nginx /bin/bash
Docker Compose:
docker-compose up
docker-compose down
docker-compose up -d
docker-compose ps
docker-compose logs
docker-compose exec web sh
curl localhost
(edit Dockerfile, and add RUN apk add --update curl)
docker-compose up -d
docker-compose up -d --build
docker-compose exec web sh
curl localhost:port
docker-compose down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment