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

How AI is Revolutionizing Quality in Electronics and High Tech

How AI is Revolutionizing Quality in Electronics and High Tech

Electronics and High Tech organizations must balance innovation with product quality. In this eBook, we explore how AI is helping teams do this. id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_self" onsubmit="return...

Related Resources

Building Secure CI/CD Pipelines with GitLab in Air-Gapped Networks

Building Secure CI/CD Pipelines with GitLab in Air-Gapped Networks

Key Takeaways Air-gapped networks protect sensitive code and data, but they break the default assumptions of most CI/CD tools, which expect internet access for images, packages, and scanner updates. GitLab Self-Managed runs fully offline, keeping source control,...

A Guide to Calculating the ROI of Your ALM Tool

A Guide to Calculating the ROI of Your ALM Tool

Key Takeaways ALM ROI comes down to one formula: (gain from investment − cost of investment) ÷ cost of investment. The hard part is quantifying both sides honestly. The highest costs of a fragmented toolchain are soft costs, like rework, manual traceability,...

Strategies for Optimizing Software License Costs

Strategies for Optimizing Software License Costs

Introduction Hello, everybody, and welcome back to another SPK & Associates Vlog. My name is Michael Roberts. I'm the Vice President of Sales and Marketing here at SPK. So today, we're going to talk about something that shows up on every engineering and IT budget...