Getting Started with Docker
Docker simplifies the process of creating, deploying, and running applications by using containerization.
What is Docker?
Docker is an open-source platform designed to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate an application and its dependencies, ensuring that it runs consistently across different environments.
Key Concepts
Before diving into practical steps, let’s understand some key Docker concepts: - Images: Read-only templates that define how a container should be instantiated. They include everything needed to run an application (code, runtime, libraries, etc.). - Containers: Instances of Docker images that run as isolated processes in user space on the host operating system. - Dockerfile: A script containing instructions on how to build a Docker image. - Docker Hub: A cloud-based registry service for sharing Docker images.
Installation
To start using Docker, you need to install Docker Desktop on your machine. Follow these steps based on your operating system: Windows and macOS
1. Download Docker Desktop:
- Visit the Docker Desktop download page and download the installer for your operating system.
2. Install Docker Desktop:
- Run the installer and follow the on-screen instructions.
3. Verify Installation:
- Open a terminal or command prompt and run: docker --version - You should see the installed Docker version. Linux
1. Install Docker Engine: - Follow the official installation guide for Docker Engine based on your Linux distribution. 2. Verify Installation: - Open a terminal and run: docker --version - You should see the installed Docker version.
Your First Docker Container
Let’s create a simple Docker container running an Nginx web server. 1. Pull the Nginx Image:
- Open a terminal and run: docker pull nginx - This command pulls the Nginx image from Docker Hub to your local machine.
2. Run the Nginx Container:
- Start an Nginx container using the pulled image: docker run --name my-nginx -p 8080:80 -d nginx - This command runs the Nginx container, names it my-nginx, maps port 80 of the container to port 8080 of the host, and runs it in detached mode.
3. Verify the Nginx Container:
- Open a web browser and navigate to http://localhost:8080. - You should see the Nginx welcome page.
Dockerfile Basics
A Dockerfile is a script containing instructions to build a Docker image. Let’s create a Dockerfile for a simple Node.js application. 1. Set Up the Project Directory: - Create a new directory for your project and navigate into it: mkdir my-node-app cd my-node-app
2. Create a Node.js Application: - Initialize a new Node.js project and install Express: npm init -y npm install express - Create an index.js file with the following content: const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello, Docker!'); }); app.listen(port, () => { console.log(`App running at http://localhost:${port}`); });
3. Create a Dockerfile: - In the same directory, create a file named Dockerfile with the following content: # Use the official Node.js image as the base image FROM node:14 # Set the working directory WORKDIR /usr/src/app # Copy package.json and package-lock.json COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application code COPY . . # Expose the application port EXPOSE 3000 # Command to run the application CMD ["node", "index.js"] 4. Build the Docker Image: - Build the image from the Dockerfile: docker build -t my-node-app .
5. Run the Docker Container: - Start a container from the built image: docker run -p 3000:3000 my-node-app
6. Verify the Application: - Open a web browser and navigate to http://localhost:3000. - You should see the message "Hello, Docker!" Managing Docker Containers Here are some useful commands to manage Docker containers: - List Running Containers: docker ps Shows all running containers.
- Stop a Container: docker stop <container_id> Stops the specified container.
- Remove a Container: docker rm <container_id> Removes the specified container.
- List Docker Images: docker images Lists all Docker images on your local machine.
- Remove a Docker Image: docker rmi <image_id> Removes the specified Docker image. Happy Dockerizing!