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.
Syntax:
docker init [OPTIONS]
Example: git init
command
- Let's create a project folder.
% mkdir myPythonProject % cd myPythonProject
- 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
- 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.
- Finally it also deploys the application at http://localhost:80

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
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Docker,
- How to docker remove a container when it exits
- Install Python on Alpine Linux - Docker
- How to Rename Docker Image with none TAG and REPOSITORY?
- Remove all stopped containers in Docker using prune command
- [Fix] Docker Error response from daemon: manifest for :latest not found: manifest unknown
- [fix] Error response from daemon: conflict unable to remove repository reference ubuntu container is using its referenced image
- [Docker] Install Python3 on Alpine Linux
- Unable to find image docker latest locally
- How to Stop/Cancel/kill docker image pull
- How to know the Docker Engine Version
- [fix] Docker: OCI runtime exec failed unable to start container process
- [Solution] Alpine Docker apt-get: not found
- Install and Run Cassandra on Docker Desktop
- Install node on Alpine Linux Docker
- How to Copy files from Docker Container to Host System
- Install the minimal Linux on Docker (only 5 mb Alpine Linux)
- How to stop and start a docker container
- Docker - Error response from daemon: You cannot remove a running container
- Open Docker Desktop from macOS Terminal
- How to check installed docker version command
- How to know the Docker Sandbox ID of a Container Network?
- [fix] docker: Error response from daemon: dial unix docker.raw.sock: connect: no such file or directory.
- [Docker] Install Git on Alpine Linux
- Docker MySQL Compose File with Volume Example
- [fix] Docker: Alpine Linux - /bin/sh: bash: not found
More Posts:
- Java - Check if array contains the value - Java
- Fix: Invalid Gradle JDK configuration found. Could not find the required JavaSDK - Gradle
- Drag drop files here option missing for SharePoint document library - SharePoint
- Android Activity Main xml stuck loading - Android-Studio
- Find your macOS version - MacOS
- Force convert HTML text input to upper case - Html
- Understanding AWS Calculator: DynamoDB on-demand capacity - AWS
- List of jar files for Jax-ws (SOAP) based Java Web Services - Java