Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Docker

Docker is an open-source software for automating and encapsulating the deployment of applications inside software containers.

It works with images and container, the main difference between this two is:

  • An image is chunk of memory allocated to store an OS and the relative applicaiton data
  • A container instead is a running image

To visualise how many images you have in your system run:

docker images

System images

An image becomes a container when you execute it. Check the images that are present in your system after the build docker images, the command to build an image is:

docker build <DirectoryWhereTheDockerfileIsLocated>

First create a docker file, which is a file named Dockerfile with a content similar to the followng script snippet (depending on your esigence) on which will be build a docker image that will become a container once is running.

# Start from the official Ubuntu official image in DockeHub (last LTS version)
FROM ubuntu:latest    

# Setting the environment variable 
ENV DEBIAN_FRONTEND=noninteractive

# Install any extra things we might need
RUN apt-get update \
	&& apt-get install -y micro curl sudo bc python3 

# Create a new user called of-user
RUN useradd --user-group --create-home --shell /bin/bash of-user ;\
	echo "of-user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Install OpenFOAM v2112 (without ParaView) including configuring for use by user=of-user plus an extra environment variable to make OpenMPI play nice
RUN curl -s https://dl.openfoam.com/add-debian-repo.sh | bash ;\
	apt-get install -y openfoam2112 ;\
	rm -rf /var/lib/apt/lists/* ;\
	echo "source /usr/lib/openfoam/openfoam2112/etc/bashrc" >> ~of-user/.bashrc ;\
	echo "export OMPI_MCA_btl_vader_single_copy_mechanism=none" >> ~of-user/.bashrc;  # By setting OMPI_MCA_btl_vader_single_copy_mechanism to none, you are disabling a specific feature or mechanism called "vader single copy mechanism" used by Open MPI for efficient communication between processes.

#   Set the default container user to of-user
USER of-user

#ENTRYPOINT ["/data/run.sh"]

To build the image from the Dockerfile use:

docker build -t <nameOfTheImage> <DockerfileDirectory>

Running the container

Running the container means that the docker file will be read and executed, the execution can be modified by the flags that follows the utility such as:

docker run -ti -–rm <imageID>

Here’s some more info on the flags:

  • ti → make you access to the terminal
  • rm → remove the container once you exit
  • d → (deamon) run the container in detached mode (in the background)
  • p 80:80 → port exposition, the local OS can connect to the docker image via port 80

To see if the container is running check:

docker ps

Then to start and/or stop the container run the following self explatory commands:

docker start <imageID>
docker stop <imageID>

Mount a file system directory inside a docker container

Do not store your simulation data in the container. Instead let’s give our container access to just a little bit of our local filesystem. From your project directory in a Windows environment, run:

docker run -ti --rm -v ${PWD}:/data -w /data <imageID>

By adding the -v option we’re asking Docker to mount our current working directory ($PWD) as /data in the container. We’ve also added the -w switch to tell Docker that we’d like to be in /data when the container starts.

Creating an image from a container

You can convert a container into an image by using the command

docker commit 

Delete container

The following command will delete the container:

docker ps			  	   # To visualise them
docker rmi <imageID>

Or you can delete all of them piping two commands:

docker rm -vf $(docker ps -aq)

Delete images

To delete the images that does not run a container above them run;

docker images  				# To visualise them
docker image rm <imageID>

Or you can delete all of them piping two commands:

docker rmi -f $(docker images -aq)

Reclaim space from machine

Docker saves container chaces on var/lib/docker, this can clog the machine disk if there is not enough storage available. A quick solution is to run the following command to clean the system cache:

docker system prune -a -f