0
0
Dockerdevops~5 mins

Building images with docker build - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building a Docker image packages your app and its environment into a single file. This lets you run your app anywhere without setup problems.
When you want to package your web app with all its software so it runs the same on any computer.
When you need to share your app with teammates or deploy it to a server easily.
When you want to test your app in a clean environment without installing anything on your machine.
When you want to update your app and create a new version to deploy.
When you want to automate app setup in continuous integration pipelines.
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"]

FROM sets the base image with Python 3.11 slim version.

WORKDIR sets the working directory inside the image.

COPY requirements.txt . copies the file with dependencies.

RUN pip install installs the dependencies.

COPY . . copies all app files into the image.

CMD defines the command to run the app.

Commands
This command builds a Docker image named 'my-app-image' with tag '1.0' using the Dockerfile in the current directory. It packages your app and its environment.
Terminal
docker build -t my-app-image:1.0 .
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 ---> 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 Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container abc123def456 ---> 901mno234pqr Step 5/6 : COPY . . ---> 567stu890vwx Step 6/6 : CMD ["python", "app.py"] ---> Running in def789ghi012 Removing intermediate container def789ghi012 ---> 345yz123abc Successfully built 345yz123abc Successfully tagged my-app-image:1.0
-t - Assigns a name and tag to the image for easy reference
Lists all Docker images on your machine so you can verify your new image was created.
Terminal
docker images
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app-image 1.0 345yz123abc 10 seconds ago 120MB
Runs a container from your newly built image to check that your app starts correctly. The --rm flag removes the container after it stops.
Terminal
docker run --rm my-app-image:1.0
Expected OutputExpected
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
--rm - Automatically removes the container after it stops to keep your system clean
Key Concept

If you remember nothing else from this pattern, remember: docker build creates a reusable image from your app and environment using a Dockerfile.

Common Mistakes
Not running the docker build command in the directory containing the Dockerfile.
Docker cannot find the Dockerfile and fails to build the image.
Always run docker build in the folder where your Dockerfile is located or specify the correct path.
Forgetting to tag the image with -t, resulting in an image with a random ID only.
It becomes hard to find and run the image later without a meaningful name.
Use the -t flag to give your image a clear name and version tag.
Not copying all necessary files into the image, causing runtime errors.
Your app may fail because required files or dependencies are missing inside the image.
Use COPY commands in the Dockerfile to include all needed files.
Summary
Write a Dockerfile to define your app environment and files.
Use docker build -t name:tag . to create an image from the Dockerfile.
Check your image with docker images and run it with docker run.