[Docker] Introduction of basic options (sharing, user, port, CPU / memory) during Docker run

I will introduce the basic options that can be specified during Docker run.

File system sharing

It is important to avoid putting files on the container side as much as possible so that the container is not too large. At such times, use the -v option, which allows you to mount the host's files in a container. The way to write is as follows.

How to specify the -v option

-v <host>:<container>

ex. When mounting the ~ / Desktop / to_container folder on the host to the / host_folder folder on the container side

docker run -it -v ~/Desktop/to_container:/host_folder <image> bash

Access to files [-u]

Sharing the file system allows the container to access the host's file system. Unless otherwise specified, the container will be running as root. It is not good to access the host side as it is, so you need to set the access authority. If you execute the container by specifying your own user id and user group and specifying the execution user, you can create a container so that users other than root privileges can operate inside the container.

How to specify the -u option

-u 501:20 However, it is troublesome to check the user id and user group each time and execute the command, so it is convenient to describe so that the user id and group can be executed and run. -u $(id -u):$(id -g) If you put a linux command in $ (), the command will be executed using the execution result. When you actually run and run bash, you should see the user as shown below. I have no name!@<serial>

The cause is that there is no user such as 501 on the container, so if you create a user appropriately, it will be displayed with the correct user name.

Connect ports

When setting up multiple containers, access is not possible unless the host port and container port are linked. The description method is not much different from the one introduced so far, and is as follows.

How to specify the -p option

-p <host port>: <container port>

Setting computer resource limits

If you do not set the host CPU and memory so that they are not used up, resources may be exhausted. There is an option to set an upper limit to prevent this from happening.

How to specify options

-cpus <number of cores> --memory <memory capacity byte>

ex. Set the upper limit of 2 cores and 4G memory and start the container. docker run --cpus 2 --memory 4g <image> bash

Display container information

You can check the id of the container, the time it was created, the environment variables, the CPU allocated, and the memory. The command is below. docker inspect <container ID>

To the above command| grep -i cpu| grep -i memoryPlease connect and check the settings.

This time is over.

Recommended Posts

[Docker] Introduction of basic options (sharing, user, port, CPU / memory) during Docker run
[Docker] Introduction of basic Docker Instruction
[Docker] Introduction to docker compose Basic summary of docker-compose.yml
Introduction of Docker --Part 1--