Skip to content

Latest commit

 

History

History
141 lines (96 loc) · 4.85 KB

getting_started_with_docker.md

File metadata and controls

141 lines (96 loc) · 4.85 KB

Getting Started with Docker for API Projects

Welcome to our API exercise platform! We're here to help you get started with Docker, understand the provided Dockerfile template, and modify it to suit your chosen language and framework. Don't worry—setting up a working Dockerfile is easier than it seems!

Step 1: Installing Docker

Before you can use Docker, you need to install it on your machine. Follow these simple instructions for your operating system:

For Windows:

  1. Download Docker Desktop from Docker's official website.
  2. Run the installer and follow the on-screen instructions.
  3. After installation, Docker Desktop should start automatically. If not, open it from the Start menu.

For macOS:

  1. Download Docker Desktop from Docker's official website.
  2. Open the downloaded .dmg file and drag Docker to your Applications folder.
  3. Open Docker from your Applications folder.

For Linux:

  1. Follow the instructions on the Docker installation page.
  2. Make sure to follow any additional steps for your specific distribution.

Step 2: Understanding the Dockerfile Template

Here's the Dockerfile template you will be working with:

# Use an official runtime as a parent image
FROM <runtime_image>

# Set the working directory in the container
WORKDIR /{project_name}

# Install any dependencies
RUN <install_command>

# Copy the current directory contents into the container at /app
COPY src/ .

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run the application
CMD [ "<start_command>" ]

Step 3: Modifying the Dockerfile for Your Project

You can choose any programming language and API framework for your project. The key is to write the Dockerfile correctly, and we believe you can do it! Here are some examples for popular languages and frameworks to help you get started. Feel free to Google for more examples specific to your setup.

Example: Python (Flask)

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
# The correct dir here will be injected automatically whenever you click 'Start Project'
WORKDIR /library_management_system

# Install any dependencies
RUN pip install flask

# Copy the current directory contents into the container at /app
COPY src/ .

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run the application
CMD [ "python", "app.py" ]

Example: Node.js (Express)

# Use an official Node.js runtime as a parent image
FROM node:14

# Set the working directory in the container
# The correct dir here will be injected automatically whenever you click 'Start Project'
WORKDIR /library_management_system

# Install any dependencies
COPY package*.json ./
RUN npm install

# Copy the current directory contents into the container at /app
COPY src/ .

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run the application
CMD [ "node", "app.js" ]

Example: Java (Spring Boot)

# Use an official OpenJDK runtime as a parent image
FROM openjdk:11

# Set the working directory in the container
# The correct dir here will be injected automatically whenever you click 'Start Project'
WORKDIR /library_management_system

# Install any dependencies (Maven in this case)
RUN apt-get update && apt-get install -y maven

# Copy the current directory contents into the container at /app
COPY src/ .

# Build the application
RUN mvn clean package

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run the application
CMD [ "java", "-jar", "target/myapp.jar" ]

Step 4: Customizing for Your Project

  • Choose the base image: Look for an official image that suits your programming language and framework. You can find these on Docker Hub.
  • Set the working directory: This is already set for you at /project_name, so no changes needed here!
  • Install dependencies: Use the appropriate command to install your project's dependencies.
  • Copy your project files: Adjust the COPY command if your project structure differs.
  • Expose the correct port: Ensure the port you expose matches the port your application runs on.
  • Run your application: Modify the CMD to start your application correctly.

Final Tips

  • Google is your friend: Search for Dockerfile examples specific to your language and framework.
  • Look at official documentation: Many frameworks provide Dockerfile examples and best practices.
  • Ask for help: If you get stuck, don't hesitate to ask questions in our community or look for solutions on forums like Stack Overflow.

Remember, you can do this! Setting up a Dockerfile is a great skill to have, and with a bit of practice, it will become second nature. Happy coding!