Docker Cookbook for CBIA

In the following lines, the most frequently asked questions are listed and subsequently answered. If no one matches your needs, please send an e-mail to cbia-supp@fi.muni.cz.


What does Docker image mean?

Docker image is simply a frozen and remarkably simplified operating system. In analogy to object oriented programming, Docker image is a class. It does nothing but it contains all the necessary defintions.


What does Docker container mean?

Docker container is an instantiated Docker image (in analogy to object oriented programming, Docker container is an object). There might be several Docker containers built upon one Docker image. All the containers are independent and modifying any container does not influence behaviour of any other container nor the structure of the shared image.


I need to deliver my application (with all the necessary dependencies) to somebody else.

You need to create a file called Dockerfile in which you specify the environment in which your application should work. Below, you can find a sample Dockerfile, that prepares a full support for some Python application computeFFT.py

# Set the base image to Ubuntu
FROM ubuntu:22.04

# Update the repository sources list
RUN apt-get update

# Install necessary packages
RUN apt-get install -y python3-pip
RUN pip install numpy

# Prepare directory for the script
WORKDIR /opt/scripts

# Copy the script to Docker image
ADD computeFFT.py /opt/scripts

Here, you can find a more complex Dockerfile that creates the Docker image distributing the i3dlib-based binary file threshold(.exe) together with all the dependencies.


What shall I do with Dockerfile?

Dockerfile is a configuration script that prepares the Docker image. Run the following command in the directory, where the Dockerfile is located, to create the Docker image:

$> docker build -t fft_image .

You can also list all the available Docker images (and their sizes):

$> docker image ls

REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
fft_image             latest    da2ab4a1f224   16 minutes ago   553MB
my_web_server_image   latest    89b87c930906   38 minutes ago   772MB
mimetic               latest    6ebbcff2fcfb   11 days ago      7.1GB
ubuntu                latest    58db3edaf2be   7 weeks ago      77.8MB


How can I execute my Docker image?

Executing the Docker image consists of creating a container and then running it:

$> docker run -i -v `pwd`:/mnt/outside -t --name fft_container fft_image bash

root@790f7e070ddc:/opt/scripts $> python3 computeFFT.py /mnt/outside/data.txt

input: 
 [1, 2, 3, 4, 1, 0, 1, 2, 3, 4, 0]
output: 
 [[21.        +0.j        ]
 [ 1.67013311-0.12398895j]
 [-9.05533691-2.31224412j]
 [-1.22427554-0.05535305j]
 [ 2.11978549-3.66986085j]
 [ 1.48969385-1.77966606j]
 [ 1.48969385+1.77966606j]
 [ 2.11978549+3.66986085j]
 [-1.22427554+0.05535305j]
 [-9.05533691+2.31224412j]
 [ 1.67013311+0.12398895j]]

The command above uses the Docker image fft_image in order to create the new container called fft_container. The current working directory is shared with the Docker container to exchange the data (file data.txt stored in the current working directory of hosting system) between the hosting system and the Docker container. The container is interactive (shares the control of keyboard and display output). The command bash offers a console for the user.


I want to send my Docker image to somebody.

You need to first export the image into TAR file:

$> docker image save fft_image > my_product.tar


I need to test some application downloaded from the web but I do not have enough privileges.

Create a simple Dockerfile:

# Set the base image to Ubuntu
FROM ubuntu:22.04
ADD <your application> /opt/

Build the Docker image and create the container:

$> docker build -t my_env_image .
$> docker run -i  -t --name my_env_container my_env_image bash

After entering the Docker container, you can additionally install any important packages (your are an administrator) and run the application.


I did some changes in the container and I want to save them for later use.

After making some changes in your running container (you may find some mistake in your computeFFT.py script and amend it) you can save these changes:

$> docker commit fft_container fft_image_v2


I want to use GPU card.

If you have a server with several GPU cards, you can assign the third GPU device to your Docker container in the following way:

$> docker run --shm-size=10gb --gpus '"device=3"' -i -v `pwd`:/mnt/data -t --name fft_container fft_image bash


Do I always need to build Docker image from scratch?

Definitely not. There are several already prepared Docker images that can be directly used. Below, you can find the creation of a simple web server running on port 8080 on hosting computer:

First, create this Docker file:

FROM nginx
ADD <your html data to be published> /usr/share/nginx/html

Second, build the image:

$> docker build -t my_web_server_image .

Third, run the container:

$> docker run --name my_web_server_container -d -p 8080:80 my_web_server_image


How shall I list the running Docker containters?

$> docker container ls


How shall I stop/kill the Docker container?

$> docker container kill <container id>