Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Docker Images

Docker images are read-only templates used to create containers. They include everything needed to run a piece of software, including code, runtime, system tools, libraries, and settings. This guide covers key concepts, steps to create and manage Docker images, examples, and best practices for using Docker images with Express.js applications.

Key Concepts of Docker Images

  • Docker Image: A read-only template used to create containers. Images are built from Dockerfiles.
  • Dockerfile: A text file that contains instructions on how to build a Docker image.
  • Layers: Each instruction in a Dockerfile creates a layer in the image. Layers are cached and reused to speed up the build process.
  • Tag: A label applied to a Docker image in a repository, used to differentiate between different versions of the image.
  • Registry: A storage and distribution system for Docker images. Docker Hub is a public registry, while private registries can also be used.

Setting Up the Project

Initialize a new Express.js project and create a Dockerfile:

// Initialize a new project
// npm init -y

// Install Express
// npm install express

// Create the project structure
// mkdir src
// touch src/index.js Dockerfile .dockerignore .gitignore

// .gitignore
node_modules
.env

// .dockerignore
node_modules
npm-debug.log

Creating an Express Application

Create a simple Express application:

Example: index.js

// src/index.js
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello, Docker Images!');
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
});

Creating a Dockerfile

Create a Dockerfile to containerize your Express application:

Example: Dockerfile

// Dockerfile
FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Expose port and start application
EXPOSE 3000
CMD ["node", "src/index.js"]

Building the Docker Image

Build the Docker image for your Express application:

// Build the Docker image
docker build -t my-express-app .

Running the Docker Container

Run the Docker container for your Express application:

// Run the Docker container
docker run -p 3000:3000 my-express-app

// Open http://localhost:3000 in your browser to see the application running

Tagging the Docker Image

Tag the Docker image to manage different versions:

// Tag the Docker image
docker tag my-express-app my-express-app:v1.0

Pushing the Docker Image to Docker Hub

Push the Docker image to Docker Hub:

// Log in to Docker Hub
docker login

// Push the Docker image to Docker Hub
docker push my-express-app:v1.0

Pulling the Docker Image from Docker Hub

Pull the Docker image from Docker Hub:

// Pull the Docker image from Docker Hub
docker pull my-express-app:v1.0

// Run the Docker container
docker run -p 3000:3000 my-express-app:v1.0

// Open http://localhost:3000 in your browser to see the application running

Inspecting Docker Images

Inspect Docker images to view details about them:

// Inspect a Docker image
docker inspect my-express-app:v1.0

Removing Docker Images

Remove Docker images when they are no longer needed:

// Remove a Docker image
docker rmi my-express-app:v1.0

Best Practices for Using Docker Images

  • Keep Images Lightweight: Use multi-stage builds and minimal base images to keep your Docker images small.
  • Use .dockerignore: Exclude unnecessary files and directories from the Docker build context to optimize build times.
  • Tag Images Properly: Use semantic versioning for your Docker image tags to manage different versions effectively.
  • Automate Builds: Use CI/CD pipelines to automate the building, testing, and pushing of Docker images to registries.
  • Scan for Vulnerabilities: Regularly scan your Docker images for vulnerabilities to ensure security.

Testing Docker Image Integration

Test your Docker image setup to ensure it works correctly:

Example: Testing with Mocha and Chai

// Install Mocha and Chai
// npm install --save-dev mocha chai

// test/app.test.js
const chai = require('chai');
const expect = chai.expect;
const axios = require('axios');

describe('Express App', () => {
    it('should return Hello, Docker Images!', async () => {
        const response = await axios.get('http://localhost:3000');
        expect(response.data).to.equal('Hello, Docker Images!');
    });
});

// Add test script to package.json
// "scripts": {
//   "test": "mocha"
// }

// Run tests
// docker run -p 3000:3000 my-express-app
// npm test

Key Points

  • Docker Image: A read-only template used to create containers. Images are built from Dockerfiles.
  • Dockerfile: A text file that contains instructions on how to build a Docker image.
  • Layers: Each instruction in a Dockerfile creates a layer in the image. Layers are cached and reused to speed up the build process.
  • Tag: A label applied to a Docker image in a repository, used to differentiate between different versions of the image.
  • Registry: A storage and distribution system for Docker images. Docker Hub is a public registry, while private registries can also be used.
  • Follow best practices for using Docker images, such as keeping images lightweight, using .dockerignore, tagging images properly, automating builds, and scanning for vulnerabilities.

Conclusion

Docker images are essential for creating and managing containers in a consistent and efficient manner. By understanding and implementing the key concepts, steps, examples, and best practices covered in this guide, you can effectively use Docker images to manage your Express.js applications. Happy coding!