0
0
Dockerdevops~5 mins

Development container patterns in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Development container patterns help you create isolated environments for coding. They solve the problem of different tools or libraries conflicting on your computer by packaging everything your app needs inside a container.
When you want to ensure your app runs the same way on any computer without setup issues
When you need to share your development environment with teammates easily
When you want to test your app with different versions of tools without changing your main system
When you want to keep your computer clean from many installed dependencies
When you want to quickly start coding without spending time installing software
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . ./

CMD ["python", "app.py"]

This Dockerfile creates a development container for a Python app.

FROM python:3.11-slim sets the base image with Python 3.11.

WORKDIR /app sets the working directory inside the container.

COPY requirements.txt ./ copies the dependencies list.

RUN pip install installs the dependencies.

COPY . ./ copies your app code.

CMD runs the app when the container starts.

Commands
This command builds the Docker image named 'my-python-dev' using the Dockerfile in the current folder. It packages your app and its dependencies.
Terminal
docker build -t my-python-dev .
Expected OutputExpected
Sending build context to Docker daemon 4.096kB Step 1/6 : FROM python:3.11-slim ---> 123abc456def Step 2/6 : WORKDIR /app ---> Using cache ---> 789ghi012jkl Step 3/6 : COPY requirements.txt ./ ---> Using cache ---> mno345pqr678 Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in abc123def456 Collecting flask Downloading Flask-2.3.2-py3-none-any.whl (96 kB) Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container abc123def456 ---> stu901vwx234 Step 5/6 : COPY . ./ ---> 567yzab890cd Step 6/6 : CMD ["python", "app.py"] ---> Running in efg567hij890 Removing intermediate container efg567hij890 ---> klm123nop456 Successfully built klm123nop456 Successfully tagged my-python-dev:latest
-t - Names and tags the image for easy reference
This command runs the container interactively, removes it after exit, mounts your current folder inside the container for live code changes, and maps port 5000 so you can access the app from your browser.
Terminal
docker run -it --rm -v $(pwd):/app -p 5000:5000 my-python-dev
Expected OutputExpected
* Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
-it - Runs container interactively with a terminal
--rm - Removes container after it stops to keep system clean
-v - Mounts current directory inside container for live code editing
-p - Maps container port to host port for access
This command lists running containers so you can check if your development container is active.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES klm123nop456 my-python-dev "python app.py" 10 seconds ago Up 9 seconds 0.0.0.0:5000->5000/tcp loving_borg
This command stops the running container named 'loving_borg' to end your development session cleanly.
Terminal
docker stop loving_borg
Expected OutputExpected
loving_borg
Key Concept

If you remember nothing else from this pattern, remember: mounting your code folder into the container lets you edit code live without rebuilding the image.

Common Mistakes
Not mounting the code folder with -v, so code changes inside the container are lost after rebuild
Without mounting, the container uses the code copied at build time, so live edits on your computer don't affect the running app
Always use -v $(pwd):/app to mount your current folder inside the container for live development
Not exposing or mapping the app port with -p, so you cannot access the app from your browser
The container runs the app internally, but without port mapping, your computer cannot reach it
Use -p 5000:5000 (or your app port) to map container port to your host
Running the container without --rm, leaving stopped containers cluttering your system
Stopped containers take disk space and make management harder
Use --rm to automatically remove the container after you stop it
Summary
Build a Docker image with your app and dependencies using 'docker build'.
Run the container with code folder mounted and ports mapped for live development.
Use 'docker ps' to check running containers and 'docker stop' to end sessions cleanly.