0
0
Dockerdevops~5 mins

Why Docker improves development workflow - Why It Works

Choose your learning style9 modes available
Introduction
Developers often face problems when their apps work on their computer but not on others or on servers. Docker solves this by packaging apps with everything they need to run, making the app behave the same everywhere.
When you want to share your app with teammates and ensure it runs the same on their computers.
When you need to run multiple apps on one machine without them interfering with each other.
When you want to test your app in an environment identical to the production server.
When you want to quickly start and stop your app without installing many dependencies.
When you want to deploy your app easily to cloud servers or other machines.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir flask
CMD ["python", "app.py"]

This Dockerfile starts from a small Python image.

It sets the working folder inside the container to /app.

It copies your app files into the container.

It installs Flask, a Python web framework.

Finally, it runs your app using Python.

Commands
This command builds a Docker image named 'my-flask-app' from the Dockerfile in the current folder. It packages your app and its environment.
Terminal
docker build -t my-flask-app .
Expected OutputExpected
Sending build context to Docker daemon 4.096kB Step 1/5 : FROM python:3.11-slim ---> 123abc456def Step 2/5 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/5 : COPY . /app ---> Using cache ---> 345ghi678jkl Step 4/5 : RUN pip install --no-cache-dir flask ---> 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 ---> 901mno234pqr Step 5/5 : CMD ["python", "app.py"] ---> Running in stu567vwx890 Removing intermediate container stu567vwx890 ---> 345yz123abc Successfully built 345yz123abc Successfully tagged my-flask-app:latest
-t - Names and tags the image for easy reference
This command runs your app inside a container and maps port 5000 of your computer to port 5000 inside the container so you can access the app in your browser.
Terminal
docker run -p 5000:5000 my-flask-app
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)
-p - Maps container port to host port for access
This command lists running containers so you can check that your app container is running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 345yz123abc my-flask-app "python app.py" 10 seconds ago Up 9 seconds 0.0.0.0:5000->5000/tcp loving_morse
This command stops the running container named 'loving_morse' to free resources or update the app.
Terminal
docker stop loving_morse
Expected OutputExpected
loving_morse
Key Concept

Docker packages your app with everything it needs so it runs the same everywhere, avoiding 'it works on my machine' problems.

Common Mistakes
Not mapping ports with -p when running the container
Without port mapping, you cannot access the app from your computer's browser.
Always use -p host_port:container_port to expose the app port.
Forgetting to rebuild the image after changing app files
The container runs the old code because the image was not updated.
Run 'docker build' again to create a new image with changes.
Running containers without stopping old ones
Multiple containers may use the same ports causing conflicts.
Stop old containers with 'docker stop' before starting new ones.
Summary
Build a Docker image to package your app and its environment.
Run the image as a container with port mapping to access the app.
Use 'docker ps' to check running containers and 'docker stop' to stop them.