SlideShare a Scribd company logo
How to create a
Devcontainer for your
Python project 🐳
How to create a Devcontainer for your Python project
How to create a Devcontainer for your Python project
The use case
Youareassignedtosetupanewrepoforateam.Therequirementsareasfollows:
Soweneedtoalignon:
📌 AspecificJavaversion
📌 AspecificPythonversion
📌 Aspecific pyspark version
→otherwisewedonotenjoytheguaranteeswewantinproductioncode
How to create a Devcontainer for your Python project
Devcontainers to the rescue ⛑!
Dockerhelpsuscreateaformaldefinitionofourenvironment.Devcontainersallowyouto
connectyoureditor(IDE)tothatcontainer.
📝 NotethatthisdoesmeanrunningDockerimagesonyourlaptop(performance
requirement).
Devcontainerscanhelpus:
🔄 Reproducibledevelopmentenvironment
⚡️ Fasterprojectsetup→fasteronboarding
👨‍👩‍👧‍👦 Betteralignmentbetweenteammembers
⏱ Forcedtokeepyourdevenvironmentup-to-date&reproducible
→savesyourteamtimegoingintoproductionlater
👷🏻‍♂️Let's build a Devcontainer!
Let’ssaywehaveareallysimpleprojectthatlookslikethis:
$ tree .
.
├── README.md
├── requirements.txt
├── requirements-dev.txt
├── sales_analysis.py
└── test_sales_analysis.py
The .devcontainer folder
YourDevcontainerspecwillliveinsidethe .devcontainer folder.
Therewillbetwomainfiles:
devcontainer.json
Dockerfile
Createanewfilecalled devcontainer.json :
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
}
}
Sohowdoesthis Dockerfile looklike?
FROM python:3.10
# Install Java
RUN apt update && 
apt install -y sudo && 
sudo apt install default-jdk -y
## Pip dependencies
# Upgrade pip
RUN pip install --upgrade pip
# Install production dependencies
COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt && 
rm /tmp/requirements.txt
# Install development dependencies
COPY requirements-dev.txt /tmp/requirements-dev.txt
RUN pip install -r /tmp/requirements-dev.txt && 
rm /tmp/requirements-dev.txt
Opening the Devcontainer
The .devcontainer folderinplace,nowit’stimetoopenourDevcontainer.
Openupthecommandpallete( CMD + Shift + P )andselect“DevContainers:Reopen
inContainer”:
Uponopeningarepowithavalid .devcontainer folder,youarealreadynotified:
YourVSCodeisnowconnectedtotheDockercontainer🙌🏻:
What is happening under the hood 🚗
BesidesstartingtheDockerimageandattachingtheterminaltoit,VSCodeisdoinga
couplemorethings:
1. isbeinginstalledonyourDevcontainer.
VSCodeServerisinstalledasaserviceinthecontaineritselfsoyourVSCode
installationcancommunicatewiththecontainer.
Forexample,installandrunextensions.
VSCodeServer
2.Configiscopiedover.
Configlike ~/.gitconfig and ~/.ssh/known_hosts arecopiedovertotheir
respectivelocationsinthecontainer.
3.Filesystemmounts.
VSCodeautomaticallytakescareofmounting:
ThefolderyouarerunningtheDevcontainerfrom.
YourVSCodeworkspacefolder.
Opening your Devcontainer with the click of a button
YourentireprojectsetupisnowencapsulatedintheDevcontainer.Soactuallywecanadd
aMarkdownbuttontoopenuptheDevcontainer:
JustmodifytheGitHubURLafter url= ✓.
[
![Open in Remote - Containers](
https://img.shields.io/static/v1?label=Remote%20-
%20Containers&message=Open&color=blue&logo=visualstudiocode
)
](
https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?
url=https://github.com/godatadriven/python-devcontainer-template
)
Thisrendersthefollowingbutton:
Remote - Containers
Remote - Containers Open
Open
WhatkindofREADMEwouldyouratherlike?
Manualinstallation UsingaDevcontainer🙌🏻
How to create a Devcontainer for your Python project
Extending the Devcontainer
WehavebuiltaworkingDevcontainer,thatisgreat!Butacouplethingsarestillmissing.
Installanon-rootuserforextrasafetyandgood-practice
PassincustomVSCodesettingsandinstallextensionsbydefault
BeabletoaccessSparkUI(openingupport4040)
RunContinuousIntegration(CI)intheDevcontainer
Let'sseehow.
Installing a non-root user
Ifyou pip install anewpackage,youwillseethefollowingmessage:
Solet'sgoaheadandcreateauserforthisscenario.
# Add non-root user
ARG USERNAME=nonroot
RUN groupadd --gid 1000 $USERNAME && 
useradd --uid 1000 --gid 1000 -m $USERNAME
## Make sure to reflect new user in PATH
ENV PATH="/home/${USERNAME}/.local/bin:${PATH}"
USER $USERNAME
Addthefollowingpropertyto devcontainer.json :
"remoteUser": "nonroot"
That'sgreat!Whenwenowstartthecontainerweshouldconnectastheuser nonroot .
Passing custom VSCode settings
"customizations": {
"vscode": {
"extensions": [
"ms-python.python"
],
"settings": {
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black",
"python.linting.mypyEnabled": true,
"python.linting.enabled": true
}
}
}
Accessing Spark UI
Sinceweareusingpyspark,itwouldbenicetobeabletoaccessSparkUI.
"portsAttributes": {
"4040": {
"label": "SparkUI",
"onAutoForward": "notify"
}
},
"forwardPorts": [
4040
]
Whenwenowrunourcode,wegetanotificationwecanopenSparkUIinthebrowser:
ResultingintheSparkUIlikeweknowit:
✨
Running our CI in the Devcontainer
Therearetwobasicoptions:
1.BuildtheDockerimagewithintheCI/CDpipeline
2.Prebuildingtheimage
Let'sseeaboutoptionnumber(1).
1. Build the Docker image within the CI/CD pipeline
Luckily,aGitHubActionwasalreadysetupforustodoexactlythis:
devcontainers/ci
Tonowbuild,pushandrunacommandintheDevcontainerisaseasyas:
name: Python app
on:
...
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout (GitHub)
uses: actions/checkout@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and run dev container task
uses: devcontainers/ci@v0.2
with:
imageName: ghcr.io/${{ github.repository }}/devcontainer
runCmd: pytest .
SeebelowatraceoftheexecutedGitHubAction:
Awesome!
The final Devcontainer definition
WebuiltthefollowingDevcontainerdefinitions.
First, devcontainer.json :
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"remoteUser": "nonroot",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python"
],
"settings": {
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black",
"python.linting.mypyEnabled": true,
"python.linting.enabled": true
}
}
},
...
}
"portsAttributes": {
"4040": {
"label": "SparkUI",
"onAutoForward": "notify"
}
},
"forwardPorts": [
4040
]
Andour Dockerfile :
FROM python:3.10
# Install Java
RUN apt update && 
apt install -y sudo && 
sudo apt install default-jdk -y
# Add non-root user
ARG USERNAME=nonroot
RUN groupadd --gid 1000 $USERNAME && 
useradd --uid 1000 --gid 1000 -m $USERNAME
## Make sure to reflect new user in PATH
ENV PATH="/home/${USERNAME}/.local/bin:${PATH}"
USER $USERNAME
## Pip dependencies
# Upgrade pip
RUN pip install --upgrade pip
# Install production dependencies
COPY --chown=nonroot:1000 requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt && 
rm /tmp/requirements.txt
# Install development dependencies
COPY --chown=nonroot:1000 requirements-dev.txt /tmp/requirements-
dev.txt
RUN pip install -r /tmp/requirements-dev.txt && 
rm /tmp/requirements-dev.txt
Three environments 🎁
Theaboveideasetsusupforhaving3differentimagesforourentirelifecycle.Onefor
Development,oneforCI,andfinallyoneforproduction.
Going further 🔮
Devcontainerfeatures
Devcontainertemplates
💡 Protip:mountyourAWS/GCP/Azurecredentials
...andmuchmore:
Mountingdirectories
Awesome resources
.RunyourCIinyourDevcontainers.Builtonthe .
.TheofficialDevcontainerspecification.
.Acollectionofready-to-useDevcontainerimages.
.Moreexplanations&instructionsforaddinga
non-rootusertoyour Dockerfile and devcontainer.json .
.Arepopointingtoyetevenmoreawesomeresources.
devcontainers/ci DevcontainerCLI
https://containers.dev/
devcontainers/images
Addanon-rootusertoacontainer
Pre-buildingdevcontainerimages
awesome-devcontainers
Concluding
Devcontainershaveprovenusefulto:
🔄 Reproducibledevelopmentenvironment
⚡️ Fasterprojectsetup→fasteronboarding
👨‍👩‍👧‍👦 Betteralignmentbetweenteammembers
⏱ Forcedtokeepyourdevenvironmentup-to-date&reproducible
→savesyourteamtimegoingintoproductionlater
NowonlyVSCode,but takingshape.
openspecification
Thanks! 🙌🏻
Repo:godatadriven/python-devcontainer-template

