visit
If you do use the staged build approach, use the target attribute in your Docker Compose file.
ARG userid
ARG groupid
# User setup
RUN addgroup mygroup --gid $groupid
RUN useradd -ms /bin/bash -u $userid -g $groupid myuser
RUN echo 'myuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
docker-compose build --build-arg userid=$(id -u) --build-arg groupid=$(id -g)
There is a secrets
attribute you can use in Docker Compose, however, since this is for development we don't need to secure a production environment, I found it easier to just mount some shared files and directories. Below is an example of some "secrets" I like to share.
services:
myapp:
volumes:
- .:/opt/myapp
- $HOME/.git:/home/myuser/.git
- $HOME/.ssh:/home/myuser/.ssh
- $HOME/.netrc:/home/myuser/.netrc
- $HOME/.pypirc:/home/myuser/.pypirc
See above. If you share your .git
and .ssh
directories with your development container you can execute git commands in your container without needing to jump in and out. This makes your development workflow much more enjoyable with fewer terminals open or fewer keystrokes.
Especially if you use a separate Dockerfile and thus don't need to worry about the container size, install some extra development tools. Some apt packages I like to install: procps, nano, vim, sudo, htop, tmux, redis-tools, autojump, openssh-client, wget, unzip
Since you have a development container you can now really easy make a share development environment setup. I like to include a shared .bashrc
in my repository and then in the Dockerfile copy that to the user's home directory.
COPY .bashrc /home/myuser/
Since containers are immutable, they will lose your command history on rebuild. You can solve this by linking in a .bash_history
file in your Dockerfile.
RUN ln -s /opt/myapp/.bash_history /home/myuser/.bash_history
Below is the full setup, Dockerfile.dev
, compose.yaml
, and .gitignore
additions.
FROM ubuntu:bionic
ARG userid
ARG groupid
WORKDIR /build
# apt layer
RUN apt update
RUN apt install -y python3.8 python3.8-venv libpython3.8
# dev tools
RUN apt install -y procps nano vim sudo htop tmux redis-tools autojump openssh-client wget unzip
# pip, pdm setup
RUN python3.8 -m pip install pip --upgrade
RUN pip3.8 install --upgrade pip
RUN pip3.8 install pdm thefuck
# User setup
RUN addgroup mygroup --gid $groupid
RUN useradd -ms /bin/bash -u $userid -g $groupid myuser
RUN echo 'myuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
WORKDIR /opt/myapp
USER myuser
COPY .bashrc /home/myuser/
RUN ln -s /opt/myapp/.bash_history /home/myuser/.bash_history
version: "3.9"
services:
myapp:
network_mode: host
build:
context: .
dockerfile: Dockerfile.dev
command: /bin/bash
environment:
- DEV_CONTAINER=1
- PYTHONUNBUFFERED=1
- XDG_DATA_HOME=/opt/myapp/.local/share
volumes:
- .:/opt/myapp
- $HOME/.git:/home/myuser/.git
- $HOME/.ssh:/home/myuser/.ssh
- $HOME/.netrc:/home/myuser/.netrc
- $HOME/.pypirc:/home/myuser/.pypirc
ports:
- 8888:8888
- 8000:8000
- 8080:8080
- 8081:8081
- 3000:3000
- 5000:5000
shm_size: 4gb
depends_on:
- redis
redis:
image: 'bitnami/redis:latest'
ports:
- 6379:6379
environment:
- ALLOW_EMPTY_PASSWORD=yes
.bash_history
.local