Docker Tips
TyeYeah Lv4

Follow this to get a good command of Docker usage.

Basic Commands

The Reference documentation | Docker Docs gives the most complete guidance.

But here, only swift commnads listed which are sufficient for general using.

BTW I documented how to run a mysql docker container in First how-to-do note, including initial steps.

Run the Container

Docker can be used as microservice, while for non-professional developers, we use it as lightweight virtual machine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ docker run -itd image_name /bin/bash
# "-itd" means to run interactive /bin/bash, on tty, in background
# which makes container stay alive and wait for attaching.
# can also add "--name container_name" to name it, or "-p host_port:guest_port" to set port forwarding

# start the contaier
$ docker start container_name
# "attach" to it, while not `docker attach`
$ docker exec -it container_name /bin/bash
# stop the container
$ docker stop container_name

# attention, if you attach to container...
$ docker attach container_name
# dont `exit` cuz it will stop the container, try "Ctrl+P+Q"

Migrate Images and Containers

To migrate images, use save and load.
First save the image:

1
2
3
4
5
$ docker save -o image.tar image_id_or_image_name
# or
$ docker save image_id_or_image_name > image.tar
# to pack many images together
$ docker save docker save -o images.tar image_1 image_2

Then load the image:

1
2
3
$ docker load -i image.tar
# or
$ docker load < image.tar

New image name stays the same with the old one.

To migrate containers, use export and import.
First export the container:

1
2
3
$ docker export container_id_or_container_name > container.tar
# or
$ docker export --output="container.tar" container_id_or_container_name

Then import the container:

1
$ docker import - new_container_name < container.tar

You see, you can name the new image this step.

Get GPU support from Nvidia

Check this out: Installing the NVIDIA Container Toolkit for updated installation.

First to figure out the differences between: nvidia-docker, nvidia-docker2 and nvidia-container-toolkits.

The nvidia-docker and nvidia-docker2 are archived(Dec 6, 2023) old repos.
In nvidia-docker age it can be runned by

1
$ nvidia-docker run XXX

In nvidia-docker2 the above nvidia-docker command is equal to:

1
$ docker run --runtime=nvidia XXX 

The relationship between these methods:

1
Backward compatibility To help transitioning code from 1.0 to 2.0, a bash script is provided in /usr/bin/nvidia-docker for backward compatibility. It will automatically inject the --runtime=nvidia argument and convert NV_GPU to NVIDIA_VISIBLE_DEVICES.

Up to now, NVIDIA Container Toolkit is the latest solution, as the sample usage goes like:

1
$ sudo docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi

Then output panel is like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 535.86.10 Driver Version: 535.86.10 CUDA Version: 12.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 34C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+

The --runtime=nvidia can be omitted, while the --gpus all is essential.
The ubuntu image here can be replaced with images listed on Nvidia CUDA Linux Container Image Supported tags.

Then process as the doc goes.

Powered by Hexo & Theme Keep
Total words 135.7k