How to Deploy a Containerized App with CircleCI
In this article
To deploy a containerized app with CircleCI, you build the Docker image in a CircleCI job, push it to a registry such as Docker Hub, then run a deploy job that pulls the image onto your server or platform. The whole flow lives in a single .circleci/config.yml file. You need a Dockerfile, a registry account, and credentials stored as CircleCI environment variables. This guide walks through the exact steps, from adding the config to verifying the running container.
What You'll Need
- A project hosted on GitHub or Bitbucket with a working
Dockerfileat the repository root. - A CircleCI account connected to that repository.
- A container registry: Docker Hub, GitHub Container Registry (GHCR), or Amazon ECR.
- Registry credentials (username and access token, or AWS keys for ECR) ready to paste into CircleCI.
- A target host or platform where the container will run: a VPS with Docker installed, a Kubernetes cluster, or a managed service.
- SSH access to that host if you deploy manually over SSH.
Menu labels in CircleCI and in your registry can differ slightly by version and plan, so treat the exact wording below as a guide rather than a guarantee.
Step-by-Step
- Create the CircleCI config file. In your repository, create a folder named
.circleciand inside it a file namedconfig.yml. CircleCI reads this file on every push. Start with a minimal skeleton that defines a version and a workflow, then add jobs to it in the next steps. - Add a build job. In the config, define a job that uses a Docker executor (for example an image with Docker CLI preinstalled, or the
dockerexecutor with asetup_remote_dockerstep). The job should rundocker build -t your-registry/your-app:$CIRCLE_SHA1 .so each commit produces a uniquely tagged image. Tagging by commit SHA avoids overwriting the samelatesttag and makes rollbacks possible. - Log in to the registry inside the job. Add a step that authenticates before pushing. For Docker Hub, use
docker login -u $DOCKER_USER -p $DOCKER_PASS. For GHCR, log in with a personal access token that has package write scope. For ECR, run the AWS CLI login command that returns a password and pipes it intodocker login. Never hardcode credentials in the config file. - Push the image. After a successful build, run
docker push your-registry/your-app:$CIRCLE_SHA1. If you also want a moving tag, push the same image again as:latestin a separate step. Keep the SHA tag as the source of truth for deployments. - Store secrets as environment variables. In the CircleCI web app, open your project, go to Project Settings, and find the Environment Variables section. Add
DOCKER_USER,DOCKER_PASS, and any SSH keys or cloud credentials there. Mark them as secret so they are masked in logs. Contexts are a cleaner option if several projects share the same credentials: create a context at the organization level and attach it to the workflow. - Add a deploy job. Create a second job that depends on the build job. The simplest form uses SSH: add your private key as an environment variable or SSH key in CircleCI, then run
ssh user@your-hostand executedocker pull your-registry/your-app:$CIRCLE_SHA1followed bydocker stopanddocker run -d --name your-app -p 80:3000 your-registry/your-app:$CIRCLE_SHA1. Adjust the port mapping to match your app. If you deploy to Kubernetes, replace the SSH commands withkubectl set imageusing the same SHA tag. - Wire the jobs into a workflow. At the bottom of the config, add a
workflowsblock with a name and a list of jobs. Make the deploy job require the build job so it only runs after a successful push. Add a filter so deploys run only on your main branch, for examplebranches: only: main. This prevents every pull request from hitting production. - Commit and push. Push the config to your repository. CircleCI detects the new file and starts a pipeline automatically. Watch the pipeline in the CircleCI dashboard; each job shows live logs. Fix any errors in the config and push again.
- Verify the deployment. Once the deploy job is green, open your app's URL or run
docker pson the host to confirm the container is running. Check the container logs withdocker logs your-appfor startup errors. If the app is behind a reverse proxy, confirm the proxy is pointing at the new port.
Common Mistakes
- Hardcoding credentials. Putting registry passwords or SSH keys directly in
config.ymlexposes them to anyone with repository access. Always use CircleCI environment variables or contexts. - Using only the
latesttag. Deployinglatestmakes it impossible to know which commit is running and breaks rollbacks. Tag with the commit SHA and keeplatestas a convenience only. - Deploying on every branch. Without a branch filter, pull requests can deploy to production. Restrict the deploy job to your main branch or a release branch.
- Forgetting
setup_remote_docker. If you use the Docker executor and try to run Docker commands without enabling the remote Docker environment, the build fails with a daemon connection error. - Not pinning image versions. Using
image: circleci/node:latestor similar floating tags can break builds when upstream images change. Pin a specific version.
Tips
- Cache Docker layers between builds where possible; it cuts build time significantly on larger images.
- Keep the image small by using multi-stage builds and a slim base image. Smaller images pull faster on the deploy host.
- Use CircleCI contexts to share credentials across projects without duplicating variables.
- Add a health check step after deploy: curl the app's health endpoint and fail the job if it does not return a success status. This catches broken deployments before they reach users.
- Store the deploy script in the repository rather than inline in the config. It is easier to test and review.
Troubleshooting
If the pipeline fails at the build step, read the log for the exact command that failed. A common cause is a missing Dockerfile path or a build context that does not include needed files. If the push fails with an authentication error, confirm the registry credentials are correct and that the token has write permission; regenerate the token if unsure. If the deploy job cannot connect over SSH, check that the host's firewall allows the connection and that the public key is in the host's authorized keys file. If the container starts but the app is unreachable, verify the port mapping and that the app listens on 0.0.0.0 rather than 127.0.0.1 inside the container. If a deploy succeeds but the old version is still running, check whether your deploy script actually stopped and removed the previous container before starting the new one.
Frequently Asked Questions
Do I need a paid CircleCI plan to deploy containers?
No. The free tier includes pipeline minutes, which is enough for small projects. Larger builds or private repositories with heavy usage may need a paid plan.
Can I deploy to Kubernetes instead of a single server?
Yes. Replace the SSH steps with kubectl commands that update the deployment image to the new SHA tag. Store the kubeconfig as a CircleCI environment variable or context variable.
Where should I store my registry password?
In CircleCI project settings under Environment Variables, or in an organization-level context. Never commit it to the repository.
How do I roll back a bad deployment?
Because each image is tagged with its commit SHA, redeploy the previous SHA tag using the same deploy job. On Kubernetes, run kubectl rollout undo.
Does CircleCI build the image on every commit?
Yes, if the workflow is configured to run on every push. You can limit builds to specific branches with workflow filters to save minutes.
Can I use GitHub Container Registry with CircleCI?
Yes. Create a personal access token with package write permission, store it as an environment variable, and log in with docker login ghcr.io using your GitHub username and the token.