How to Use Dockerized to Run Commands
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Dockerized is a utility that runs common commands, interpreters, and other applications within a Docker container. This saves you from manually installing each command (and its dependencies) to your local system. Using Dockerized allows you to try out a new tool, leverage a tool for a one-off task, or ensure your team is using the same version of a tool.
In this guide, learn more about what Dockerized is and how to install it. Additionally, you can follow along with some example scenarios to start seeing how Dockerized might be useful to you.
Before You Begin
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
sudo
. If you’re not familiar with the sudo
command, see the
Linux Users and Groups guide.What Is Dockerized?
Dockerized packages a set of useful command-line tools, allowing you to use those tools without further installation. Out of the box, Dockerized includes many popular development tools (like git, npm, and pip), database CLIs (like mysql and postgres), networking (like wget and telnet), and many others. For a list, review the Supported Commands section on the GitHub readme page. You can even expand Dockerized with additional commands, so you are not limited to only the ones included by default.
Dockerized accomplishes all of this by leveraging Docker and Docker Compose. Each Dockerized command creates a temporary Docker container with the given tool. These containers let you run each software setup without adding installations or dependencies to your system.
Dockerized has a wide range of use cases and you may already have some of your own in mind. But two categories, in particular, stand out.
Trying out new tools. Dockerized lets you test a range of tools without having to install each one. This allows you to compare and explore a tool’s capabilities without the tedium of installation and the clutter of numerous applications that you may never use again.
Running one-off tasks. Scripting languages excel at running single-use tasks, but typically they require you to have installed an interpreter and its dependencies. Dockerized includes numerous script interpreters, meaning that you can create and run scripts without further installations on your system.
How to Install Dockerized
Dockerized is distributed as a precompiled binary and its only external dependency is Docker. Once you have those set up, you are able to run all of the included commands with Dockerized — as well as any additional commands you add.
Install Docker
Dockerized runs commands through Docker containers, so you need to have Docker installed to be able to use Dockerized.
On most Linux systems, you can install the Docker Engine by following our
Installing and Using Docker guide. Additionally, you need to add any non-root users you want to run Dockerized with to the docker
user group. Follow the section on running Docker as a non-root user in the guide linked above to see the appropriate command to do so.
Download and Install Dockerized
To install Dockerized, download the latest zip file corresponding with your operating system, extract the zip file, and add the Dockerized bin
directory to your PATH variable.
Download the Dockerized package for your operating system from the Releases page on GitHub. The quickest way to do so is by using a single command like the one shown below. This command fetches information about the latest release, finds the download URL for the specified operating system, and downloads the package.
curl -s https://api.github.com/repos/datastack-net/dockerized/releases/latest \ | grep "browser_download_url.*dockerized.*linux-x86_64.zip" \ | tail -n 1 \ | cut -d : -f 2,3 \ | tr -d \" \ | wget -O dockerized.zip -qi -
If you are using macOS, replace
linux-x86_64.zip
in the command above with one of the following strings:- Apple Silicon:
mac-arm64.zip
- Intel:
mac-x86_64.zip
- Apple Silicon:
Since Dockerized is packaged as a zip file, you need a way to extract the file. This guide uses the unzip command, which comes with macOS but not most Linux distributions. Linux users should first install unzip through their package manager:
Ubuntu, Debian, and many other distributions using the APT package manager:
sudo apt install unzip
CentOS/RHEL, AlmaLinux, Rocky Linux 8 (and above) and Fedora:
sudo dnf install unzip
Unzip the Dockerized package to a new directory in your home folder (
~/dockerized/
).unzip dockerized.zip -d ~/dockerized/
Modify your PATH environmental variable to include the Dockerized binary. There are many methods of doing this, though this guide covers two common ways.
Install just for current user: Add the
dockerzied/bin
folder to the PATH variable.export PATH="$PATH:$HOME/dockerized/bin"
Install for all users: Add a symlink of the Dockerized binary file to an existing directory accessible to all users and make sure that directory is on your system’s PATH variable.
First, move the
dockerized/
directory to a more centralized location.sudo mv ~/dockerized/ /usr/local/lib/dockerized/
Then, create a symbolic link within a directory on your system’s shell path. Typically,
/usr/local/bin/
is on that path, but change that in this example as needed.sudo ln -s /usr/local/lib/dockerized/bin/dockerized /usr/local/bin/dockerized
Verify the setup by checking running a command from Dockerized. For instructions, review the Run Commands with Dockerized section below.
Run Commands with Dockerized
To start using Dockerized to run one of the included command-line tools, enter the following command. Replace COMMAND with the command you wish to run.
dockerized COMMAND
For instance, you can use the npm utility (included with Dockerized). As a basic example, run the command below to check the version of npm that is used:
dockerized npm --version
The first time a command is run, Dockerized downloads the appropriate image(s) needed for the associated tool. You should then see the command’s output.
[+] Running 10/10
⠿ npm Pulled
[...]
8.5.2
If you run the command outside of Dockerized, you may notice different output or receive a command not found error. This is because your system either is running a different version of the utility used by Dockerized or, more likely, the utility isn’t installed locally on your system.
To demonstrate further, the following is a simple Hello, world!
execution using one of the interpreters included with Dockerized, Lua.
dockerized lua -e 'print("Hello, world!")'
Hello, world!
What follows are some more advanced usages of Dockerized. These each aim to broadly cover a use case, giving you bases and some ideas for how you might use Dockerized. The possibilities are extensive, but these examples are meant to start you out and show off more of Dockerized’s capabilities.
Database Maintenance Scripting
Dockerized includes containers for several scripting languages, including Lua, Python, and Ruby. Scripting languages excel at being able to complete one-off tasks, and this fits well with the model for Dockerized. You do not need to install a script interpreter and dependencies whenever you need to run a simple, one-off script.
This example, while rudimentary, demonstrates how you can set up a Ruby script for modifying a database.
Create a directory for the script, and change into that directory. This example puts the new directory in the current user’s home directory.
mkdir ~/ruby-script cd ~/ruby-script
Use the Gem command included with Dockerized to install the
sqlite3
Ruby gem. The command below ensures that the gem gets installed into agem/
subdirectory of the current directory.dockerized gem install sqlite3 --install-dir ./gems
Create a
test.rb
file within the directory, and give it the contents shown here. The example code has comments explaining what each part does.In summary, the script connects to an SQLite database file. It displays the results from a given table if there are any. If there are not any results, the script inserts several entries.
- File: test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# Alter the load path for Ruby to include the local gem path $LOAD_PATH.unshift 'gems/gems/sqlite3-1.6.0-x86_64-linux/lib' # Import the sqlite3 gem require 'sqlite3' # Make a connection to test.db; configure to process rows as hashes db = SQLite3::Database.open 'test.db' db.results_as_hash = true # Create a table if it does not already exist db.execute 'CREATE TABLE IF NOT EXISTS the_data (id INTEGER PRIMARY KEY, name TEXT);' # Define an array of values to be inserted into the table name_array = ["first name", "second name", "third name"] # Fetch everything in the table results = db.query 'SELECT name FROM the_data;' # If there is anything in the table, display what is there; if there is # not anything in the table, insert the values from name_array first_result = results.next if first_result puts "Results found!" puts first_result["name"] results.each do |result| puts result["name"] end else puts "No results found" puts "Inserting new records" name_array.each do |new_name| db.execute 'INSERT INTO the_data (name) VALUES (?);', new_name end end
Run the script using the Ruby container from Dockerized.
dockerized ruby test.rb
The first time running the script, you should see an indication that no records were found and that new records are being added.
No results found Inserting new records
Run the script again, and you should see that the script fetches the results it inserted into the table on the previous run.
Results found! first name second name third name
Static HTTP Server
Dockerized includes several tools that can serve static files over HTTP — Node, Python, and Ruby, for instance. These can be useful if your want to test some static website files without preparing an entire project.
Python provides an easy way to see this in action through its http.server
module.
Create a directory for the static website files, then change into that directory. This example uses a directory in the current user’s home directory.
mkdir ~/simple-website cd ~/simple-website
Give the directory an
index.html
landing page. From there you can provide whatever additional content you want. The following gives a basic example.- File: index.html
1 2 3 4 5 6 7 8 9
<!doctype html> <html lang="en"> <head> <title>A Simple Website</title> </head> <body> <p>This is a test website.</p> </body> </html>
Run the following Dockerized command to serve the static website files in the current directory. The
-p
option lets you forward a port from within the container to the host machine.dockerized -p 8080:8080 python -m http.server 8080
In this case,
http.server
serves the content on port8080
of its container, and the-p
option forwards that port to port8080
on the host machine as well.Navigate to the host’s port
8080
in a web browser, and you should see the website’s content.This is a test website.
How to Add More Commands to Dockerized
While Dockerized comes with a useful set of commands, you may want to expand the existing functionality and add new commands. As Dockerized uses Docker Compose, you can create additional Compose files to define new commands. Then you can specify the location of the Compose file(s) through the use of Dockerized environment variables.
Define the new command(s) within Docker Compose files: Compose files are used to define additional commands for use with Dockerized. A single file can store multiple commands or you can create a file for each command. If you wish for the commands to be used by other users, make sure the corresponding Compose file is accessible by those users. Otherwise, if your current user is the only intended user, it can be stored within your home directory.
As an example, create a new file called
new-command.yml
with the following contents:- File: ~/new-command.yml
1 2 3 4 5
version: "3" services: example: image: curlimages/curl:latest entrypoint: ["curl"]
This creates a new service named
example
. This service name defines the Dockerized command, so a service namedexample
is called by runningdockerized example
. The service uses the curlimages/curl Docker image and theentrypoint
tells Dockerized to run thecurl
command within the container immediately whenever the service is called.Specifiy the location of the Compose file(s) within Dockerized: Once you have created your Compose files, you need to tell Dockerized where to look. This is accomplished through the use of environment configuration files (
dockerized.env
), which can be stored within your home directory (so you can access the new command globally) or within a specific project directory (so you can access the command from within a certain project folder). In this way, Dockerized allows you to apply a scope to additional commands, fitting them to specific users and project directories. There are two variables that can be used to specify the relative location of the Compose file(s):${HOME}
(which corresponds to the user’s home directory) and${DOCKERIZED_PROJECT_ROOT}
(which corresponds to the current working directory).To continue with the example in the previous step, create a new file in your home directory called
dockerized.env
with the following contents:- File: ~/dockerized.env
1
COMPOSE_FILE="${COMPOSE_FILE};${HOME}/new-command.yml"
This updates the
COMPOSE_FILE
setting to include both the existing built-in commands (${COMPOSE_FILE}
variable) and the new command(s) (${HOME}/new-command.yml
). Additional locations can be added using a semicolon (;
) as the delimiter.
You can test out this example by running the newly defined command as shown below, replacing example.com with the URL of any web page or API endpoint you would like to output.
dockerized example example.com
Since the Dockerized example
service name is mapped to the curl command, running the command actually runs curl
from within the Docker container and displays the contents of the web page provided.
For additional instructions on defining new commands or adjusting Dockerized settings, review the Customization section of the readme file on GitHub.
This page was originally published on