Docker image example

Creating Docker images using Dockerfiles is a straightforward process that involves defining the steps required to build the image in a text file called a Dockerfile. Here's a step-by-step demonstration of how to create a simple Docker image using a Dockerfile:

  • Create a Dockerfile: Open a text editor and create a new file named "Dockerfile" in your project directory.
  • Write Dockerfile Instructions:
  • Add the following instructions to the "Dockerfile":

    # Use an existing base image as the starting point
    FROM alpine:latest
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # Copy the application files from the host to the container
    COPY . /app
    
    # Install any dependencies needed for the application
    RUN apk add --no-cache python3
    
    # Set the command to run when the container starts
    CMD ["python3", "app.py"]

    In this Dockerfile:

    • "FROM" specifies the base image to use (in this case, Alpine Linux).
    • "WORKDIR" sets the working directory inside the container.
    • "COPY" copies files from the host directory (current directory) to the container's working directory.
    • "RUN" executes commands in the container to install dependencies (here, Python 3).
    • "CMD" specifies the default command to run when the container starts (in this case, running a Python script named "app.py").
  • Create Application Files:
  • Create the application files (e.g., "app.py") in the same directory as the "Dockerfile". For this demonstration, let's create a simple Python script:

    # app.py
    print("Hello, Docker!")
  • Build the Docker Image:
  • Open a terminal or command prompt, navigate to the directory containing the "Dockerfile" and application files, and run the following command to build the Docker image:

    docker build -t my-python-app .
    • "-t my-python-app" tags the built image with a name ("my-python-app" in this case) to make it easier to reference.
    • "." specifies the build context, indicating that Docker should use the current directory as the context for building the image.
  • Verify the Docker Image:
  • Once the build process completes, you can verify that the Docker image was created successfully by running the following command:

    docker images

    This command lists all the Docker images on your system, and you should see "my-python-app" listed among them.

  • Run the Docker Container:
  • Finally, you can run a Docker container using the newly created image by running the following command:

    docker run my-python-app

    This command starts a container from the "my-python-app" image and executes the default command specified in the Dockerfile "python3 app.py". You should see the output "Hello, Docker!" printed to the terminal, indicating that the container is running the Python application successfully.

That's it! You've successfully created a Docker image using a Dockerfile and run a container based on that image. This demonstration illustrates the basic steps involved in building Docker images and running containers using Dockerfiles.