Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Dockerfile

    Docker Tutorials


    A Dockerfile is a text file named Dockerfile (without extension) that contains Docker's instructions for creating images. Each instruction defines a specific step, such as choosing a base image, installing packages, copying files, or specifying how to start containers. Mastering Dockerfiles allows for efficient, consistent, and reproducible image creation, which simplifies your development and deployment workflows.

    Why Use a Dockerfile?

    A Dockerfile automates image creation and eliminates the need for manual setup steps. The key advantages include:

    • Consistency across different development and production environments.
    • Automation of the repetitive image-building tasks.
    • Version control for environment configurations.
    • Portability, allowing images to run seamlessly across various platforms.

    Dockerfiles replace manual commands like docker commit, offering a transparent, maintainable approach to image management.

    Dockerfile Syntax and Instructions

    Here are commonly used Dockerfile instructions:

    Instruction Description
    FROM Specifies the base image (required)
    RUN Executes commands during image build
    COPY Copies files/directories from host to image
    ADD Similar to COPY but supports remote URLs and archives
    CMD Sets the default command for container startup
    ENTRYPOINT Configures container execution as a specific command
    WORKDIR Defines the working directory within the container
    ENV Sets environment variables
    EXPOSE Indicates the port the container listens on

    Creating and Using a Dockerfile

    Follow these steps to build and run Docker images using Dockerfiles:

    Step 1: Create a Dockerfile

    Create a file named exactly Dockerfile (no extension) in your project's root directory.

    Example: Dockerfile for a Node.js App

    Below is a simple Dockerfile for a Node.js app that uses index.js and package.json:

    # Set base image
    FROM node:18
    
    # Set working directory inside the container
    WORKDIR /app
    
    # Copy package.json and install dependencies
    COPY package.json ./
    RUN npm install
    
    # Copy application code
    COPY . .
    
    # Expose port 3000
    EXPOSE 3000
    
    # Run the app
    CMD ["node", "index.js"]
    

    This Dockerfile uses the official Node.js image, sets the working directory, installs dependencies, copies your code, exposes port 3000, and runs the app using Node.

    Step 2: Build Docker Image

    To build the image using the Dockerfile, open a terminal in the project folder and run docker build -t my-node-app . where -t my-node-app names the image and . tells Docker to use the current directory for context.

    docker build -t my-node-app .
    

    Step 3: Run the Container

    After building the image, run the container:

    docker run -p 3000:3000 my-node-app
    

    This command maps port 3000 of the container to your local machine, making the app accessible from your browser.

    Example: Dockerfile for a Python Flask App

    Here's a Dockerfile for a simple Flask application:

    # Base image with Python
    FROM python:3.10
    
    # Set working directory
    WORKDIR /app
    
    # Install dependencies
    COPY requirements.txt .
    RUN pip install -r requirements.txt
    
    # Copy source code
    COPY . .
    
    # Expose port 5000
    EXPOSE 5000
    
    # Run the app
    CMD ["python", "app.py"]
    

    Build the Docker image:

    docker build -t my-flask-app .

    Run the container, mapping port 5000:

    docker run -p 5000:5000 my-flask-app

    Your Flask app will now be accessible locally.

    Conclusion

    This tutorial taught you how a Dockerfile automates Docker image creation, why it is necessary, and how to create and use Dockerfiles effectively. Examples for Node.js and Flask applications demonstrate practical application, allowing you to apply these concepts in real-world situations.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.