spk-logo-white-text-short
0%
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 Hybrid-Remote Playbook

The Hybrid-Remote Playbook

Post-pandemic, many companies have shifted to a hybrid or fully remote work environment. Despite many companies having fully remote workers, many still rely on synchronous communication. Loom offers a way for employees to work on their own time, without as many...

Related Resources

Optimize Your Databases with Azure SQL

Optimize Your Databases with Azure SQL

Making data-driven decisions is one of the most valuable things a business can do to achieve and maintain success. Businesses thrive on their ability to make intelligent, timely decisions based on accurate, accessible data. Without the use of data to inform their...

Seamlessly Transition from AWS CodeCommit to GitLab

Seamlessly Transition from AWS CodeCommit to GitLab

In July of 2024, AWS announced that AWS CodeCommit would no longer be sold to new customers.  And thus begins the journey of winding down a product for AWS.  As AWS CodeCommit approaches its end-of-life, many organizations face a tough decision. Choosing where to...

Tackling Cultural Resistance to Enhance Data-Driven Decision Making

Tackling Cultural Resistance to Enhance Data-Driven Decision Making

Adopting a data-driven approach is essential for companies seeking to enhance decision-making and drive growth.  However, one of the biggest obstacles to achieving this is cultural resistance. This resistance is often from HiPPOs (Highest Paid Person's Opinion), who...