Basic Docker Commands
Docker commands are essential for managing Docker containers, images, networks, and volumes. This guide covers key Docker commands, examples, and best practices for using Docker commands with Express.js applications.
Key Docker Commands
- docker version: Display the Docker version information.
- docker info: Display system-wide information.
- docker build: Build an image from a Dockerfile.
- docker images: List images.
- docker run: Run a command in a new container.
- docker ps: List containers.
- docker stop: Stop one or more running containers.
- docker rm: Remove one or more containers.
- docker rmi: Remove one or more images.
- docker exec: Run a command in a running container.
- docker logs: Fetch the logs of a container.
- docker-compose: Define and run multi-container applications.
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!');
});
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"]
Using Basic Docker Commands
Build and run the Docker container for your Express application using basic Docker commands:
Build the Docker Image
// Build the Docker image
docker build -t my-express-app .
Run the Docker Container
// Run the Docker container
docker run -p 3000:3000 my-express-app
// Open http://localhost:3000 in your browser to see the application running
List Docker Images
// List Docker images
docker images
List Running Containers
// List running containers
docker ps
Stop a Running Container
// Stop a running container
docker stop
Remove a Container
// Remove a container
docker rm
Remove an Image
// Remove an image
docker rmi my-express-app
Run a Command in a Running Container
// Run a command in a running container
docker exec -it /bin/bash
Fetch Container Logs
// Fetch the logs of a container
docker logs
Using Docker Compose
Set up Docker Compose to manage multi-container applications:
Example: docker-compose.yml
// docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
// Run Docker Compose
docker-compose up
// Open http://localhost:3000 in your browser to see the application running
Best Practices for Using Docker Commands
- 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.
- Use Environment Variables: Store configuration settings in environment variables to keep your Docker images environment-agnostic.
- Automate Builds: Use CI/CD pipelines to automate the building, testing, and deployment of your Docker images.
- Monitor and Log: Monitor your Docker containers and log important events for debugging and auditing.
Testing Docker Command Integration
Test your Docker 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!', async () => {
const response = await axios.get('http://localhost:3000');
expect(response.data).to.equal('Hello, Docker!');
});
});
// Add test script to package.json
// "scripts": {
// "test": "mocha"
// }
// Run tests
// docker run -p 3000:3000 my-express-app
// npm test
Key Points
- docker version: Display the Docker version information.
- docker info: Display system-wide information.
- docker build: Build an image from a Dockerfile.
- docker images: List images.
- docker run: Run a command in a new container.
- docker ps: List containers.
- docker stop: Stop one or more running containers.
- docker rm: Remove one or more containers.
- docker rmi: Remove one or more images.
- docker exec: Run a command in a running container.
- docker logs: Fetch the logs of a container.
- docker-compose: Define and run multi-container applications.
- Follow best practices for using Docker commands, such as keeping images lightweight, using .dockerignore, tagging images properly, using environment variables, automating builds, and monitoring and logging.
Conclusion
Docker commands are essential for managing Docker containers, images, networks, and volumes. By understanding and implementing the key commands, examples, and best practices covered in this guide, you can effectively use Docker commands to manage your Express.js applications. Happy coding!