About Docker Image
A Docker image is like a template that instructs how to create and run a Docker container.
A Docker image is a file that contains everything needed to run an application inside a Docker container. It includes the application code, libraries, tools, dependencies and other files.
A Docker image can have multiple layers, each one building on top of the previous one. You can create your own Docker images or use existing ones from the Docker Hub.
To use a Docker image, you need to run it using the docker run command. This will create a running container based on the image. You can also specify options such as the port mapping, environment variables, volumes, etc.
Let's take a basic example to understand what Image is. Suppose you create a backup of your Window 10Pro OS which could be an ISO image file as a result. This ISO Image will contain all software Codes, libraries, dependencies that can be used later on to recover / recreate Windows 10Pro OS.
In the same way we create Docker image which contains code,library,dependencies for code execution.
About "Container" or Docker Container
A Docker container is an isolated environment for running an application using a Docker image. A Docker container has its own file system, network, and process space, but it shares the host's operating system kernel. A Docker container can run on any machine that has Docker installed, regardless of the underlying infrastructure. You can create, start, stop, and delete Docker containers using the docker command-line tool or the Docker Desktop GUI
To create a Docker container, you need to specify which Docker image to use as the base. You can also provide other options such as the name, port mapping, environment variables, volumes, etc.
As per above example we created ISO file, now if we wish to install Windows 10Pro in any Laptop/Desktop, we will insert that ISO Image file and then we start installation of Windows 10Pro.
We can assume in the same way, Container uses the docker image files and executes the codes inside the container using that image.
Before proceeding for this example let's understand the basic way, which we follow without Docker-Container technology. Let's suppose we are going to write code in Node.js programming language.
- We first install Node.js software in our laptop and it will install all dependencies as well.
- Post that we create some projects / write some codes and execute that projects/codes.
- Once code execution completes, we get our final output as excepted if everything goes well.
- Start Docker Desktop which starts Docker Engine as well.
- Open Visual Studio Code and write codes about that project and embed all dependencies in those codes as well.
- Create Image from above written code.
- Create containers from above resulted images which in turns execute the codes in container.
DockerfileFROM node:14WORKDIR /appCOPY package.json .RUN npm installCOPY . .EXPOSE 1000CMD [ "node", "app.mjs" ]
app.mjsimport express from 'express';import connectToDatabase from './dbconn.mjs'const app = express();app.get('/', (req, res) => {res.send('<h2>Congratulations for your First Porject !!</h2>');});await connectToDatabase();app.listen(1000);
dbconn.mjsconst connectToDatabase = () => {const dummyPromise = new Promise((resolve, reject) => {setTimeout(() => {resolve();}, 1000);});return dummyPromise;};export default connectToDatabase;
package.json{"name": "docker-complete","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"repository": {"type": "git","url": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/docker-complete-guide"},"author": "","license": "ISC","dependencies": {"express": "^4.17.1"}}
We are having below files in Visual Code Studio (VCS).
Steps to Compile and Run Application.
1=> Create Image from Codes and Run Container
Open terminal in Visual Code Studio (VCS) and execute below commands to Run the Sample projects.
PS D:\Docker> ls Directory: D:\Docker Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 04-07-2023 14:14 247 app.mjs -a---- 05-10-2020 18:40 214 dbconn.mjs -a---- 04-07-2023 12:49 114 Dockerfile -a---- 04-07-2023 14:14 394 package.json PS D:\Docker> docker build . [+] Building 2.3s (10/10) FINISHED => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 151B 0.0s => [internal] load metadata for docker.io/library/node:14 2.1s => [1/5] FROM docker.io/library/node:14@sha256:a158d3b9b4e3fa813fa6c8c590b8f0a860e015ad4e59bbce5744d2f6 0.0s => [internal] load build context 0.0s => => transferring context: 120B 0.0s => CACHED [2/5] WORKDIR /app 0.0s => CACHED [3/5] COPY package.json . 0.0s => CACHED [4/5] RUN npm install 0.0s => CACHED [5/5] COPY . . 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:d618acc2a79832666204277c872644589df5760cae721f274a61c46fb248baa2 0.0s PS D:\Docker> PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> d618acc2a798 15 minutes ago 916MB PS D:\Docker> PS D:\Docker> docker run -p 1000:1000 d618acc2a798
Description of Commands
The commands used in above code can be described as below.
> ls
this command tell files/folders in particular folders.
> docker build .
this command direct docker to create image using Dockerfile which is present in folder.
> docker image
once image is built this will list all images available at local repository. And this image will be used to create container further.
> docker run -p 1000:1000 *IMAGEID*
this command tells docker to create container based on specified image file and run container while listening on 1000 port and exposing port 1000 on host machine.
2=> Test Application Code
Next step is to open Browser and enter below URL and your application is live now through Docker.
http://localhost:1000/
PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03b2aa0d7dc5 d618acc2a798 "docker-entrypoint.s…" 38 seconds ago Up 36 seconds 0.0.0.0:1000->1000/tcp gracious_tu PS D:\Docker> PS D:\Docker> docker stop gracious_tu gracious_tu
Description of Commands
The commands used in above code can be described as below.
> docker ps -a
list all running/stopped containers with container name and id
> docker stop *CONTAINER_NAME*
this will stop any running container which is specified in command.
No comments:
Post a Comment