Personal knowledge based for Tech, Interviews and many more.
This project is maintained by im-Rajat
dockerdocker container --helpdocker --versiondocker versiondocker infoDocker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.
The process can be summarized as follows :-
The Docker Flow:


docker image - help
docker image ls or docker images - Show all imagesdocker image ls -a - Show all images, including intermediate images (dangling images).docker image ls -q - Show only the image IDs.docker pull <image_name>:<tag>docker pull ubuntu:latest - Downloads an image from a Docker registry (Docker Hub).docker build -t <image_name>:<tag> <path> - Builds a new Docker image from a Dockerfile.docker build -t rajatapp:v1 . - Dot (.) means dockerfile is located in current directory.docker build -t image_name -f mycustom.dockerfile . - If we want to provide different dockerfile name, use -fdocker run -it -v /D:/mnt/d -p 85:85 -p 82:82 image_name bash - Using -v we can provide host machine volume access to containers, using -p we can expose ports.docker tag <image_id> <new_repo>:<new_tag> : - Assigns a new name or tag to an image.docker tag abc123 myrepo/myapp:v1docker inspect <image_id> - Displays detailed information about an image.docker inspect ubuntu:latestdocker rmi <image_id> or docker image rm <image_id> - Deletes an image from the local system.docker rmi -f abc123 - Force removal of an image using -f.docker image prune – Remove dangling (unused) imagesdocker image rm $(docker image ls -q) – Remove all images (use with caution)docker push <repo-name>:<tag> - Uploads an image to a Docker registry.docker push myrepo/myapp:v1docker save -o <file>.tar <image>:<tag> – Save an image to a tar archivedocker load -i <file>.tar – Load an image from a tar archivedocker search <term>
docker search ubunturegistry.example.com:port/organization/image-name:version-tagorganization/image-name is enough (e.g., ubuntu, nginx, myorg/myapp:1.0).docker run hello-world - Run a Containerdocker run hello-world - does exactly what it sounds like. It runs an image named “hello-world.”docker ps -a - lists the containers on our systemdocker run hello-world. The output is almost the same.docker ps -a again - We see two stopped instances of hello-world, with two different names. Docker created an additional container. It didn’t reuse the first. When we told Docker to run an image named hello-world, it did exactly that; it ran a new instance of the image. If we want to reuse a container, we refer to it by name or id.docker run -it ubuntu bash, we downloaded an Ubuntu Linux image and started a login shell as root inside it. The -it flags allow us to interact with the shell.docker ps or docker container ls - Lists all running containers.docker ps -a - Show all containers (both running and stopped).docker ps -q - Show only container IDs.docker run <options> <image-name> or docker container run <image_id> - Creates and starts a new container from an image.docker run -it ubuntu bash - Start an interactive session using -it.docker run -d <image-name> - Starts a container in detached mode (background) using -ddocker container run -p <host_port>:<container_port> <image_id> – Map container ports to host machinedocker run -d -p 8080:80 nginx - Map container ports to host ports using -d.docker run --name mycontainer nginx - Assign a name to the container using --namedocker container start is same as docker startdocker container start <container_id/name> - Start a Stopped Container.docker container stop <container-id/name> - Stop a Running Container.docker container restart <container_id/name> - Restart a Container.docker container exec -it <container_id> bash - Run a container with it’s ID.docker container inspect <container-id or name> - Displays detailed information about a container.docker container stop $(docker ps -q) - Stops all running containers.docker container attach <container-id> - Attaches to a running container’s terminal.container keyword) vs Modern Syntax (with container keyword):docker container rm <container-id or name> - Remove a Specific Container.docker container prune - Remove All Stopped Containers.docker container rm $(docker ps -aq) - Removes all containers, including stopped ones.docker container kill <id> - Kills a container (forcefully stops it).docker cp <host-path> <container-id>:<container-path> - Copy From Host to Container.docker cp <container-id>:<container-path> <host-path> - Copy From Container to Host. docker commit <container_id/name> <new_image_name>:<tag> # Commit the Current State of the Container to a New Image
docker run -it <new_image_name> bash # Run a New Container from the New Image
docker container ls -a # Show all containers (first exit from docker container)
docker container logs <container_id> – View container logsdocker container top <container_id> – Show running processes inside a containerdocker container stats – Display real-time resource usage of all containersdocker export -o D:/wsl/filename.tar <container_id> - Export the containerwsl --import <DistroName> D:/WSL/ D:/wsl/filename.tar - Import it as a new WSL distribution
FROM, RUN, COPY, ADD, CMD, EXPOSE, WORKDIR, ENV, ENTRYPOINT, VOLUME, and USER.sudo su -, root directory if on Linux)Dockerfile in your project directory:nano Dockerfile (Write your Dockerfile code)docker build -t <image_name>:<tag> <path_to_dockerfile_directory>docker build -t myapp:latest .Or, for a simple tag:
docker build -t filename ./
-t filename gave Docker a tag for the image. Since we only supplied a name, Docker tagged this build as latest in the last line of the build output..), told Docker to look for the Dockerfile in the current working directory.docker imagesdocker run <image_name>:<tag>docker run myapp:latestdocker run --rm -it filenamedocker run -p <host_port>:<container_port> <image_name>:<tag>docker run -p 8080:80 myapp:latestdocker run -it <image_name>:<tag> /bin/bash.dockerignore to exclude files/folders from the build context.docker build --no-cache ... to build without using cache.docker logs <container_id> to view output from your running container.FROM ubuntu
# You can start with any base Docker image that works for you
# Update package list and install git
RUN apt update && apt-get install -y git
# Set the working directory
WORKDIR /root/bacnet-stack-0.8.6/
# Print the current working directory (for debug)
RUN pwd
# Copy the bin directory from your context to the image
COPY ./bin/ ./
# Expose the BACnet default port
EXPOSE 47808
# Set environment variable for PATH
ENV PATH="/root/bacnet-stack-0.8.6/bin:${PATH}"
# Example commands to run (only the last CMD will take effect)
# You can choose the one you need or use ENTRYPOINT for more control
# CMD ["./bacwi", "-1"]
# CMD ["./bacrp", "1234", "1", "1", "85"]
CMD ["./bacwp", "1234", "1", "1", "85", "16", "-1", "4", "10"]
docker run --memory <max-memory> <image-name> <command>docker run --cpu-shares <value> <image-name> (relative to other containers)docker run --cpu-quota <value> <image-name> (absolute limit)Example:
docker run --rm -e SECRET=theinternetlovescats -it --name catserver ubuntu:14.04 bashdocker run --rm -it --link catserver --name dogserver ubuntu:14.04 bashnc -lp 1234nc catserver 1234Check environment variables with env.
MAINTAINER Firstname Lastname <email@example.com>RUN unzip install.zip /opt/install/ADD project.tar.gz /install/ENV DB_PORT=1234ENTRYPOINT specifies the start of the command to run.CMD specifies the whole command to run.CMD echo helloCMD ["/bin/echo", "hello"]EXPOSE 8080VOLUME ["/host/path/", "/container/path/"]WORKDIR /install/USER rajat or USER 1000FROM ubuntu:16.04 as builder (# assign image name as builder, so we can use it later)
RUN apt-get update
RUN apt-get -y install curl
RUN curl https://google.com | wc -c > google-size
FROM alpine
COPY --from=builder /google-size /google-size (# copy data from one image to another, it saves space because, ubuntu image is big as compared to alpine. Alpine is small base image)
ENTRYPOINT echo google is this big; cat google-size
docker compose version – Check Compose versiondocker compose up -d – Start services in backgrounddocker compose down – Stop and remove containers and networkdocker compose down --volumes – Also remove named volumes-p (publish) flag:
docker run --rm -it -p 45678:45678 -p 45679:45679 --name echo-server ubuntu:14.04 bash
-p <host_port>:<container_port> (outside port : inside port).-p 45678), Docker will randomly assign a host port:
docker run --rm -it -p 45678 -p 45679 --name echo-server ubuntu:14.04 bashdocker port echo-serverecho-server is the container name)docker run --rm -it -p 45678/udp --name echo-server ubuntu:14.04 bashdocker network ls
NETWORK ID NAME DRIVER SCOPE
c7376020aba9 bridge bridge local
75919c8d5779 host host local
53f3bd0aeab7 none null local
docker network create learningdocker run --rm -it --net learning --name catserver ubuntu:14.04 bashdocker network create catsonlydocker network connect catsonly catserver-v):
docker run -it -v D:\tempdocker:/shared-folder ubuntu bashD:\tempdocker from Windows host to /shared-folder in the container)--mount):
docker run -it --mount type=bind,src=/path/to/data,target=/usr/local/data ubuntu bash$(pwd) for current directory:docker run -it --mount type=bind,src="$(pwd)",target=/src ubuntu bashdocker run -it -v /shared-folder ubuntu bashdocker run -it --volumes-from <container-name> ubuntu bash--mount):
docker run -it --mount type=volume,src=my-volume,target=/usr/local/data ubuntu bashProblem : Not able to access internet inside docker windows container.
Solution : Resolve it by setting the DNS.