fbpx
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.
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

6 Secrets To A Successful Atlassian Migration At Scale

6 Secrets To A Successful Atlassian Migration At Scale

With large scale migrations, large user bases, multiple Atlassian tools, plenty of apps, and lots of data, moving to Atlassian Cloud may feel like a steep mountain to climb. But, it doesn't have to be. In fact, we've already helped many customers make the move. Plus,...

Related Resources

Storytelling with Data

Storytelling with Data

Telling a story and telling a story with data are similar, but also different. And many of the differences are points that people don’t consider.  In this blog, we’ll try to break down the components of storytelling with data in an effort to share main points to...

Data Engineering Supports Digital Transformation

Data Engineering Supports Digital Transformation

Data engineering supports digital transformation. Fact. But, how do companies move away from more paper processes and towards digital transformation? It’s not easy. And, the larger the organization, the more difficult it is. Companies can take solace in knowing there...

What Is Atlassian?

What Is Atlassian?

Distributed workforces are commonplace in today’s manufacturing and IT worlds. More and more colleagues work remotely. Additionally, we use more data to drive decisions as the market competition increases. Alongside this, our businesses are expected to meet...