Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Containers are ubiquitous in the software industry, so I would recommend taking the time to read the official Docker documentation (https://docs.docker.com/get-started/). However, if you're in a hurry and just want to get a GPU-powered python environment up and running asap, the guide below should help you get started.


Step-by-step guide

1 - Making sure you have access to docker

By default (for security reasons), you should not have access to docker, even if you have access to the host machine. To check whether you have access, log into the host machine and run any docker command, e.g.:

...

If you get an error message complaining about access rights, it means that you are not part of the docker group on the machine. Ask your supervisor to relay a request to one of the engineers at IDI, who should be able to grant you the necessary privileges.


2 - The Dockerfile - building an image

Our end goal is to make a container from which we can run our own code. However, to achieve this, we need to create something called aimage first. An image is a prototype of a container; it serves as a premade snapshot that can be used to spawn any number of containers. An image is created from something called a Dockerfile, which in its most basic form is just a list of prerequisites you want installed and commands you want to run before every startup. The example below should be a nice starting point.

...

Code Block
languagebash
themeMidnight
titleChange working directory and build image
collapsetrue
cd ~/docker/myproject 
docker build -t <image_name> . 

 

Note the dot the ‘.’ (dot) at the end of the command; don't forget it as it tells docker where to look for a Dockerfile. <image_name> is a user-specified name used to identify the created image. By convention, since all images created on the machine is stored in one place, it is common to include your username in the image name; e.g.: olanorm/testproject

...

It prints a list of all the available images on the current machine. You should find your newly created one at the top.

 

3 - Running the container

Once the image is successfully built we can run a container from it. The docker run command contains many different options that you might want to explore through the official reference. However, to keep things simple, here’s a command for running a container capable of providing a jupyter notebook that can be accessed from the outside:

...

The command should give you a output similar to this: 

 

4 - Connecting to jupyter

Normally, when using jupyter notebook-like apps on a computer, we just run the server and access it through a web browser. However, since the server process is running on a different machine that most likely does not expose the serving port on the network, we need some extra magic to make it accessible. To achieve this, open a new SSH connection to the server that is running your docker container with the following command:

...

This should return a list of all running notebooks in your container (just the one started by the root process unless you’ve done anything else). Simply copy the token part (highlighted in the image below) of the URL in the notebook list, paste it into the password prompt in your web browser, and you should be good to go.

 

5 - Further steps

Below are some extra tips and commands that might come in handy. 

 

Code Block
languagebash
themeMidnight
titleExecuting a command in a currently running container
collapsetrue
docker exec <container name> <command arguments>

We ran into this command when accessing jupyter in the section above. The exec command allows you to execute arbitrary commands in a running container, e.g. a python script.

 

Code Block
languagebash
themeMidnight
titleGetting a shell in a running container
collapsetrue
docker exec -it <container name> bash

This returns a shell that gives you terminal level access to your container. It might be highly useful when setting up things in a container that was not already fixed by the docker script, or for just getting more familiar with the environment you’ve created. Note the –it switches used; they make the session interactive, which means that stdin from your current shell will be hooked up to the shell granted by the container. 

 

Code Block
languagebash
themeMidnight
titleMoving files in and out of a container
collapsetrue
docker cp <container_name> path/to/src/file path/to/dest/file

This command allows copying single files to or from a container. Usage-wise it is more or less identical to the normal cp command.

 

Code Block
languagebash
themeMidnight
titleMounting external directories
collapsetrue
docker run -v /host/path/to/dir:/container/mount/location --name <container...

If you want to keep whole directories of files synched or a permanent place to store output, then the copy command is not going to cut it. Fortunately, docker comes with the option to mount whole directories from the host machine into the container. You can do this by adding an extra argument to the docker run command, specifying the source directory root and the location you want it to show up in the container. While it is often convenient to mount your own home directory, this is in my experience currently not feasible on most docker setups we got because the ticket used to access your home folder is not transferred to the docker program. One alternative is to use the host machine’s /tmp folder, but this is naturally a bad idea if you're looking for permanent storage. Therefore, most machines are set up with a /data folder, where users of the system can get their own subfolder which they can mount into the containers they spawn.

 

 

Security issues 

On most NTNU systems docker runs as root, meaning that anything you do through docker will be run as root – not just in your container but also when interacting with the host machine. Please keep this in mind when working and be responsible / careful. Misuse of trust may lead to exclusion.





 

 

Info

Feel  Please feel free to help us provide a better wiki by sharing share your skills and knowledge if you think some of it could be nice to include in this wiki. To contribute you can send an email to: joakim.g.antonsen@ntnu.no 

Content by Label
showLabelsfalse
max5
spacesailab
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel = "docker" and type = "page" and space = "ailab"
labelsDocker

...