Member-only story
Turn Your Code into a Real Program: Packaging, Running and Distributing Scripts using Docker
Easily share and run code in a Docker container
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.
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