Skip to content

Commit 410632b

Browse files
committedNov 17, 2023
Starting app with docker and bazel
1 parent ccae461 commit 410632b

11 files changed

+180
-0
lines changed
 

‎.devcontainer/Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM mcr.microsoft.com/devcontainers/python:1-3.11-bullseye
2+
3+
ENV PYTHONUNBUFFERED 1
4+
ENV PYTHONIOENCODING UTF-8
5+
6+
# Install Bazel
7+
RUN apt-get update && apt-get install -y curl gnupg && \
8+
curl https://bazel.build/bazel-release.pub.gpg | apt-key add - && \
9+
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list && \
10+
apt-get update && apt-get install -y bazel && \
11+
apt-get clean && rm -rf /var/lib/apt/lists/*
12+
13+
# [Optional] If your requirements rarely change, uncomment this section to add them to the image.
14+
# COPY requirements.txt /tmp/pip-tmp/
15+
# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
16+
# && rm -rf /tmp/pip-tmp
17+
18+
# [Optional] Uncomment this section to install additional OS packages.
19+
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
20+
# && apt-get -y install --no-install-recommends <your-package-list-here>

‎.devcontainer/devcontainer.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/postgres
3+
{
4+
"name": "Python 3 & PostgreSQL",
5+
"dockerComposeFile": "docker-compose.yml",
6+
"service": "app",
7+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
8+
"features": {
9+
"ghcr.io/devcontainers/features/aws-cli:1": {},
10+
"ghcr.io/devcontainers-contrib/features/node-asdf:0": {},
11+
"ghcr.io/dhoeric/features/oras:1": {}
12+
},
13+
14+
// Features to add to the dev container. More info: https://containers.dev/features.
15+
// "features": {},
16+
17+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
18+
// This can be used to network with other containers or the host.
19+
"forwardPorts": [5000],
20+
21+
// Use 'postCreateCommand' to run commands after the container is created.
22+
"postCreateCommand": "bazel build //:app"
23+
24+
// Configure tool-specific properties.
25+
// "customizations": {}
26+
27+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
28+
// "remoteUser": "root"
29+
}

‎.devcontainer/docker-compose.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
9+
volumes:
10+
- ../..:/workspaces:cached
11+
12+
# Overrides default command so things don't shut down after the process ends.
13+
command: sleep infinity
14+
15+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
16+
network_mode: service:db
17+
18+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
19+
# (Adding the "ports" property to this file will not forward from a Codespace.)
20+
21+
db:
22+
image: postgres:latest
23+
restart: unless-stopped
24+
volumes:
25+
- postgres-data:/var/lib/postgresql/data
26+
environment:
27+
POSTGRES_USER: postgres
28+
POSTGRES_DB: postgres
29+
POSTGRES_PASSWORD: postgres
30+
31+
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
32+
# (Adding the "ports" property to this file will not forward from a Codespace.)
33+
34+
volumes:
35+
postgres-data:

‎.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,17 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
#.idea/
161+
162+
163+
# Bazel build directories
164+
bazel-out/
165+
bazel-bin/
166+
bazel-genfiles/
167+
bazel-testlogs/
168+
bazel-*/
169+
170+
# Bazel workspace directory
171+
/bazel-*
172+
173+
# Bazel symlinks
174+
/bazel

‎.vscode/extensions.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"ms-vscode.makefile-tools"
4+
]
5+
}

‎.vscode/extenstions.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"ms-python.python"
4+
]
5+
}

‎BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
load("@pip_deps//:requirements.bzl", "requirement")
3+
4+
py_binary(
5+
name = "app",
6+
srcs = ["app.py"],
7+
deps = [
8+
requirement("Flask"),
9+
],
10+
)

‎Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.PHONY: build run clean
2+
3+
build:
4+
bazel build //...
5+
6+
run:
7+
bazel run //...
8+
9+
clean:
10+
bazel clean

‎WORKSPACE

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
workspace(name="postgres")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "rules_python",
7+
sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
8+
strip_prefix = "rules_python-0.26.0",
9+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.26.0/rules_python-0.26.0.tar.gz",
10+
)
11+
12+
load("@rules_python//python:repositories.bzl", "py_repositories")
13+
14+
py_repositories()
15+
16+
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
17+
18+
python_register_toolchains(
19+
name = "python_3_11",
20+
# Available versions are listed in @rules_python//python:versions.bzl.
21+
# We recommend using the same version your team is already standardized on.
22+
python_version = "3.11",
23+
)
24+
25+
load("@python_3_11//:defs.bzl", "interpreter")
26+
27+
load("@rules_python//python:pip.bzl", "pip_parse")
28+
29+
pip_parse(
30+
name = "pip_deps",
31+
requirements_lock = "//:requirements_lock.txt",
32+
)
33+
34+
load("@pip_deps//:requirements.bzl", "install_deps")
35+
36+
install_deps()

‎app.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Flask
2+
app = Flask(__name__)
3+
4+
@app.route('/')
5+
def hello_world():
6+
return 'Hello, World!'
7+
8+
if __name__ == '__main__':
9+
app.run(host='0.0.0.0', port=5000)

‎requirements_lock.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Flask==3.0.0
2+
Jinja2==3.1.2
3+
Werkzeug==3.0.1
4+
click==8.1.7
5+
blinker==1.4
6+
itsdangerous==2.1.2
7+
markupsafe==2.1.3

0 commit comments

Comments
 (0)