More Related Content

PDF
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
ODP
Aug penguin16
 
PPTX
No more Dockerfiles? Buildpacks to help you ship your image!
PDF
Docker for data science
PPTX
Patterns & Antipatterns in Docker Image Lifecycle
 
PDF
Deploy Deep Learning Application with Azure Container Instance - Devdays2018
PDF
Continuous Delivery w projekcie Open Source - Marcin Stachniuk - DevCrowd 2017
PDF
DevOps MeetUp NL - Docker (Oct 2014)
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Aug penguin16
 
No more Dockerfiles? Buildpacks to help you ship your image!
Docker for data science
Patterns & Antipatterns in Docker Image Lifecycle
 
Deploy Deep Learning Application with Azure Container Instance - Devdays2018
Continuous Delivery w projekcie Open Source - Marcin Stachniuk - DevCrowd 2017
DevOps MeetUp NL - Docker (Oct 2014)

Similar to How to create a Devcontainer for your Python project (20)

PDF
Kubernetes debug like a pro
PDF
DevOps meetup 16oct docker and jenkins
PDF
CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
PDF
GDGSCL - Docker a jeho provoz v Heroku a AWS
PDF
AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
PDF
DevOps Workflow: A Tutorial on Linux Containers
PDF
Developing and deploying applications with Spring Boot and Docker (@oakjug)
PDF
Creating Scalable JVM/Java Apps on Heroku
PPTX
Getting Started with Pelican
PDF
eXoer on the grill: eXo Add-ons factory using Docker and Codenvy
PDF
DCSF 19 Building Your Development Pipeline
PDF
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
PPTX
CNCF QuĂŠbec Meetup du 16 Novembre 2023
PPTX
Docker for Fun and Profit, Devoxx 2014
ODP
DevAssistant, Docker and You
PDF
Microservices DevOps on Google Cloud Platform
PDF
Making Sense Out of Amazon EC2 Container Service
PDF
Web application on menu card qrcode generator.pdf
PDF
DevOps tools for everyone - Vagrant, Puppet and Webmin
PDF
Codetainer: a Docker-based browser code 'sandbox'
Kubernetes debug like a pro
DevOps meetup 16oct docker and jenkins
CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
GDGSCL - Docker a jeho provoz v Heroku a AWS
AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
DevOps Workflow: A Tutorial on Linux Containers
Developing and deploying applications with Spring Boot and Docker (@oakjug)
Creating Scalable JVM/Java Apps on Heroku
Getting Started with Pelican
eXoer on the grill: eXo Add-ons factory using Docker and Codenvy
DCSF 19 Building Your Development Pipeline
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
CNCF QuĂŠbec Meetup du 16 Novembre 2023
Docker for Fun and Profit, Devoxx 2014
DevAssistant, Docker and You
Microservices DevOps on Google Cloud Platform
Making Sense Out of Amazon EC2 Container Service
Web application on menu card qrcode generator.pdf
DevOps tools for everyone - Vagrant, Puppet and Webmin
Codetainer: a Docker-based browser code 'sandbox'
Ad

