One good thing about dockerizing all your services is the ability to choose from a plethora of orchestration tools to deploy those docker images in different ways for different needs, like:
- Bootstrapping your multi-tiered app on developer boxes
- Creating ad-hoc disposable staging environments for testing pull requests
- Deploying, scaling and monitoring services in production
There are so many tools out there which help you run docker containers on the cloud - Kubernetes, Rancher, Flynn and many more. If you’re just getting started with docker based deployments and wondering how to set up your first continuous deployment pipeline for your dockerized app, this blog post is for you.
PS: There are no illustrations or specific instructions on how to execute a specific step. That’s intentional because UIs change often, but the concepts should remain the same. Anyways, if you get stuck, drop a comment.
What you need:
- An app with a Dockerfile like this one hosted on Github (or BitBucket)
- A Docker Hub account
- Knowledge of how to write a
docker-compose.yml(or readiness to learn :))
Step 1. Automated builds on Docker Hub
Login to Docker Hub, create an auto-building docker repository by clicking on
“Create” > “Create Automated Build”. Finish the steps by following instructions
or follow this official documentation
if you get stuck. What you need to ensure is that you have set up the automated
deployment of your production ready branch and given it a tag like
in Docker Hub. We’ll use this tag name later.
Step 2. Set up your node
Now log in to Docker Cloud, a tool for running docker containers on nodes - either on a cloud provider or your own servers. Follow your gut or this guide until you are ready with a node running Docker Cloud agent.
Step 3. Create a stack
Once you have set up your node, you can proceed to create a “stack”. A stack is a collection of services which run together to make your application work. Click on “Stacks” and click “Create”. This will open a text editor where you can write something called a “Stackfile”.
Step 4. Write a Stackfile for your application
A Stackfile is a YAML file that specifies the configurations for the
docker containers that are part of your stack. If you have ever written a
docker-compose.yml file, you should feel right at home. If you have no idea
what I’m talking about, read this official documentation.
Here’s an example Stackfile:
db: image: 'postgres:9.4' environment: - POSTGRES_PASSWORD=secret - POSTGRES_USER=user mywebapp: image: 'codemancers/mywebapp:latest' autoredeploy: true environment: - POSTGRES_HOST=db - POSTGRES_PASSWORD=secret - POSTGRES_USER=user - WEB_HOST=example.com links: - db ports: - '3000:3000' volumes: - '/home/user/cache:/workspace/cache'
The above Stackfile will start two containers: a “db” container and a “mywebapp” container. A quick walkthrough:
- image - This is the docker image that will be run as containers. For the service that requires continuous deployment, this is the namespaced repository name along with the tag that you configured on Docker Hub in step one.
- environment - Everything under
environmentwill be passed as environment variables when starting the containers. Use these to configure your containers.
- autoredeploy - This is the secret sauce for continuous deployment. Setting this to true will cause the containers of this service to be redeployed everytime the docker image is updated on Docker Hub.
- links - Using links you can create references to other containers inside
the containers of this service. In the above example,
POSTGRES_HOSTis set as
dbinstead of a static IP address. That’s because
mywebappwill resolve to the IP address of the
- ports - Use this if you want to expose any TCP/UDP port of your container on your host node. In the above example, port 3000 of the container can be accessed from the host node’s port 3000.
- volumes - As the name implies, this allows you to mount directories on the host as volumes on the container when it runs. You can use this for persisting data across container redeployments.
Step 5. Deploy!
Once you have a Stackfile, paste it into the text editor you saw in step 3 and click the button to save and deploy. You can access the deployment logs for the stack by clicking on the event name under “Timeline”.
To recap, this is how the continuous deployment pipeline works:
- You push code to Github
- Github triggers docker image build on Docker Hub
- Docker Hub triggers redeployment of containers started from the docker image on Docker Cloud
PS: For those of you who reached this far, thanks for reading. I’ll make you an offer you can’t refuse. Get in touch with us and I’ll help you set up a kickass dev-to-prod workflow using chatbots and docker that allows you to test feature branches on disposable staging environments. This is free for the first 10 requests I get (sorry, I have only so much time for doing things for free :)).
I hope that helped. Now, dockerize all the things!If you have any questions or feedback, feel free to drop us a mail at email@example.com.