Docker is a platform that automates deploying applications in portable containers. These containers include everything needed to run the application, ensuring consistency across different environments. Key components are Docker Engine, Docker Image, and Docker Container.
Key Features and Benefits:
Portability: Consistent deployment across environments.
Isolation: Independent application execution without interference.
Efficiency: Lightweight containers share the host OS kernel.
Scalability: Easily scale applications across hosts or a cluster.
Versioning and Rollback: Support for versioning and rollback of application images.
DevOps and CI/CD: Facilitates automation in development, testing, and deployment.
Microservices: Ideal for microservices architecture.
Resource Optimization: Efficient use of system resources.
Limitations of Docker:
Complex Multi-Container Applications: Managing multiple containers independently can be complex.
Orchestration Complexity: Docker alone may lack advanced orchestration features for complex production deployments.
Learning Curve: Steeper learning curve for configuring networking and services in complex scenarios.
Example:
This setup creates a Docker image for a simple Node.js app using Express. When the container starts, it runs the Express app responding with "I am Batman!" at http://<ip-addr>:3000
. The Dockerfile ensures proper setup and dependency installation.
Directory Structure:
bruno@Batman-2 Docker % tree . ├── Dockerfile ├── docker.sh ├── package.json └── server.js 1 directory, 4 files
Dockerfile:
Starts with Node.js 16 image.
Sets working directory to
/usr/src/app
.Copies and installs dependencies.
Copies all files, including
server.js
.Default command:
["npm", "start"]
.
package.json:
Name: "simple-node"
Version: "1.0.0"
Description: "A sample Docker application"
Main File: "server.js"
Scripts: {"start": "node server.js"}
Author: "Bruno"
server.js:
Requires Express and creates an app.
Defines a route for root, responding with "I am Batman!".
Listens on port 3000, logs server start messages.
bruno@Batman-2 Docker % ls -la
total 32
drwxr-xr-x 6 bruno staff 192 Feb 18 11:58 .
drwxr-xr-x 6 bruno staff 192 Feb 18 11:48 ..
-rw-r--r-- 1 bruno staff 13 Feb 18 11:58 .dockerignore
-rw-r--r-- 1 bruno staff 405 Feb 18 12:07 Dockerfile
-rw-r--r-- 1 bruno staff 214 Feb 18 11:47 package.json
-rw-r--r-- 1 bruno staff 245 Feb 18 11:56 server.js
bruno@Batman-2 Docker %
bruno@Batman-2 Docker % tree
.
├── Dockerfile
├── package.json
└── server.js
bruno@Batman-2 Docker % cat Dockerfile
# Start from a Node.js 16 (LTS) image
FROM node:16
# Specify the directory inside the image in which all commands will run
WORKDIR /usr/src/app
# Copy package files and install dependencies
COPY package*.json ./
RUN npm install
RUN npm install express # Corrected typo here
# Copy all of the app files into the image
COPY . .
# Default Command to run when starting the container
CMD ["npm", "start"]
bruno@Batman-2 Docker % cat server.js
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('I am Batman!');
});
app.listen(3000, function() {
console.log('Listening on port 3000!');
console.log('<http://localhost:3000>');
});
bruno@Batman-2 Docker % cat server.js
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('I am Batman!');
});
app.listen(3000, function() {
console.log('Listening on port 3000!');
console.log('<http://localhost:3000>');
});
bruno@Batman-2 Docker %
I have a Virtual Machine (Ubuntu 20.04) where I installed Docker using a Docker installation script.
kato@docker-srv:~/Bat-Blog/Docker$ cat docker.sh
#!/bin/bash
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker kato #my case is kato
newgrp docker
sudo chmod 777 /var/run/docker.sock
kato@docker-srv:~/Bat-Blog/Docker$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2024-02-18 10:30:36 UTC; 4min 40s ago
TriggeredBy: ● docker.socket
Docs: <https://docs.docker.com>
Main PID: 1064 (dockerd)
Tasks: 18
Memory: 113.2M
CGroup: /system.slice/docker.service
└─1064 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Feb 18 10:30:33 docker-srv dockerd[1064]: time="2024-02-18T10:30:33.802602013Z" level=info msg=>
Feb 18 10:30:33 docker-srv dockerd[1064]: time="2024-02-18T10:30:33.954573007Z" level=info msg=>
Feb 18 10:30:34 docker-srv dockerd[1064]: time="2024-02-18T10:30:34.273565930Z" level=info msg=>
Feb 18 10:30:35 docker-srv dockerd[1064]: time="2024-02-18T10:30:35.496493277Z" level=info msg=>
Feb 18 10:30:35 docker-srv dockerd[1064]: time="2024-02-18T10:30:35.568612673Z" level=info msg=>
Feb 18 10:30:35 docker-srv dockerd[1064]: time="2024-02-18T10:30:35.721085438Z" level=warning m>
Feb 18 10:30:35 docker-srv dockerd[1064]: time="2024-02-18T10:30:35.724989433Z" level=info msg=>
Feb 18 10:30:35 docker-srv dockerd[1064]: time="2024-02-18T10:30:35.729136346Z" level=info msg=>
Feb 18 10:30:36 docker-srv systemd[1]: Started Docker Application Container Engine.
Feb 18 10:30:36 docker-srv dockerd[1064]: time="2024-02-18T10:30:36.010195825Z" level=info msg=>
kato@docker-srv:~/Bat-Blog/Docker$ docker build -t simple-node .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
<https://docs.docker.com/go/buildx/>
Sending build context to Docker daemon 6.144kB
Step 1/7 : FROM node:16
---> 1ddc7e4055fd
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> f4de5b458507
Step 3/7 : COPY package*.json ./
---> b3f62a1f0940
Step 4/7 : RUN npm install
---> Running in ebc4f5ecdb46
up to date, audited 1 package in 196ms
found 0 vulnerabilities
Removing intermediate container ebc4f5ecdb46
---> 1035c62b7076
Step 5/7 : RUN npm install express # Corrected typo here
---> Running in 460f8147e7af
added 64 packages, and audited 65 packages in 20s
12 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New major version of npm available! 8.19.4 -> 10.4.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.4.0>
npm notice Run `npm install -g npm@10.4.0` to update!
npm notice
Removing intermediate container 460f8147e7af
---> d199bf18f937
Step 6/7 : COPY . .
---> c9e51adbffb9
Step 7/7 : CMD ["npm", "start"]
---> Running in 9f1ae96f702a
Removing intermediate container 9f1ae96f702a
---> 07604be49925
Successfully built 07604be49925
Successfully tagged simple-node:latest
kato@docker-srv:~/Bat-Blog/Docker$ docker run --rm -p 3000:3000 simple-node
> simple-node@1.0.0 start
> node server.js
Listening on port 3000!
<http://localhost:3000>
From my web browser (Screenshot)
If you're interested in experimenting with Docker, feel free to check out the source code on my GitHub repository: https://github.com/Gatete-Bruno/Bat-Blog