Debug School

Fahad Rehman
Fahad Rehman

Posted on

Docker Assignment

1.) What is Docker and why is it used?

Docker is an open-source containerization platform that allows developers to package applications and dependencies into a portable container that can be deployed consistently across different environments, such as development, testing, and production.
Used:-
Consistency: Docker containers ensure that the application runs consistently across different environments, regardless of the underlying system or dependencies.
Portability: Docker containers can be easily moved between different hosts, platforms, and cloud providers, making it easy to deploy applications in various environments.
Scalability: Docker containers can be scaled up or down quickly and easily, making it easier to manage applications that have varying levels of usage.
Resource efficiency: Docker containers use fewer resources than virtual machines, allowing for more efficient use of hardware resources and reducing costs.

2.) What is a Docker image and how is it different from a Docker container?
A Docker image is a pre-built, standalone, and executable package that contains everything needed to run a specific application, including the application code, dependencies, and other required components such as system libraries and tools. Docker images are created using a Dockerfile, which is a script that defines the instructions needed to build the image.On the other hand, a Docker container is a running instance of a Docker image that is isolated from the host system and other containers. Each container has its own file system, networking, and process space, and runs as a lightweight, standalone unit. Docker containers are created by instantiating an image using the Docker engine.

3.) How do you create a Docker image and run a Docker container?
To create a Docker image, need to create a Dockerfile, which is a script that defines the instructions for building the image.
Here are the basic steps to create a Docker image:
Create a Dockerfile: This is a text file that contains the instructions needed to build the Docker image. The Dockerfile typically includes commands to install dependencies, copy files into the image, and set environment variables.
Build the Docker image: Use the docker build command to build the Docker image from the Dockerfile. This command will read the instructions in the Dockerfile and create an image based on those instructions.
To run a Docker container based on the image you just created, follow these steps:
Start a Docker container: docker run -d -p 8080:80 my-image
Check the status of the container: Use the docker ps command to check the status of the container. This command will list all the running containers on your system.

4.) What is a Dockerfile and how do you use it to create a Docker image?
A Dockerfile is a script that contains a set of instructions for building a Docker image. The Dockerfile defines how the image should be constructed, including what software packages and dependencies should be included, how files should be copied, and what commands should be run.

steps to create a Docker image using a Dockerfile:

Create a Dockerfile: The Dockerfile typically includes a series of FROM, RUN, COPY, and CMD commands to install dependencies, copy files into the image, and set environment variables.
Define the base image: The FROM command is used to specify the base image to use for the Docker image. For example, if you want to create an image based on Ubuntu, you would use the command FROM ubuntu.

Install dependencies and set up the environment: Use the RUN command to install any necessary software packages and dependencies, and set up the environment.
For example:- RUN apt-get update && apt-get install -y python3 to install Python 3.

Copy files into the image: Use the COPY command to copy files from the host machine into the Docker image.
For example, COPY app.py /app/

Build the Docker image: docker build -t my-image .

Run the Docker container: Use the docker run command to start a new container from the image you just built
docker run -d -p 8080:80 my-image

5.) How can you inspect the contents of a Docker container and the changes made to a container while it was running?

Docker exec command: You can use the docker exec command to run a command inside a running container.
For example:- docker exec my-container cat /var/log/myapp.log will show the contents of the myapp.log file in the running container named my-container.

Docker logs command: You can use the docker logs command to view the logs generated by a running container.
For example:- docker logs my-container

Docker diff command: You can use the docker diff command to view the changes made to the filesystem of a container since it was created.
For example:- docker diff my-container

6.) How can you share a Docker image with others and pull an image from a Docker registry?
steps to share a Docker image with others-
Tag the Docker image:- docker tag my-image myusername/my-image.
Push the Docker image:- docker push myusername/my-image

7.) What are the different network modes available in Docker and how do you choose the right network mode for your application?
Host mode
Bridge mode
Overlay mode
Macvlan mode
None mode

8.)How can you mount a volume in a Docker container and share data between the host and container?

To mount a volume in a Docker container and share data between the host and container, you can use the -v or --mount option when running the container.

9.What is the difference between a Docker Compose file and a Dockerfile, and how are they used in deploying multi-container applications?

A Dockerfile is a text file that contains the commands that a user can execut to build an image.
Docker Compose is a tool for defining and running multi-container Docker applications.

10.How can you monitor the performance of a Docker container and diagnose issues with it?

We can monitor the performance of a docker container by executing the command "docker stats".

11.Components of Docker and its Brief Summary
Docker Engine: Docker Engine is a containerization technology that is used to build and containerize applications.
Docker Image: A Docker image contains set of instructions that are used to create a container.
Docker Registry: A Docker registry is a storage system for Docker images.
Docker container: Docker containers are standardized, executable components that combine application source code with the operating system and dependencies that are required to run the code in any environment.

12.What is the differenet between docker pause and unpause?
The docker pause command is used to suspend all processes in the specified containers The docker unpause command un-suspends all processes in the specified containers.

13.What is the differenet between docker stop and kill?
he docker stop command stops the container and provides a safe way to stop the container whereas docker kill command kills one or more containers.

14.What is the differenet between docker exec and attach?
The docker exec command is used to run a new command in a running container whereas docker attach command is used to attach the terminal's standard input, output, and error to a running container.

15.List of dockerfile instructions and its Brief Summary?
Dockerfile instructions with a brief summary:
FROM: Specifies the base image to use for the Docker image being built.
MAINTAINER: Specifies the name and email address of the person or team responsible for maintaining the image.
RUN: Runs a command inside the Docker image to install or configure software.
COPY or ADD: Copies files or directories from the host machine into the Docker image.
CMD: Specifies the command to be run when a container is started from the image.
ENTRYPOINT: Specifies the command to be run when a container is started from the image, but allows for additional command-line arguments to be passed to the container.
ENV: Sets environment variables inside the Docker image.
EXPOSE: Specifies the network ports that the Docker container will listen on at runtime.
VOLUME: Specifies the directories or files that the Docker container should create a volume for at runtime.
USER: Sets the user ID or name that the Docker container will run as.
WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, or ADD commands that follow it in the Dockerfile.

16.What is the differenet between CMD vs Entry

CMD: This command is used to run a docker container by specifying a default command that gets executed for all the containers of that image by default.
Entrypoint: This instruction is used to configure the executables that will always run after the container is initiated.

Top comments (0)