Contents

Docker Run

Docker Run options

-v : Share a file system

Mount host directory

1
docker run -v <host directory path>:<container directory path>

Dockerfile:

1
2
FROM ubuntu:latest
RUN mkdir new_dir
1
docker run -it -v ~/Desktop/monted_folder:/new_dir <container> bash

If new_dir directory does not exist in the container, docker automatically create it.

-u : Set access privilege to the files

1
docker run -u <user id>:<group id>

At host, check user and group id.

1
2
3
4
% id -u
501
% id -g
20

Create a folder on the host.

1
mkdir ~/Desktop/mounted_folder

Dockerfile

1
2
FROM ubuntu:latest
RUN mkdir created_in_Dockerfile

Docker build

1
docker build .

Run a container with -u option.

1
docker run -it -u $(id -u):$(id -g) -v ~/Desktop/mounted_folder:/created_in_run 87496285f4d1 bash

At the container,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
% docker start 87496285f4d1     
% docker attach 87496285f4d1 

I have no name!@87496285f4d1: $ ls -alrt
total 60
drwxr-xr-x   2 root root    4096 Apr 18  2022 home
drwxr-xr-x   2 root root    4096 Apr 18  2022 boot
lrwxrwxrwx   1 root root       8 Jun 24 02:06 sbin -> usr/sbin
lrwxrwxrwx   1 root root       7 Jun 24 02:06 lib -> usr/lib
lrwxrwxrwx   1 root root       7 Jun 24 02:06 bin -> usr/bin
drwxr-xr-x  11 root root    4096 Jun 24 02:06 usr
drwxr-xr-x   2 root root    4096 Jun 24 02:06 srv
drwxr-xr-x   2 root root    4096 Jun 24 02:06 opt
drwxr-xr-x   2 root root    4096 Jun 24 02:06 mnt
drwxr-xr-x   2 root root    4096 Jun 24 02:06 media
drwxr-xr-x  11 root root    4096 Jun 24 02:31 var
drwx------   2 root root    4096 Jun 24 02:31 root
drwxrwxrwt   2 root root    4096 Jun 24 02:31 tmp
drwxr-xr-x   5 root root    4096 Jun 24 02:31 run
drwxr-xr-x   2  501 dialout   64 Aug  4 08:04 created_in_run
drwxr-xr-x   2 root root    4096 Aug  5 04:16 created_in_Dockerfile
drwxr-xr-x   1 root root    4096 Aug  5 04:20 etc
-rwxr-xr-x   1 root root       0 Aug  5 04:20 .dockerenv
drwxr-xr-x   1 root root    4096 Aug  5 04:20 ..
drwxr-xr-x   1 root root    4096 Aug  5 04:20 .
dr-xr-xr-x  13 root root       0 Aug  5 04:29 sys
dr-xr-xr-x 207 root root       0 Aug  5 04:29 proc
drwxr-xr-x   5 root root     360 Aug  5 04:29 dev

-p : Publish a port

Connect host port to container port
(Publish a container port to a host port)

1
docker run -p <host_port>:<container_port>
1
2
docker run -it -p 8888:8888 --rm jupyter/datascience-notebook bash
jupyter notebook

Limit a computer resources

1
2
3
4
5
6
7
8
% sysctl -n hw.physicalcpu_max
10
% sysctl -n hw.logicalcpu_max     
10
% sysctl sysctl hw.memsize
sysctl.proc_translated: 1
sysctl.proc_native: 1
hw.memsize: 17179869184
  • –cpus : Limit the number of CPU that container can access
  • –memory: Limit the size of memory that container can access
1
docker run -it --rm --cpus 4 --memory 2g ubuntu bash

docker inspect

Check with docker inspect

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
% docker inspect b37bb2217b8a | grep -i cpu
 "CpuShares": 0,
            "NanoCpus": 4000000000,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "CpuCount": 0,
            "CpuPercent": 0,
1
2
3
4
5
6
7
8
% docker inspect b37bb2217b8a | grep -i memory
            "Memory": 2147483648,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 4294967296,
            "MemorySwappiness": null,