1-888-310-4540 (main) / 1-888-707-6150 (support) info@spkaa.com
Select Page

Creating and Deploying a Docker Image

Published by SPK Blog Post
on April 26, 2016

In my last blog post, I introduced Docker and all of the potential it gives you to manage your DevOps infrastructure. In this post, I’m going to get a bit more hands on and show you how easy it is to setup an application, create a Docker image of it, and then how to deploy it to as many servers or Docker instances as you like!

For this example, I’ll be using Ubuntu 14.04, but you can create images based on pretty much any distribution. In fact, you can create images for different variants (like Ubuntu 12.04), so you have the ability to use different versions of different applications if that suits your needs. Also for this example I’ll assume you have already installed Docker and it’s required dependencies. If you need full install instructions for Ubuntu, this guide is perfect.

Step One: Setting up our Docker image

All Docker images are built by using a Dockerfile. A Dockerfile is just a plain text file with a bunch of commands. These commands are used by Docker to build the image. In this example, we’re going to setup a simple image that hosts a SSH server, and all of the git environment. That way, we can deploy the image and developers can then use a client to securely push changes to git. So, this is my example Dockerfile (with helpful comments!):

FROM ubuntu:14.04

# This next line updates the repository and downloads and installs openSSH and git into the image. 
# It also cleans up any leftover files to minimize the Docker image :)
RUN apt-get update && apt-get install -y openssh-server && apt-get install git && apt-get clean && rm -rf /var/lib/apt/lists/*

# We need to turn on password authentication, as it's off by default in Ubuntu 14.04
RUN mkdir /var/run/sshd
RUN sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config

# Create the git user
RUN adduser --system git
RUN mkdir -p /home/git/.ssh

# Setup authorized_keys for public key authentication
RUN touch /home/git/.ssh/authorized_keys

# Uncomment this line (or add more) so users can login without password and with their public key instead!
# Don't forget to rebuild the image after doing so!
#RUN echo '<paste users public key here>' >> /home/git/.ssh/authorized_keys

# The Git user is not allowed to login by default, this line changes that.
RUN sed -i s#/home/git:/bin/false#/home/git:/bin/bash# /etc/passwd

# This tells the Docker container to allow this port to be accessed on the image.

# This starts the SSH daemon. And after this, we're ready!
CMD ["/usr/sbin/sshd", "-D"]

Save this file (make sure it’s named Dockerfile), and then tell Docker to build you an image:

docker build -t="ssh-git_img" .

After a bit of rumbling and churning, Docker will complete building the image.

And now you’re ready to run the image!

docker run -p 2222:22 -d -v /home/git/gitrepo --name ssh-git ssh-git_img

A note on the -p command above. That maps port 2222 of the Docker host to port 22 of the Docker image. You can obviously change that to any port number you like, just make sure not to use a port that is already in use on the host!

The -v option creates a data volume within the Docker container. This volume can be used to save data that will persist,  meaning you won’t lose any data stored in that volume. So for our simple Git repository, I created a data volume under the git user’s home directory named gitrepo. The developers using this Docker container will have to commit to that directory for all changes to be saved.

Congrats! You now have a very basic Git repository setup which can be accessed by using port 2222 of the Docker host. There are a lot more things we can do with Docker, but I hope this simple example shows you how easy and quick it is to setup a Docker infrastructure as opposed to creating a large VM farm to manage your DevOps infrastructure. If you have any questions or comments, feel free to leave them in the comment section!

Bradley Tinder
Sr. Integration Engineer
SPK & Associates

Latest White Papers

Atlassian Cloud: Understanding Zero Trust Security

Atlassian Cloud: Understanding Zero Trust Security

Where To Start & Why It Matters What is the Atlassian Cloud Zero Trust Security model? Well, for decades, enterprise security controls were built to protect a large, single perimeter around a corporation. Often described as castle-and-moat security, This approach...

Related Resources

Top 6 Ways To Improve Your DevOps Journey

Top 6 Ways To Improve Your DevOps Journey

Knowing how to improve DevOps can be challenging. But, creating an integrated DevOps toolchain can set organizations apart from the rest. This is because having a well-defined business DevOps journey can reduce errors, improve collaboration and drastically increase...

CloudBees SDA:  Software Delivery Without Silos

CloudBees SDA:  Software Delivery Without Silos

Tired of missing deadlines, and only measuring performance of departments, but not of your entire software delivery organization? This blog will show you how CloudBees Software Delivery Automation (CloudBees SDA) can break down silos, drive DevOps mentality and...