DEV Community

Ajeet Singh Raina
Ajeet Singh Raina

Posted on

🐳 Boost Your Docker Workflow: Introducing Docker Init for Python Developers πŸš€

Are you a Python developer still building Dockerfile and Docker Compose files manually? If the answer is "yes," then you're definitely missing out on an essential tool that can significantly streamline your development workflow.

Docker has introduced a powerful feature called "docker init," designed specifically for Python developers, to simplify the process of creating Docker assets. With docker init, you can say goodbye to manual configuration and enjoy a more efficient and productive development experience. Let's explore how this tool can benefit Python developers and enhance their Docker workflows.πŸš€

The docker init command is a Docker CLI (Command Line Interface) command introduced in Docker Desktop version 4.18. It facilitates the creation of Docker assets for projects, making it easier to create Docker images and containers.

When you run the docker init command in your project directory, it guides you through the process of generating the necessary files with sensible defaults. These files typically include:

  • .dockerignore: This file specifies patterns of files and directories to exclude when building Docker images. It helps optimize the build process by excluding unnecessary files.

  • Dockerfile: This file contains instructions to build a Docker image. It specifies the base image, the application's dependencies, any necessary configurations, and the commands to run when the container starts.

  • docker-compose.yaml: This file defines a multi-container application using Docker Compose. It allows you to specify the services, networking, volumes, and other configurations required to run your application.

The docker init command also provides prompts to choose the application platform your project uses (such as Python, Go, Node, Rust, or Other) and the relative directory of your main package. These choices help the command generate the appropriate Docker assets specific to your project's requirements.

By automating the creation of Docker files, the docker init command simplifies the Docker setup process and ensures that best practices are followed. It saves time and effort, especially for developers who want to quickly create and manage Docker assets without manually configuring everything from scratch.

Top 5 Benefits of docker init

1. Simplified Docker asset creation

The docker init command streamlines the creation of necessary Docker files, reducing the chances of errors and ensuring that best practices are followed. It provides a guided process to generate the files with sensible defaults, saving you from manually configuring them.

2. Time and effort savings

With the default settings and guided prompts of docker init, developers can quickly create Docker assets without requiring extensive knowledge of Docker or its syntax. It eliminates the need for manual setup, allowing you to focus more on developing your application.

3. Improved project organization

The generated files by docker init provide a standardized and organized structure for your project. This organization makes it easier for developers to understand and maintain the project over time. It ensures consistency across projects by following recommended file structures and practices.

4. Enhanced portability

By using Docker assets created with docker init, your projects become more portable across different environments. Docker allows you to package your application and its dependencies into a container, ensuring that it runs consistently regardless of the underlying host system. This portability simplifies deployment and helps in transitioning your project from development to production environments.

5. Increased collaboration and reproducibility

Docker assets generated by docker init can be shared with team members, enabling collaboration and reproducibility. With the same set of Docker files, anyone can build and run the application in a consistent and reproducible manner, reducing issues related to differences in development environments.

In nutshell, the docker init command simplifies the process of creating Docker assets, saves time and effort, improves project organization, enhances portability, and facilitates collaboration among developers. It is particularly beneficial for those who want to quickly set up Docker for their projects without having to manually configure everything from scratch.

Getting Started

Prerequisite:

  • Download and install Docker Desktop 4.18 and above

Step 1. Clone the repository

To showcase the capabilities of the docker init command-line interface, we will select a Git repository. It includes a simple Python code that defines a handler that responds to GET requests with the specified text and starts an HTTP server listening on port 8080. When you run the script, you can access the server at http://localhost:8080 and see the same message as the Python program.

git clone https://github.com/dockersamples/helloworld-demo-python 
Enter fullscreen mode Exit fullscreen mode

Step 2. Initialize the Docker Assets

Change directory to hello-world-go-demo and run the docker init command

docker init
Enter fullscreen mode Exit fullscreen mode

This utility will walk you through creating the following files with sensible defaults for your project:

  • .dockerignore
  • Dockerfile
  • docker-compose.yaml

Results:

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml

Let's get started!

WARNING: The following Docker files already exist in this directory:
  - .dockerignore
  - Dockerfile

? Do you want to overwrite them? Yes
? What application platform does your project use?  [Use arrows to move, type to filter]
> Python - (detected) suitable for a Python server application
  Go - suitable for a Go server application
  Node - suitable for a Node server application
  Rust - suitable for a Rust server application
  Other - general purpose starting point for containerizing your application
  Don't see something you need? Let us know!
  Quit
Enter fullscreen mode Exit fullscreen mode

The docker init command also allows you to choose the application platform that your project uses and the relative directory of your main package.

Choose Python from the list. Choose the default 3.11.3 version.

? What version of Python do you want to use? 3.11.3
Enter fullscreen mode Exit fullscreen mode

Choose the default command to run your app at this point of time.

? What port do you want your app to listen on? 8080
Enter fullscreen mode Exit fullscreen mode
? What is the command to run your app (e.g., gunicorn 'myapp.example:app' --bind=0.0.0.0:8080)? python3 ./app.py

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml

βœ” Your Docker files are ready!

Take a moment to review them and tailor them to your application.

WARNING: No requirements.txt file found. Be sure to create one that contains the dependencies for your application, including an entry for the gunicorn package, before running your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:8000
ajeetsraina@Ajeets-MacBook-Pro python %
Enter fullscreen mode Exit fullscreen mode

The file structure look like this

tree
.
β”œβ”€β”€ Dockerfile
└── compose.yaml

1 directory, 2 files
Enter fullscreen mode Exit fullscreen mode

Here’s the content of Dockerfile it creates:

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

ARG PYTHON_VERSION=3.11.3
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8080

# Run the application.
CMD python3 ./app.py
Enter fullscreen mode Exit fullscreen mode

Here’s the default compose file it creates:

services:
  server:
    build:
      context: .
    ports:
      - 8080:8080
Enter fullscreen mode Exit fullscreen mode

Step 3. Running the container service

docker compose up -d --build
Enter fullscreen mode Exit fullscreen mode

Step 4. Accessing the Python app

curl localhost:8080

         ##         .
   ## ## ##        ==
## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
{                       /  ===-
\______ O           __/
\    \         __/
 \____\_______/

Hello from Docker!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)