More from GoDataDriven (20)

PDF
Streamlining Data Science Workflows with a Feature Catalog
PDF
Visualizing Big Data in a Small Screen
PDF
Building a Scalable and reliable open source ML Platform with MLFlow
PDF
Training Taster: Leading the way to become a data-driven organization
PDF
My Path From Data Engineer to Analytics Engineer
PDF
dbt Python models - GoDataFest by Guillermo Sanchez
PDF
Workshop on Google Cloud Data Platform
PDF
Using Graph Neural Networks To Embrace The Dependency In Your Data by Usman Z...
PDF
Common Issues With Time Series by Vadim Nelidov - GoDataFest 2022
PDF
MLOps CodeBreakfast on AWS - GoDataFest 2022
PDF
MLOps CodeBreakfast on Azure - GoDataFest 2022
PDF
Tableau vs. Power BI by Juan Manuel Perafan - GoDataFest 2022
PDF
Deploying a Modern Data Stack by Lasse Benninga - GoDataFest 2022
PPTX
AWS Well-Architected Webinar Security - Ben de Haan
PDF
The 7 Habits of Effective Data Driven Companies
PPTX
DevOps for Data Science on Azure - Marcel de Vries (Xpirit) and Niels Zeilema...
PDF
Artificial intelligence in actions: delivering a new experience to Formula 1 ...
PDF
Smart application on Azure at Vattenfall - Rens Weijers & Peter van 't Hof
PDF
Democratizing AI/ML with GCP - Abishay Rao (Google) at GoDataFest 2019
PDF
The world runs on AI - Tony Krijnen (Microsoft) at GoDataFest 2019
Streamlining Data Science Workflows with a Feature Catalog
Visualizing Big Data in a Small Screen
Building a Scalable and reliable open source ML Platform with MLFlow
Training Taster: Leading the way to become a data-driven organization
My Path From Data Engineer to Analytics Engineer
dbt Python models - GoDataFest by Guillermo Sanchez
Workshop on Google Cloud Data Platform
Using Graph Neural Networks To Embrace The Dependency In Your Data by Usman Z...
Common Issues With Time Series by Vadim Nelidov - GoDataFest 2022
MLOps CodeBreakfast on AWS - GoDataFest 2022
MLOps CodeBreakfast on Azure - GoDataFest 2022
Tableau vs. Power BI by Juan Manuel Perafan - GoDataFest 2022
Deploying a Modern Data Stack by Lasse Benninga - GoDataFest 2022
AWS Well-Architected Webinar Security - Ben de Haan
The 7 Habits of Effective Data Driven Companies
DevOps for Data Science on Azure - Marcel de Vries (Xpirit) and Niels Zeilema...
Artificial intelligence in actions: delivering a new experience to Formula 1 ...
Smart application on Azure at Vattenfall - Rens Weijers & Peter van 't Hof
Democratizing AI/ML with GCP - Abishay Rao (Google) at GoDataFest 2019
The world runs on AI - Tony Krijnen (Microsoft) at GoDataFest 2019
Ad

Recently uploaded (20)

PDF
Introduction to Data Science and Data Analysis
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
Fluorescence-microscope_Botany_detailed content
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
SAP 2 completion done . PRESENTATION.pptx
Introduction to Data Science and Data Analysis
Qualitative Qantitative and Mixed Methods.pptx
climate analysis of Dhaka ,Banglades.pptx
Introduction to Knowledge Engineering Part 1
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Supervised vs unsupervised machine learning algorithms
Fluorescence-microscope_Botany_detailed content
Clinical guidelines as a resource for EBP(1).pdf
Reliability_Chapter_ presentation 1221.5784
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
[EN] Industrial Machine Downtime Prediction
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
1_Introduction to advance data techniques.pptx
ISS -ESG Data flows What is ESG and HowHow
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
SAP 2 completion done . PRESENTATION.pptx

How to create a Devcontainer for your Python project