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

Creating and Deploying a Docker Image

Written by SPK Blog Post
Published 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.
EXPOSE 22

# 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

The Next Chapter of Jira Service Management

The Next Chapter of Jira Service Management

The service industry is only becoming more competitive as the years pass, making efficient delivery vital to success. Development and Operations teams need to work together to deliver aid and Jira Service Management can help achieve this. Explore the future of Jira...

Related Resources

Exploring Modern Software Deployment Strategies

Exploring Modern Software Deployment Strategies

Deploying software can feel like a gamble due to all the strategies and solutions on the market, but it doesn’t have to be. Discovering which software deployment strategy works best for your organization is a great place to start. This strategy, combined with a modern...

Automatically Visualizing Dependencies in Codebeamer

Automatically Visualizing Dependencies in Codebeamer

If you work in the software and systems engineering space, you likely understand that managing dependencies across multiple components and requirements is critical for project success. Unfortunately, specifications can be difficult to track, and dependencies hard to...