0
0
Dockerdevops~5 mins

Deploying from CI/CD pipeline in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Deploying from a CI/CD pipeline means automatically building and sending your app to a server or cloud when you update your code. This saves time and avoids mistakes from doing it by hand.
When you want your app to update automatically after you push code changes to your repository
When you want to build a new version of your app image and run it without manual steps
When you want to test your app in a clean environment every time you change code
When you want to avoid forgetting steps or making errors during deployment
When you want to deliver updates faster and more reliably to users
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  webapp:
    image: my-app:latest
    ports:
      - "8080:80"
    restart: always

This docker-compose.yml file defines a service named webapp that runs the my-app:latest Docker image. It maps port 8080 on your machine to port 80 inside the container, so you can access the app in a browser at localhost:8080. The restart: always ensures the container restarts automatically if it stops.

Commands
This command builds a Docker image named 'my-app' with the 'latest' tag from the current folder. It packages your app and its environment into one image.
Terminal
docker build -t my-app:latest .
Expected OutputExpected
Sending build context to Docker daemon 10.24MB Step 1/5 : FROM python:3.10-slim ---> 123abc456def Step 2/5 : COPY . /app ---> Using cache Step 3/5 : WORKDIR /app ---> Using cache Step 4/5 : RUN pip install -r requirements.txt ---> Using cache Step 5/5 : CMD ["python", "app.py"] ---> Using cache Successfully built abcdef123456 Successfully tagged my-app:latest
This command starts the services defined in the docker-compose.yml file in detached mode, running your app in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating webapp ... done
-d - Run containers in the background (detached mode)
This command lists running Docker containers so you can check if your app container is running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abcdef123456 my-app:latest "python app.py" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp myproject_webapp_1
This command shows the live logs of your app container so you can see what it is doing or if there are errors.
Terminal
docker-compose logs -f
Expected OutputExpected
Attaching to myproject_webapp_1 webapp_1 | Starting app... webapp_1 | App running on port 80
-f - Follow log output live
Key Concept

If you remember nothing else from this pattern, remember: automate building and running your app with Docker commands in your CI/CD pipeline to deploy reliably and quickly.

Common Mistakes
Not tagging the Docker image with a consistent name and tag
The pipeline won't know which image to deploy or may deploy an old version
Always use a clear image name and tag like 'my-app:latest' or version numbers
Running 'docker-compose up' without '-d' in CI/CD
The command will block the pipeline and never finish because it runs in the foreground
Use 'docker-compose up -d' to run containers in the background
Not checking container status with 'docker ps' after deployment
You might miss that the container failed to start or crashed
Always verify running containers with 'docker ps' to confirm deployment success
Summary
Build your Docker image with 'docker build' to package your app.
Use 'docker-compose up -d' to start your app container in the background.
Check running containers with 'docker ps' to confirm deployment.
Follow logs with 'docker-compose logs -f' to monitor your app.