Skip to content

Commit f016bac

Browse files
update devcontainer configuration using docker-compose
1 parent 1521132 commit f016bac

File tree

4 files changed

+65
-57
lines changed

4 files changed

+65
-57
lines changed

.devcontainer/Dockerfile

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,28 @@
1-
# Use an official Ubuntu as a parent image
2-
FROM ubuntu:22.04
1+
FROM mcr.microsoft.com/devcontainers/miniconda:3
32

43
# Set environment variables for Conda
54
ENV PATH=/opt/conda/bin:$PATH
6-
ENV MYSQL_ROOT_PASSWORD=dj_secret123
75
ENV DJ_HOST=localhost
8-
ENV DJ_PASS=$MYSQL_ROOT_PASSWORD
9-
ENV DJ_USER=root
6+
ENV DJ_PASS=$DJ_PASS
7+
ENV DJ_USER=$DJ_USER
108

11-
# Install necessary system tools
9+
# Install system dependencies including netcat for connection checking
1210
RUN apt-get update && apt-get install -y \
1311
wget \
1412
vim \
15-
bzip2 \
16-
ca-certificates \
1713
curl \
1814
git \
19-
webp \
20-
mysql-server \
15+
netcat-openbsd \
16+
default-mysql-client \
2117
&& rm -rf /var/lib/apt/lists/*
2218

23-
# Download and install Miniconda
24-
RUN ARCH=$(uname -m) && \
25-
if [ "$ARCH" = "x86_64" ]; then \
26-
MINICONDA_URL="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh"; \
27-
elif [ "$ARCH" = "aarch64" ]; then \
28-
MINICONDA_URL="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh"; \
29-
else \
30-
echo "Unsupported architecture: $ARCH" && exit 1; \
31-
fi && \
32-
wget --quiet $MINICONDA_URL -O /tmp/miniconda.sh && \
33-
/bin/bash /tmp/miniconda.sh -b -p /opt/conda && \
34-
rm /tmp/miniconda.sh && \
35-
conda config --set auto_update_conda false && \
36-
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main && \
37-
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r && \
38-
conda clean --all -f -y && \
39-
conda install -n base libarchive -c main --force-reinstall --solver classic
40-
4119
# Install additional tools or packages as needed in the base environment
4220
RUN conda init bash && \
43-
conda install -n base -c conda-forge python=3.11 python-graphviz jupyterlab 'nodejs>20,<21' pip -y && \
44-
pip install ipython-sql && \
21+
conda install -n base -c conda-forge python-graphviz jupyter jupysql 'nodejs>20,<21' && \
4522
pip install mystmd && \
4623
pip install jupyterlab_myst && \
47-
pip install git+https://github.com/datajoint/datajoint-python.git && \
48-
pip install Faker
49-
50-
# Set up shell to use Conda by default
51-
RUN echo "source /opt/conda/etc/profile.d/conda.sh && conda activate base" >> ~/.bashrc
24+
pip install git+https://github.com/datajoint/datajoint-python.git
5225

53-
# Configure MySQL to allow remote connections (optional)
54-
RUN sed -i 's/bind-address\s*=.*/bind-address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf
55-
56-
# Initialize MySQL with the root password
57-
RUN service mysql start && \
58-
mysqladmin -u root password "$MYSQL_ROOT_PASSWORD" && \
59-
mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '$MYSQL_ROOT_PASSWORD'; FLUSH PRIVILEGES;"
6026

6127
# Expose ports for JupyterLab and MySQL
62-
EXPOSE 3306
63-
64-
# Default command for the Devcontainer
65-
CMD ["bash"]
28+
EXPOSE 8888

.devcontainer/devcontainer.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
{
22
"name": "DataJoint Book Devcontainer",
3-
"build": {
4-
"dockerfile": "Dockerfile",
5-
"context": ".."
6-
},
3+
"dockerComposeFile": "docker-compose.yml",
4+
"service": "app",
5+
"workspaceFolder": "/workspace",
6+
"forwardPorts": [8888],
7+
"postStartCommand": "until nc -z $MYSQL_HOST $MYSQL_PORT; do echo 'Waiting for MySQL...'; sleep 2; done && jupyter notebook --ip 0.0.0.0 --no-browser --allow-root --port 8888 &",
78
"customizations": {
89
"vscode": {
910
"settings": {
10-
"terminal.integrated.shell.linux": "/bin/bash"
11+
"terminal.integrated.shell.linux": "/bin/bash",
1112
"python.defaultInterpreterPath": "/opt/conda/bin/python",
12-
"python.terminal.activateEnvironment": false // since conda is auto-activated
13+
"python.terminal.activateEnvironment": false
1314
},
1415
"extensions": [
1516
"ms-python.python",
1617
"ms-toolsai.jupyter",
17-
"ms-azuretools.vscode-docker"
18-
"ExecutableBookProject.myst-highlight", // MyST syntax highlighting
19-
"ms-python.black-formatter", // Python formatting
20-
"ms-vscode.vscode-json" // JSON support
18+
"ms-azuretools.vscode-docker",
19+
"ExecutableBookProject.myst-highlight",
20+
"ms-python.black-formatter",
21+
"ms-vscode.vscode-json"
2122
]
2223
}
2324
},
24-
"postCreateCommand": "service mysql start"
25+
"remoteUser": "root"
2526
}
2627

.devcontainer/docker-compose.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
services:
2+
app:
3+
build:
4+
context: ..
5+
dockerfile: .devcontainer/Dockerfile
6+
volumes:
7+
- ..:/workspace:cached
8+
command: sleep infinity
9+
depends_on:
10+
db:
11+
condition: service_healthy
12+
environment:
13+
DJ_HOST: db
14+
DJ_USER: dev
15+
DJ_PASS: devpass
16+
DJ_PORT: 3306
17+
working_dir: /workspace
18+
networks:
19+
- devcontainer
20+
db:
21+
image: mysql:8.0
22+
restart: unless-stopped
23+
environment:
24+
MYSQL_ROOT_PASSWORD: rootpass
25+
MYSQL_USER: dev
26+
MYSQL_PASSWORD: devpass
27+
volumes:
28+
- ./init-dev-privileges.sql:/docker-entrypoint-initdb.d/init-dev-privileges.sql:ro
29+
healthcheck:
30+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
31+
timeout: 20s
32+
retries: 10
33+
interval: 5s
34+
start_period: 30s
35+
networks:
36+
- devcontainer
37+
38+
networks:
39+
devcontainer:
40+
driver: bridge
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Ensure dev user exists and has broad privileges
2+
CREATE USER IF NOT EXISTS 'dev'@'%' IDENTIFIED BY 'devpass';
3+
GRANT ALL PRIVILEGES ON *.* TO 'dev'@'%' WITH GRANT OPTION;
4+
FLUSH PRIVILEGES;

0 commit comments

Comments
 (0)