Learning Docker: Using a container for a simple purpose

Posted by Doug Toppin

Feb 18, 2015 9:00:00 AM Docker Containers

docker-logo-loggedout

Docker is a rapidly evolving facility with great utility. An excellent way to get started learning about it is using a building block process. This also allows you to learn the real world use of it by finding a way to use it for actual tasks that you have to perform.

Here is a simple example of this that you may find helpful: you are not running Linux but want to run something that is available on Linux. 

A practical case for that might be where you are unable to install a useful utility on your machine due to availability or restrictions. You could instead use Docker to temporarily run a Linux container and not change the actual state of your machine. This assumes that you already have Docker installed and working. Note that for this example we do not need to use a Dockerfile as we will only use an already existing public Ubuntu Linux image.

Let's say that you receive a file in 7zip archive format and do not have a tool to extract the contents.
If you do not want to install a suitable unarchiver on your machine you could perform the following steps. Note that each line starts with `host` or `container` to indicate where the action is performed. To follow this you should plan on having two shell terminals open. One will be used for the Docker container and the other to act on it.

One challenge to note that is there is no direct Docker command to copy a file from the host to the container without building a new image. This can be easily overcome using the Docker `exec` command.

Start a stock Ubuntu container and go directly into the shell

host: docker run -i -t ubuntu

Bring Ubuntu up to date (which should make it aware of the p7zip package) and then install the package

container: # apt-get update   
container: # apt-get install p7zip

Using ps get the container id

    host: docker ps

The output of that will include an alphanumeric string that should include something similar to 97774de1565b which is the container id.

There are a few methods to copy files from a host to a container that involve additional steps.

A simpler alternative is to use the Docker `exec` command as follows which will send the file to Docker via stdin and 'cat' in the container will save the input to a file in the container.

host: cat archivefile.7z | docker exec -i CONTAINERID sh -c 'cat > /tmp/archivefile.7z'

Now from your container shell do the archive extract and next create a compatible archive format that you can read (such as a tarball). Note that p7zip will delete the original archive file in the container when it unzips it.

container: # cd /tmp
container: # p7zip -d archivefile.7z
container: # tar cvf archivefile.tar *

Now copy the newly created archive file back to your host machine (which can use the Docker cp command)

host: docker cp CONTAINERID:/tmp/archivefile.tar .
The container can be exited
container: # exit

The new `tarball` can be found in the current directory on your machine.

Hopefully you found this helpful.  Please leave  a comment and let me know what you think.

Are you looking to leverage Docker in your organization but unsure of how to get started? Click here to request a complimentary consultation today.

Posted by Doug Toppin

Doug Toppin has been involved in software systems design and development in both the commercial and defense realms for more than 30 years. His experience includes small and large scale systems with high processing and availability requirements in a variety of applications.

Find me on:

An Open View

Vizuri Blog

Subscribing to our blog is a great way to stay up to date with the latest information from Vizuri, as well as our strategic partners. We focus on providing a range of content that is practically useful and relevant from both a technical and business perspective.

We promise to respect your privacy.

×