Up to this point, you have been running pre-existing images from Docker Hub, built by Toradex or other third-party. But it is possible to create your own Docker image to load whatever application you wish. To fulfill this goal, the Dockerfiles exist.
In this section, you will:
- Write a Dockerfile.
- Build a Docker image from the Dockerfile.
- Upload the image to Docker Hub.
Typographic Conventions
Throughout the Torizon documentation, the following typographic conventions are used:
$ (dollar sign) Command in the host computer (e.g. your PC)
$ Command in your PC
$$ (double dollar sign) Command in a container in the host computer (e.g. your PC)
$$ Command inside a container in your PC
# (hashtag) Command in the target device/board (e.g. Linux terminal)
# Command in the target board, e.g. Colibri iMX6
## (double hashtag) Command inside a container in the target device (Torizon)
## Command inside a container in Torizon
> (greater-than sign) Command in the bootloader (e.g. U-Boot console)
> Command in the Bootloader
No symbol: Command output
$ Command waiting for output
Output
For this Getting Started Guide:
- Successfully completed the previous lesson from this guide.
- Linux Machine (We tested this guide on Ubuntu 18.04 LTS. However, it can work on other distributions.).
For this lesson:
- Docker downloaded and running.
Note: Carefully read this module's cover page clicking on "Module 3: Creating my Container" on the left menu bar before starting this lesson.
In your Linux machine, create a new folder named getting-started on the Desktop and a new file called Dockerfile
inside it. Copy the following content into the newly created file:
Note: a standard Dockerfile
has no extension, therefore make sure your file is not named Dockerfile.txt
, especially since file extensions are hidden by default. Consult this lesson's FAQ for details about naming.
getting-started\Dockerfile
FROM torizon/arm64v8-debian-base
RUN apt update && apt install nano -y
Note: If you wish, modify the Dockerfile to include more commands, such as RUN apt install python
.
In this example, the FROM command shows where to get the base of our Docker image. If you want to cross-build it, make sure to choose a pre-built image for ARM.
We also run some commands to install packages from Debian feeds, to test if the cross-build really works.
At the terminal, login to Docker CLI:
docker login
Follow the prompt with your Docker Hub credentials.
Enter your getting-started folder and build the image:
docker build -t <username>/gs-torizon .
Upload the image to your Docker Hub:
docker push <your-username>/gs-torizon
What is a Dockerfile?
A Dockerfile is a text file containing instructions for building a Docker image. At the
Docker Documentation there is a lot of information about them, and guidelines which we will use in this article.
This is a sample Dockerfile:
Dockerfile
# Points to a base, pre-existing image. In this case, it points to the [ubuntu container](https://hub.docker.com/_/ubuntu), tagged with version 18.04;
FROM ubuntu:18.04
# Copies files from the host machine to the created image;
COPY script.sh /root
# Runs commands as root on the created image;
RUN apt update && apt install nano
# Specifies what command will run when a new container is created based on the resulting image.
CMD [ "./startup.sh" ]
Warning: Be aware that the resulting image will be compatible with the host system architecture only, by default.
Can I have multiple Dockerfiles with different names?
Yes, you can. The command docker build
accepts a parameter for specifying a Dockerfile with a name different from the standard one, which enables you to have separate Dockerfiles for development vs. production, or for different base images, for instance, Python vs. Python slim or the latest stable vs. bleeding edge release of a distro. For example for a Dockerfile named foo.Dockerfile
:
docker build -f foo.Dockerfile .
Any name is accepted, though you may consider using a standard. For instance, some Visual Studio Code plugins automatically have syntax highlight for any files with .Dockerfile
extension, such as prod.Dockerfile
and dev.Dockerfile
.