Turn Your Code into a Real Program: Packaging, Running and Distributing Scripts using Docker

Easily share and run code in a Docker container

Turn Your Code into a Real Program: Packaging, Running and Distributing Scripts using Docker
Your packaged code is being delivered (image by Kira auf der Heide on Unsplash)

With Docker, it is easy to package your code into an image that we can run anywhere, anytime; regardless of your hardware or software. It will run on a laptop, Raspberry Pi, Server, Mac, Linux or Windows. This article will detail the process of packaging your script into a Docker image that can be shared and run. It focuses particularly on how to run the programs that exist in the Docker image.

By the way: are you new to Docker? Check out this article for a clear, brief introduction on Docker and this article on how to use Docker Compose.


1 Goal and preparation

I’ve written two programs: a Python command-line application and a bash file. You are not restricted to Python and bash, you can use Docker to run every kind of program you like.

The bash file is called testbash.sh and just echo’s “hello world”, nothing special. The Python file is called py_cmd.py and is a simple command-line application that can tell you jokes. We’ll test this out later. The goal is to package these programs into a Docker image, run the image and call the specific programs.

First, we’ll check out the project structure and the Dockerfile that we use to package the programs into an image and then we’ll run the image and use the programs. Lastly, we can get into distribution by uploading our image to Docker Hub; a sort of YouTube for Docker images.

Our goal: nicely packaged code (image by Kira auf der Heide on Unsplash)

2 Packaging into a Docker image

Think of an image as a CD. It contains all the code and instructions to use the code. The CD can be played in the same way that we can run an image. A running image is called a container. Our goal is to package all of our source code into an image.

Project structure

We have just 4 files in our project directory:

  • py_cmd.py which contains our Python command-line application
  • testbash.sh which contains our bash script
  • requirements.txt which contains a list of dependencies we use for python (check out this article for more information)
  • Dockerfile which contains our packaging instructions

The contents of the first three files aren’t that important but you can check them out here.


Dockerfile; packaging instructions for the image

The Dockerfile is most important here. Let’s check it out and walk through it line by line.

There are just 3 steps in packaging our code:

  • Line 1: we install Debian that has Python 3.9 installed into our image
  • Line 3–6: we copy our requirements into the image and tell pip to install
  • Line 8–10: We make sure there is a folder called app in the image and set that as our current directory. Then we copy all of our source code from our laptop into the image. The . . implies from our laptop’s current directory to the work directory that we’ve set in the image (which is /app)

These simple steps build our image, install all dependencies and copy over our source code.


Actually packaging the code into an image

The Dockerfile just contains the packaging instructions. We still have to actually execute them in order to build an image. We’ll need to use a terminal for this. If you are unfamiliar with using a terminal check out this article for some simple instructions.

Open a terminal and cd to the directory that contains the Dockerfile. Then execute the following command: docker build . -t medium_cmd_app.

  • The docker build . part tells docker to check in the current directory for your Dockerfile. An alternative is to use docker build -f path/to/Dockerfile. This way you can execute from another directory and your Dockefile can have another name.
  • The -t medium_cmd_app tells docker to tag the image with medium_cmd_app. This just gives it a name so we can call it later.

3 Running the image

If all went well we now have a built image. Time to spin it up and use our programs! We can simply use the two commands below. These will run the image and execute one of the scripts that it contains.

medium_cmd_app
  • docker run -it tells docker to run in interactive mode
  • medium_cmd_app is the name of our image, we’ve tagged it with this name (-t medium_cmd_app)
  • python3 py_cmd.py is just like normale: execute py_cmd.py with pytho3. Easy!

If all went well you’ll see “Hello from the bash file” when executing the testbash.sh script. And you’ll see a command-line interface when executing the Python program like in the image below:

Our Python command-line program can make jokes! Type ‘joke’ to hear a new one every time.

4 Distributing our image

Once our image is built we can run it on any machine that has Docker installed. But how do we get the image on the machine? An easy way is to upload your image to Docker Hub. Think of this as a sort of YouTube for Docker images; anyone can upload and download any image on there.

Docker Hub is stacked full of images (image by Chuttersnap on Unsplash)

Pushing to Docker Hub

First we’ll need to create an account: go to https://hub.docker.com to sign up. Then we need to create a repository; this is a place where your image will go. I’ve named mine medium_cmd_app.

The next step is to open a terminal, log in to Docker Hub and push (upload) your image. Here’s how to do it:

  1. Open a terminal
  2. CD to your project directory
  3. docker login → enter your credentials
  4. Build the image again but with the following tag:
    docker build . -t [your_dockerhub_username]/[your_respo_name]

5. docker push [your_dockerhub_username]/[your_respo_name]

Now the image is uploaded to Docker Hub. Anyone with Docker installed can now execute docker run [your_dockerhub_username]/[your_repos_name] to download the image and run it!

Pulling from Docker Hub

I’ve uploaded our image from before to Docker Hub. You can download it and run it by executing the code below:docker run -it mikehuls/medium_cmd_app python3 py_cmd.py
docker run -it mikehuls/medium_cmd_app bash testbash.sh

This will look for an image called mikehuls/medium_cmd_app on your machine and if it can’t find it: download it from Docker Hub if it exists there.

Time to start distributing those images! (image by Kai Pilger on Unsplash)

Conclusion

As we’ve seen distributing your code in a Docker image guarantees that it can run any place where Docker is installed. I hope I’ve shed some light on how to package, distribute and run your code in this containerized way. Also check out this article for a great, practical example.

If you have suggestions/clarifications please comment so I can improve this article. In the meantime, check out my other articles on all kinds of programming-related topics like these:

Happy coding!

— Mike

P.S: like what I’m doing? Follow me!