-
Docker Image == Bare minimum OS + App run time + system packages and dependencies + App code (conatiner is the running istance of docker image)
-
docker pslists all running Docker containers. -
usermod -aG docker <username>adds the specified user to the Docker group, allowing them to run Docker commands withoutsudo. -
docker imagesdisplays a list of all locally stored Docker images. -
docker pull nginxdownloads the latest Nginx image from Docker Hub to your local machine. -
docker create <image>:tagcreates a new container from the specified image and tag but does not start it. -
docker ps -a: Lists all Docker containers, including both running and stopped ones. -
docker start <container ID>starts a stopped container specified by the container ID. -
docker runcreates and starts a new container from a specified image, running it in the foreground. (docker run = docker pull + docker create + docker start docker run effectively combines the actions of docker pull (downloading the image if not already available), docker create (creating a container from the image), and docker start (starting the container)) -
docker rm <container ID>removes the specified container from the Docker host, but only if it is stopped. -
docker rm -f <container ID>forcefully removes the specified container, stopping it if it is currently running. -
docker rmi <image ID>removes the specified image from the local Docker host, deleting it if no containers are using it. -
docker rmi -a -q <image ID>removes all images matching the specified image ID, using the-aoption to target all tags and-qto only show the image IDs without additional output. -
docker rm
docker image -a -qremoves all locally stored Docker images by executing the inner command to get all image IDs and passing them todocker rmi. -
docker rm
docker ps -a -qremoves all stopped containers by executing the inner command to list all container IDs and passing them todocker rm. -
docker run -d nginxstarts a new Nginx container in detached mode, allowing it to run in the background.
Q. How can you access docker container from internet ? A. By enabling the port, we need to open host port that can redirect traffic to container
-
docker run -d -p <host-port>:<container-port>starts a new container in detached mode, mapping a specified port on the host to a port on the container. -
docker logs <container ID>retrieves and displays the logs from the specified container, showing its output and any error messages. -
docker logs -f <container ID>streams the logs of the specified container in real-time, allowing you to see new log entries as they are generated. -
docker inspect <container or image ID>provides detailed information about a specified container or image in JSON format, including its configuration and state. -
docker exec -it <container ID> bashopens an interactive bash shell in the specified running container, allowing you to execute commands inside it. -
docker images -f <filter>lists Docker images with optional filters applied, allowing you to narrow down the results based on specific criteria, such as dangling images or specific labels. -
docker tag <source-image> <target-image>creates a new tag for an existing Docker image, allowing you to reference it by a different name or version, typically used for versioning or preparing images for pushing to a registry. -
docker login -u <username>prompts for the password and logs the user into a Docker registry with the specified username, allowing access to private repositories. -
docker push <username>/<image-name>:<tag>uploads a local Docker image to the specified repository on Docker Hub, making it accessible to others or for use on different systems. -
" DOCKERFILE ------>> It is the declarative way of creating custom images "
-
docker build -t url/username/image-name:tag . ---> will check for the dockerfile
-
FROM shuould be the first instruction to represent the base OS. FROM image-name:tag(The
FROMinstruction in a Dockerfile specifies the base image to use for the new image, defining the starting point for the build process.) -
We will use RUN instruction to install the packages and configure them. run instruction will execute at the time of image creation.(The
RUNinstruction in a Dockerfile executes a command in the shell during the image build process, allowing you to install packages or modify the file system of the image.) -
CMD instruction run at the time of container creation (The
CMDinstruction in a Dockerfile specifies the default command to run when a container is started from the image, which can be overridden by command-line arguments when running the container.) -
The
LABELinstruction in a Dockerfile adds metadata to an image in the form of key-value pairs, which can be used for documentation, organization, or management purposes. -
The
EXPOSEinstruction in a Dockerfile informs Docker that the container listens on specified network ports at runtime, serving as documentation for users and other developers, but it does not actually publish the ports. -
The
ENVinstruction in a Dockerfile sets environment variables in the container, which can be accessed by applications running inside the container and can also influence the behavior of the image. -
The
ENTRYPOINTinstruction in a Dockerfile specifies a command that will always run when a container is started from the image, allowing you to configure a container to behave like a standalone executable. It can be combined withCMDto provide default arguments.- CMD instruction can be overridden
- we cant override entrypoint , if you try to over ride it will be append
- we can mix the CMD and entrypoint for better result
- CMD is used to supply default arguments to ENTRYPOINT
- we can always override defailt args from the run time.
-
Q what is the best practices in docker? A. It's best practice to avoid running containers with root access to enhance security, as it minimizes the potential impact of vulnerabilities or breaches by restricting permissions. Instead, create and use a non-root user within the container.
- The
USERinstruction in a Dockerfile sets the username or UID (and optionally the group or GID) to use when running the container, allowing you to specify a non-root user for improved security.
- The
-
The
WORKDIRinstruction in a Dockerfile sets the working directory for any subsequentRUN,CMD,ENTRYPOINT,COPY, andADDinstructions, making it easier to manage file paths within the container. If the directory does not exist, it will be created. -
The
ARGinstruction in a Dockerfile defines a variable that users can pass at build-time to the Docker build process, allowing for dynamic configuration of the image. Unlike environment variables set withENV,ARGvariables are not available in the running container.-
ARG defines build-time variables; in this Dockerfile,
version,COURSE,TRAINER, andDurationare specified, with a default version of8for the base image. -
ENV sets environment variables for the container; here,
Durationis made available to applications running inside the container.
-
-
The
ONBUILDinstruction in a Dockerfile adds a trigger that will execute specified commands when the image is used as a base for another image. This is useful for creating reusable base images that automatically include additional setup in derived images.this is useful as a trigger if someone is trying to use your image . you can force them to keep somefiles in their workspace or some configuration