Docker Dockerfile

Docker Dockerfile

A Dockerfile is a script that contains instructions for building a Docker image. Docker images are the basis for creating containers that can run applications in a consistent and portable manner. In this tutorial, we will cover the basics of creating a simple Dockerfile and building an image from it.

  1. Prerequisites:

    • Install Docker on your system
  2. Create a new directory for your Dockerfile:

    mkdir my-docker-project cd my-docker-project 
  3. Create a new file named Dockerfile in the newly created directory:

    touch Dockerfile 
  4. Open the Dockerfile in a text editor and add the following lines:

    # Use an official Python runtime as a parent image FROM python:3.8-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --trusted-host pypi.python.org -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"] 

    Here's a breakdown of the Dockerfile:

    • FROM specifies the base image you want to build from (in this case, the official Python 3.8 image).
    • WORKDIR sets the working directory for any subsequent instructions.
    • COPY copies files from the source on the host machine to the destination in the container.
    • RUN executes a command during the build process.
    • EXPOSE opens a specific port for external access.
    • ENV sets environment variables.
    • CMD specifies the command to run when the container starts.
  5. Create a simple Python web app and requirements.txt file:

    app.py:

    from flask import Flask import os app = Flask(__name__) @app.route('/') def hello(): return f'Hello {os.environ.get("NAME")}!' if __name__ == '__main__': app.run(host='0.0.0.0', port=80) 

    requirements.txt:

    Flask==1.1.2 
  6. Build the Docker image:

    docker build -t my-docker-image . 

    The -t flag assigns a tag or name to the image. The . at the end specifies the build context, which is the current directory.

  7. Run the Docker container:

    docker run -p 4000:80 my-docker-image 

    This command maps port 4000 on the host machine to port 80 on the container.

  8. Access the app in your browser by navigating to http://localhost:4000.

Congratulations! You've successfully created a simple Dockerfile, built an image from it, and run a container using the image. This example just scratches the surface of what you can do with Dockerfiles, but it should provide a good starting point for further exploration.

Examples

  1. Dockerfile FROM Instruction:

    • Description: The FROM instruction in a Dockerfile specifies the base image for the subsequent instructions. It is the starting point for building your Docker image.
    • Code Example:
      FROM ubuntu:20.04 # Additional instructions go here 
  2. Dockerfile ENV vs ARG:

    • Description: ENV sets environment variables that are persistent in the image, while ARG defines build-time variables that are only accessible during the build process.
    • Code Example:
      ARG version=latest ENV APP_VERSION=$version 
  3. Dockerfile COPY vs ADD:

    • Description: Both commands copy files from the host to the container, but COPY is preferred for simple file copying, while ADD has additional features like URL support and auto-extraction of compressed files.
    • Code Example:
      COPY src/ /app/src/ # or ADD http://example.com/file.tar.gz /app/ 
  4. Multi-stage Builds in Dockerfile:

    • Description: Multi-stage builds allow you to use multiple FROM instructions in a single Dockerfile to create smaller, optimized images.
    • Code Example:
      FROM builder as build # Build stage FROM base COPY --from=build /app /app 
  5. Dockerfile CMD vs ENTRYPOINT:

    • Description: Both define the command to run when the container starts, but CMD provides default arguments that can be overridden, while ENTRYPOINT is intended for setting a fixed command with optional arguments.
    • Code Example:
      CMD ["python", "app.py"] # or ENTRYPOINT ["java", "-jar", "app.jar"] 
  6. Configuring Ports in Dockerfile:

    • Description: Exposes ports and specifies the network protocols used by the application running inside the container.
    • Code Example:
      EXPOSE 80/tcp 
  7. Setting Up Volumes in Dockerfile:

    • Description: Defines volumes to persist data or share data between containers.
    • Code Example:
      VOLUME /data 
  8. Dockerfile for Python Application:

    • Description: A Dockerfile tailored for running Python applications.
    • Code Example:
      FROM python:3.8 WORKDIR /app COPY . /app CMD ["python", "app.py"] 

More Tags

python-mock celery-task size tmx client-certificates maintainability modalviewcontroller external gantt-chart flutter-appbar

More Programming Guides

Other Guides

More Programming Examples