TeamCity is a powerful CI/CD server from JetBrains, and running it inside Docker simplifies setup, upgrades, and isolation. This guide shows how to set up TeamCity Server and a TeamCity Agent capable of building Docker images.
Prerequisites
Before you start, ensure you have:
- Docker installed and running
- Docker Compose installed
- Sufficient permissions to run Docker commands on your host
- A terminal/command-line interface
Step 1: Create a directory structure
It’s best practice to separate your data, logs, and agent configuration:
mkdir -p teamcity/{data,logs,agent}
cd teamcity
Your directory structure should look like this:
teamcity/
├── docker-compose.yml
├── data/ # Server persistent data
├── logs/ # TeamCity logs
└── agent/ # Agent configuration
Step 2: Create the Docker Compose file
Create a file called docker-compose.yml with the following content:
version: "3.8"
services:
teamcity-server:
image: jetbrains/teamcity-server:2023.11.4
container_name: teamcity-server
ports:
- "8111:8111"
volumes:
- ./data:/data/teamcity_server/datadir
- ./logs:/opt/teamcity/logs
restart: unless-stopped
teamcity-agent:
image: jetbrains/teamcity-agent:2023.11.4
container_name: teamcity-agent
user: root
environment:
- SERVER_URL=http://teamcity-server:8111
volumes:
- ./agent:/data/teamcity_agent/conf
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- teamcity-server
restart: unless-stopped
Key points:
- Server volumes:
-
./datastores TeamCity server data and build history. -
./logskeeps server logs persistent across container restarts.
- Agent setup:
- The agent runs as
rootto have permissions to use the host Docker socket. -
/var/run/docker.sockis mounted to allow the agent to access the host Docker daemon. -
SERVER_URLpoints the agent to the server.
Step 3: Start TeamCity
Run:
docker compose up -d
-
-druns containers in the background. - The server will be available at http://localhost:8111.
- The agent container will automatically try to connect to the server.
Step 4: Initial TeamCity setup
- Open your browser and go to:
http://localhost:8111
- Follow the web setup wizard:
- Confirm the data directory (mapped to
./data) - Choose a database (internal database is fine for testing)
- Accept the license
- Create the initial admin account
Step 5: Authorize the agent
- Go to Agents → Unauthorized in the TeamCity UI.
- Click Authorize for your agent.
- Once authorized, the agent will show as Connected and Compatible.
Step 6: Verify Docker capability
Open a terminal inside the agent container:
docker exec -it teamcity-agent docker version
You should see the Docker client and server versions. If so, the agent can now build Docker images.
Step 7: Configure a Docker build in TeamCity
You can use Command Line or Docker runner build steps.
Example Command Line step:
docker build -t my-app:%build.number% .
docker push my-app:%build.number%
Example Docker Runner step:
- Add a Docker build step in the UI.
- Set the Dockerfile path.
- Set the image name.
- TeamCity will use the agent’s Docker access to build images.
Step 8: Recommended best practices
- Keep your
/datafolder on fast, persistent storage. - Backup your TeamCity server data regularly.
- Limit access to the TeamCity server and Docker socket, since running the agent as
rootgives it full control over Docker on the host. - For production environments, consider a Docker-in-Docker setup or a remote Docker host for better isolation.
Step 9: Stopping and restarting
To stop:
docker compose down
To restart:
docker compose up -d
The agent will reconnect automatically after restart.
✅ Summary
With this setup:
- You have a fully functional TeamCity server running in Docker.
- Your TeamCity agent can build Docker images using the host Docker daemon.
- Data and logs persist across restarts, and you can expand with more agents if needed.
This setup is ideal for testing, development, and small production environments.
Top comments (0)