Your First Custom Image in Docker

melissa • January 21, 2015 • 1 Comment

In my last article at 24x7ITConnection, I talked a little bit about what docker is, and walked everyone through setting up Boot2docker on their platform of choice.  At this point, I’m assuming you have it boot2Docker running, or a Linux machine running Docker.  Now, let’s take it a step further, and look at how we can use an application within Docker.  We’re going to create an Ubuntu container, and install Apache in it.  Then, we’re going to do some testing to make sure everything is working.  Finally, we’re going to export or new container to the Docker Hub.

The first step, if you don’t already have it running, is to start boot2Docker.  You can do this by double clicking the icon on your desktop, or, if you’re using Mac OS X, you can type boot2docker start, then enter the export commands you are given on the screen.  We’re going to need them again later, so it may be a good idea to copy them.

0

Now, let’s get our Ubuntu image by entering docker run ubuntu /bin/echo hello world.  As you can see, if you don’t already have the Ubuntu image, docker will download it for you.  There’s also a special bonus round, you will see hello world on your screen when it is complete.

2

Once your download is complete, type docker run -t -i ubuntu /bin/bash.  What you will end up with is a shell prompt for the Ubuntu image you have just run.  Now, we can install Apache, just like we would in any Linux environment.  Type apt-get update && apt-get install -y apache2 curl, and you will see the Apache installation begin.

3
Once the installation is complete, we can now set up a test webpage to make sure things are working.  First, navigate to the directory we’re going to put or page in, by typing cd /var/www/html.  Then, let’s put something to test there.  Type echo “vMiss Taught Me This” > index.html.  You don’t actually have to say vMiss Taught Me This, you can say whatever you would like within the quotation marks.  Yes, Apache, we know you don’t know the FQDN, but that’s okay.  Finally, type curl http://locahost to view the website running, which consists of the text you entered before.

4

Congratulations!  You’ve successfully downloaded and run an Ubuntu image within Docker, and installed and tested Apache.  You’ll notice after we got our image running, we just ran the commands we were used to.  In this case, it was Linux commands for Ubuntu.  This is one of the big positives with Docker, we treat our apps in containers just like we would outside of them.  We don’t need to change anything we’re doing!

Now, let’s put our image on Docker Hub!  If you don’t already have one, head on over there and get yourself set up.  You can use the same account information you have set up for GitHub.  Once you have an account, open up another terminal window, and enter those export commands again.  We want to see what containers we have running, so type docker ps.

5

This will show you the containers you currently have running.  Take note of the container ID, in this case,  815acfe802a5. We’re going to need this.  First, we’re going to commit our Docker image, then push it to the Docker Hub.  To commit our container, we’re going to need the container ID before.  Type docker commit containerID YourDockerHubUserName/apache-test.  Now, we’re going to push it up to the Docker Hub by typing docker push YourDockerHubUserName/apache-test.  You’ll get prompted for your Docker Hub login credentials.

6

Now, check it out on your profile page on Docker Hub by heading over to hub.docker.com.  You’ll be able to see your image, and the creation of your repository, as well as the push.

7

That’s it!  Now you’ve created a new container (Ubuntu), installed an app (Apache) inside of it, and pushed it up to the Docker Hub!  Pretty simple, isn’t it?  I figured this out by taking a look at the CoreOS Getting Started with Docker guide, as well as the instructions on how to use Docker on Mac OS X.  Stay tuned for more on getting settled on board the Docker ship!

Categories Docker

1 Comment