Docker Init Command to Create Starter Files for Your Project


As a developer, we are all aware of the git init command, which quickly turns your project folder into a version-controlled repository irrespective of the programming language or the type of file you are using.

Similarly, In Docker version 4.18 or above you can make use of the docker init command to create Docker-related starter files for your project.


    Note: docker init is currently in beta and is not recommended for use in the production environments.


Syntax:

docker init [OPTIONS]

Example: git init command

  1. Let's create a project folder.
    % mkdir myPythonProject
    
    % cd myPythonProject 
  2. Now let's run the git init command inside it.
    % docker init
    
    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!
    
    ? What application platform does your project use?  [Use arrows to move, type to filter]
      Go - suitable for a Go server application
    >  Python - suitable for a Python server application
      Node - suitable for a Node server application
      Rust - suitable for a Rust server application
      ASP.NET - suitable for an ASP.NET application
      Other - general purpose starting point for containerizing your application
      Don't see something you need? Let us know!
      Quit

    Let's choose Python.

    ? What application platform does your project use? Python
    ? What version of Python do you want to use? 3.11
    ? What port do you want your app to listen on? 80
    ? What is the command to run your app (e.g., gunicorn 'myapp.example:app' 
         --bind=0.0.0.0:80)? hello 'demo.code2care:app' --bind=0.0.0:80
    
    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 before running it.
    
    When you're ready, start your application by running: docker compose up --build
    
    Your application will be available at http://localhost:80
  3. As you can see, the docker init command creates dockerignore, Dockerfile and compose.yaml as well as the install all dependencies based on rerequirements.txt file in this case.
  4. Finally it also deploys the application at http://localhost:80
Docker Init Command Demo

Created Dockerfile

# 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
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 80

# Run the application.
CMD hello 'demo.code2care:app' --bind=0.0.0:80

Created compose.yaml

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

# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
  server:
    build:
      context: .
    ports:
      - 80:80

# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker compose up`.
#     depends_on:
#       db:
#         condition: service_healthy
#   db:
#     image: postgres
#     restart: always
#     user: postgres
#     secrets:
#       - db-password
#     volumes:
#       - db-data:/var/lib/postgresql/data
#     environment:
#       - POSTGRES_DB=example
#       - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
#     expose:
#       - 5432
#     healthcheck:
#       test: [ "CMD", "pg_isready" ]
#       interval: 10s
#       timeout: 5s
#       retries: 5
# volumes:
#   db-data:
# secrets:
#   db-password:
#     file: db/password.txt

This is not an AI-generated article but is demonstrated by a human on Docker Desktop 4.23 on M1 Mac running macOS Sonoma 14.0.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap