Can you provide more Docker examples?

Category : Docker | Sub Category : Docker for Java Developers | By Prasad Bonam Last updated: 2023-08-03 14:19:23 Viewed : 431

Here are some additional Docker examples to help you understand and explore various features of Docker:

1. Running an Official Docker Image: Docker Hub provides a wide range of official and community-contributed images. For example, you can quickly run an Nginx web server using the official Nginx image:

docker run -d -p 80:80 nginx

This command pulls the Nginx image from Docker Hub, creates a container, and maps port 80 of the container to port 80 on the host. You can access the Nginx welcome page by visiting http://localhost in your web browser.

2. Docker Compose: Docker Compose is a tool for defining and managing multi-container Docker applications. Create a docker-compose.yml file to define the services and their configurations:

version: `3` services: web: image: nginx ports: - "80:80"

Run the services using Docker Compose:

docker-compose up -d

3. Building an Image with a Custom Application: Suppose you have a simple Node.js application in a directory named my-app, which includes app.js and package.json. Create a Dockerfile in the same directory:

# Use an existing Node.js base image FROM node:14 # Set the working directory inside the container WORKDIR /app # Copy the application files into the container COPY package.json package-lock.json ./ COPY app.js ./ # Install dependencies RUN npm install # Expose the port that the application listens on EXPOSE 3000 # Command to run the application CMD ["node", "app.js"]

Build and run the Docker image:

docker build -t my-custom-app . docker run -p 3000:3000 my-custom-app

4. Data Volumes: Docker volumes allow data to persist between container instances. For example, you can use a data volume to store a databases data:

docker volume create my-db-data docker run -d -p 5432:5432 -v my-db-data:/var/lib/postgresql/data postgres:latest

The -v option binds the volume my-db-data to the PostgreSQL containers data directory.

5. Docker Networking: By default, containers can communicate with each other using Dockers internal networking. Create a custom network to isolate containers:

docker network create my-network docker run -d --name my-app --network my-network my-custom-app docker run -d --name my-db --network my-network postgres:latest

Containers my-app and my-db are connected on the my-network network, allowing them to communicate using their container names.

6. Multi-Stage Builds: To optimize image size, use multi-stage builds. For example, a multi-stage Dockerfile for a Node.js application might look like this:

# Build stage FROM node:14 AS builder WORKDIR /app COPY package.json package-lock.json ./ RUN npm install COPY . . RUN npm run build # Production stage FROM nginx COPY --from=builder /app/dist /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]

The first stage (builder) builds the application, and the second stage (production) creates a lightweight production image.

These are just a few examples to showcase some common Docker use cases and features. Docker offers numerous capabilities for creating, deploying, and managing containers and applications. As you continue to work with Docker, you will discover more advanced use cases and ways to optimize your containerized applications.

Related Articles

Leave a Comment: