0
0
Dockerdevops~5 mins

Consistent environments across teams in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Different team members often have different software versions and settings on their computers. This causes apps to work on one machine but fail on another. Docker solves this by packaging the app and its environment together, so everyone runs the same setup.
When developers want to avoid the 'it works on my machine' problem.
When you want to share your app with testers who have different computers.
When you need to deploy the app to servers that might have different software installed.
When you want to quickly onboard new team members with the exact environment.
When you want to run multiple apps on the same machine without conflicts.
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 small Python 3.11 environment.

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

WORKDIR /app sets the working folder inside the container.

COPY requirements.txt ./ copies the list of Python packages.

RUN pip install installs the packages inside the container.

COPY . . copies your app code into the container.

CMD runs your app when the container starts.

Commands
This command builds a Docker image named 'my-app' with tag '1.0' using the Dockerfile in the current folder. It packages your app and its environment together.
Terminal
docker build -t my-app:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 5.12kB Step 1/6 : FROM python:3.11-slim ---> 123abc456def Step 2/6 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/6 : COPY requirements.txt ./ ---> Using cache ---> 345ghi678jkl 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 ---> 901mno234pqr Step 5/6 : COPY . . ---> Using cache ---> stu567vwx890 Step 6/6 : CMD ["python", "app.py"] ---> Running in xyz123abc456 Removing intermediate container xyz123abc456 ---> 789stu012vwx Successfully built 789stu012vwx Successfully tagged my-app:1.0
-t - Assigns a name and tag to the image for easy reference
This command runs the 'my-app:1.0' image in a container, removes it after stopping, and maps port 5000 inside the container to port 5000 on your computer so you can access the app.
Terminal
docker run --rm -p 5000:5000 my-app:1.0
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)
--rm - Automatically removes the container when it stops
-p - Maps a port from the container to the host machine
This command lists all running containers so you can check if your app container is running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 789stu012vwx my-app:1.0 "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 end the app.
Terminal
docker stop loving_morse
Expected OutputExpected
loving_morse
Key Concept

If you remember nothing else from this pattern, remember: Docker packages your app and its environment together so everyone runs the exact same setup.

Common Mistakes
Not copying all necessary files into the Docker image.
The app will fail inside the container because required files or code are missing.
Use COPY commands in the Dockerfile to include all needed files and folders.
Forgetting to map ports when running the container.
You cannot access the app from your computer because the container's ports are isolated.
Use the -p flag to map container ports to your computer's ports.
Running the app without building the image first.
Docker will not find the image and the run command will fail.
Always run docker build before docker run to create the image.
Summary
Create a Dockerfile to define your app environment and dependencies.
Build the Docker image with docker build to package your app and environment.
Run the container with docker run, mapping ports to access the app.
Use docker ps to check running containers and docker stop to end them.