For working professionals
For fresh graduates
More
5. Docker Hub
7. Docker Swarm
9. Docker Image
10. Docker Registry
11. Podman vs Docker
A Docker Image includes everything needed to run a specific application. It tells the container what software to run and how to set it up.
A Docker Container is like a mini-computer that runs an application using this package. It includes all the necessary parts to make the application work smoothly, no matter where it's being used.
Let us begin by understanding What a Docker Image is.
A Docker image is developed by using the DockerFile, which comprises a set of instructions needed to containerize an application. The Docker image includes a number of pieces of software. It is a platform-independent image built in a Windows environment that can be pushed in the Docker Hub and pulled by others with various OS environments, like Linux.
Now, let us understand the Layers in a Docker Image.
Source: https://medium.com/@mrdevsecops/docker-objects-e561f0ce3365
Docker images have multiple layers, each building on the previous one but with some differences. These layers make Docker builds faster and more efficient by reusing parts that haven't changed, saving disk space, and avoiding unnecessary data transfers.
Each layer in an image is read-only. When a container is build, a writable layer is added, allowing you to make changes.
When discussing disk space in Docker, it's important to understand the difference between size and virtual size. Size refers to the space used by a container's writable layer. Virtual size includes both the writable layer and the shared read-only layers from the image. Multiple containers can share these read-only layers from the same image.
Now, moving ahead, let us understand the use cases of Docker Image.
Below are the use cases of Docker Image:
Now, let us understand the difference between Docker Images and Docker Container.
The below differences give you a clear picture of Docker Image and Docker Container.
Docker Image | Docker Container |
The docker image is the source of the docker container | Whereas, the Docker contains an instance of a Docker image |
Dockerfile is a priority in a Docker Image | Docker Image is a prerequisite in any Docker Container |
Docker Images are shared between users via a Docker registry | Docker Containers cannot be shared between users |
Modifications must be made to a Dockerfile to make changes to any Docker Image | Changes can be made directly to a Docker Container by integrating with it. |
Now, let us understand the Subcommands of a Docker Image.
Here are some of the subcommands commonly used with Docker Image:
The `docker image prune` command removes unused Docker images from the Docker host.
docker image prune
Unused images, or dangling images, are not associated with any containers.
The following command is used to build the Docker image.
Docker build -t your_image_name: tag -f path/to/Dockerfile.
Docker build: Initiates the build process.
-t your_image_name:tag: Assigns a name and, optionally, a tag to the image you're creating.
-f path/to/Dockerfile: Specifies the location of the Dockerfile. Provide the correct path if it's not in the current directory. The dot (.) represents the current working directory.
Docker tags are labels for container images used to differentiate versions and variants of an image during development and deployment. These tags help you identify and distinguish between different versions of Docker images. Using Docker tags enables efficient version control and management, facilitating quick and reliable continuous deployment.
Now, moving further, let us understand the Structure of Docker Image.
The software layers that make up a Docker image simplify the configuration of dependencies needed to run a container.
Now, moving forward, let us create a Docker Image.
To generate a Docker Image and launch it as a container, follow these steps:
Step 1: Make a Dockerfile with the building instructions for the image.
Step 2: To create the Docker image with all required dependencies, type the following command into the terminal:
build-t docker :.
The process of creating an image begins with this command.
Step 3: Move on to the following step after the Dockerfile and associated image have been successfully produced.
Step 4: To launch the program and build a running container with all necessary dependencies, type the following command into the terminal:
Docker run -p : at 9000:80
The port for accessing the application is '9000' in this command, whereas the port that the container exposes for host access is '80'.
Moving ahead, let us understand how to build Docker Python Images.
Make a Dockerfile specifically for Python programs to start. Provide instructions on how to use the CMD directive to launch the Python application. This is an example of a Dockerfile:
Dockerfile
As a basis, use the official Python image from Python:3.9-thin
In the container, set the working directory.
WORKDIR/application
Move the contents of the current directory to the container located at /app.
COPY. /application
Install any dependencies needed, as listed in requirements.txt.
USE pip install -r requirements.txt -no-cache-dir
Open port 80 to the outside world from this container.
DISCLOSE 80
Explain the environment variable.
World ENV NAME
When the container launches CMD ["python," "app.py"], execute app.py.
Next, include an app.py file with the Python program and a requirements.txt file with the names of the necessary applications. Here is an example of what's in these files:
requirements.txt:
plaintext
Flask==2.1.0 app.py: import Python from Flask Flask __name__ = Flask(@app.route('/'))
return 'Hello, World!' from function hello_world()
if __name__ == '__main__', then app.run(host='0.0.0.0', port=80), debug=True
To create the Docker Python image from the Dockerfile, use the following command:
build -t my python-app:v1 with bash docker image.
Check the successful build of the Docker application with the following command:
bash
docker images
Run a container using the Docker Python image with the following command:
bash
docker run -dit -p 80:80 --name mycontainer1 my python-app:v1
You can access the Docker container with the public IP of the host system and the exposed port to view the successful run of the containerized Python application.
Use the below command to list Docker images:
bash
docker images
Pull a Docker image from a registry by using the command:
bash
docker image pull <image-name>
Remove unused Docker images not associated with any containers with the command:
bash
docker image prune
Filter specific images by labels or meta-information tags using the command:
bash
docker image ls -f "reference=python-app"
Remove an Image from the Docker
bash
docker rmi <id-of-image>
Search for a specific image on Docker Hub with the command:
bash
docker search ubuntu
In conclusion, Docker images and containers revolutionize applications' deployment and management. Docker images package all necessary components, including code, dependencies, and runtime, making them portable and easy to deploy across various environments.
Docker containers leverage these images to create lightweight, isolated environments that ensure consistency and reliability in application execution. With Docker, developers can streamline the development and deployment process, increasing efficiency and scalability in modern software development practices.
1. What is a Docker image?
A Docker image is like a box containing everything your computer program needs to run. It includes the program, all the extra stuff it needs, and even the settings. This box makes moving your program from one computer to another easy.
2. What is the purpose of Docker?
Docker simplifies building, sending, and using computer programs. It wraps up your program and all its parts into one neat package, making it easier to run your program on different computers without worrying about setup problems.
3. Is a Docker image a Dockerfile?
Nope! A Docker image is the finished package with everything your program needs to run. A Dockerfile is more like a recipe that tells Docker how to assemble that package.
4. How do I build a Docker image?
To build a Docker image, you write a simple set of instructions called a Dockerfile. Then, you tell Docker to follow those instructions and make the image. It's like giving directions to build a LEGO set.
5. What is a container image?
A container image is just another name for a Docker image. It's like a lunchbox that holds your program and all its stuff, ready to be opened and used whenever needed.
6. How do I run a Docker image?
To run a Docker image, you use a special command that tells Docker to open the lunchbox and start your program. It's like saying, "Hey, Docker, let's use this lunchbox now!"
7. What are the benefits of Docker?
Docker makes it easy to share and run programs by packaging everything neatly. It keeps programs running smoothly, no matter where they go and saves time by ensuring everything works the same everywhere.
8. What is Docker and Kubernetes?
Docker is like a chef that packs your program into a lunchbox. Kubernetes is like a waiter who helps serve many lunchboxes at once, ensuring each gets to the right place and works well.
9. Where are Docker images stored?
Docker images are kept in a special online storage called a registry. It's like a library where you can put your lunchboxes so others can use them. The most popular one is Docker Hub, but you can also have your private storage.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.