Introduction
Docker describes itself as “an open source project to pack, ship and run any application as a lightweight container.” The principle idea behind development with Docker is to package your application in a way that allows you to run the application on any infrastructure that handles Docker images. Stack Harbor has made it easy for you to begin developing Docker images using our turnkey Docker application stack.
Accessing Your Application
Once your stack is built, you can verify that you have a working Docker application by SSHing into your remote machine and running docker to see the list of commands you can use. No introductory tutorial would be complete without the mandatory ‘Hello, World!’ application — let’s get started.
We’ll first use Docker to pull a Ubuntu image from the Docker repositories. The Docker repositories are a centralized location for Docker users to push and pull Docker images from. The repos contain images for many of the popular Linux distributions, as well as additional projects and add-ons on top of them.
docker pull ubuntu:latest
This command will pull the latest stable release of Ubuntu which is 14.04 Trusty (the same version of Ubuntu that your Stack is running). Once it finishes downloading and extracting, you can verify that it was correctly pulled from the repos by running docker images. Once you’ve verified that you’ve downloaded a Ubuntu base image, let’s run a simple ‘Hello, world!’. Execute the following command:
docker run ubuntu echo "Hello, World!"
You should have been echoed “Hello, World” on your command line. Although it might not seem very exciting, what happened behind the scenes is that Docker ran the echo command using the base ubuntu image, redirected the output to your terminal, then destroyed the image. To get a better idea of how Docker containers work, let’s create a persistent Docker container in which we’ve stored a hello_world.txt file in the /root directory.
docker run -i -t ubuntu:latest /bin/bash
Let’s disect this command piecewise. We’ve already seen docker run which runs a Docker image. the -i -t flags tell Docker to run the image that we’re supplying interactively. You’ll be able to execute shell commands from your terminal just as you would a normal operating system. The ‘ubuntu:latest’ portion we’ve also already seen — it tells Docker to run the ‘ubuntu’ images with the ‘latest’ tag. Finally, we’ve supplied Docker with /bin/bash to tell it which shell to run when it enters the interactive prompt. After you’ve run this command, your terminal prompt should change to root@some_uid where some_uid is a randomly generated identified created by Docker to identify your running container. Now that we can interactively issue commands with the container, let’s create the sample text file.
touch hello_world.txt
This creates the hello_world.txt file and doesn’t write anything to it. Now that we’ve done all that we wanted to do inside the Docker container, lets exit back to our Stack and save the state of the container. Press CTRL-P followed by CTRL-Q to exit the container but not kill the container process. Your terminal prompt should change back to its normal state, indicating that you’re again interacting with your Stack. If you run docker ps you’ll see a list of running containers — one of which should be the container you just detached from. Take note of the name of the docker container, shown under “NAMES”. Docker automatically assigns each container a silly name if you don’t provide one so that you don’t have to reference your containers with a long UID. We’re going to be committing this Docker image to our local repository so that we can run new containers off of the newly-created base image. Run the following command:
docker commit [container name] helloworld:test
You should substitute [container name] with the name of the running container that you took note of earlier. When this is done, running docker images should show you a new image with the repository helloworld and tag test. You can now build more images off this base image by running docker run helloworld:test.
Final Words
Congratulations! You have a dedicated Stack on which you can begin developing application in Dockerized environments. Much of the power of Docker comes from its rich community and from the Docker repositories. For more information regarding Docker and server administration, check out our Community Section. From all of us at Stack Harbor, ahoy!