0
0
Dockerdevops~5 mins

Building images in CI pipeline in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building images in a CI pipeline automates creating container images whenever code changes. This ensures your app is always packaged and ready to run in any environment without manual steps.
When you want to automatically create a new app version image after every code update.
When you need to test your app inside a container before deploying it.
When you want to push the built image to a registry for sharing or deployment.
When you want to catch build errors early by building images in a controlled environment.
When you want to keep your deployment process consistent and repeatable.
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 starts from a small Python 3.11 image.

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

It copies the requirements file and installs Python packages.

Then it copies the app code and sets the command to run the app.

Commands
This command builds a Docker image named 'my-app-image' with tag '1.0' from the current folder where the Dockerfile is located.
Terminal
docker build -t my-app-image:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 12.3MB Step 1/5 : FROM python:3.11-slim ---> 123abc456def Step 2/5 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/5 : COPY requirements.txt . ---> Using cache ---> 345abc678def Step 4/5 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 456def789abc Collecting flask Installing collected packages: flask Successfully installed flask-2.2.2 Removing intermediate container 456def789abc ---> 901abc234def Step 5/5 : COPY . . ---> 567def890abc Successfully built 567def890abc Successfully tagged my-app-image:1.0
-t - Assigns a name and tag to the image for easy reference.
This command lists the Docker images filtered by the name 'my-app-image' to verify the image was built successfully.
Terminal
docker images my-app-image
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app-image 1.0 567def890abc 10 seconds ago 120MB
This runs the built image in a container to test if the 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 exits to keep the system clean.
Key Concept

If you remember nothing else from this pattern, remember: building images in CI automates packaging your app so it runs the same everywhere.

Common Mistakes
Not specifying the build context as '.' in the docker build command.
Docker won't find the Dockerfile or app files, causing the build to fail.
Always include '.' at the end of 'docker build' to specify the current folder as context.
Forgetting to tag the image with -t during build.
Without a tag, the image gets a random ID making it hard to reference later.
Use '-t name:tag' to give your image a clear name and version.
Running the image without testing it first.
You might deploy a broken image if you don't verify it runs correctly.
Run the image locally with 'docker run' to confirm it works before pushing or deploying.
Summary
Use 'docker build -t name:tag .' to create a container image from your app code.
Verify the image exists with 'docker images name'.
Test the image by running it with 'docker run --rm name:tag' to ensure it works.