Docker is a technology that makes it easy to build an environment that has been introduced by many IT development companies. A container created with Docker has a relationship with the host (own PC or cloud such as AWS), such as reading files and connecting to the container from the host's server. Run docker run with options to specify the conditions for those relationships. This time, I will introduce options for conditioning the relationship between the container and the host.
Making the host file behave as if it also exists in the container is called mounting. The container should be small in size so that someone can use it or run it easily, so try not to put files in the container itself as much as possible. For example, if you want to keep the code file on the host and load the host code file from the container, you say that the code file is mounted on the container.
docker run -it -v (Host path):(Container path) (DockerImage name or ID) bash
Example) docker run -it -v /Desktop/sample_dir:/new_dir (Docker Image ID) bash
=>Container new_A sample that exists on the host Desktop in a directory called dir_A directory called dir will be mounted.
(New in container_dirというディレクトリが存在しない場合は、New in container_A directory called dir is created automatically.)
When mounting a file on a host with a container, if you do not specify a user ID or group ID, the container will be able to access with root privileges. If you have root privileges on the container side, you can create and edit host files from the container, and much more. If you do not want to have root authority, specify the user ID and group ID and mount it.
docker run -it -u (User ID):(Group ID)
Example) docker run -it -u $(id -u):$(id -g) -v /Desktop/sample_dir:/new_dir (Docker Image ID) bash
=>A container with the host files mounted is created with the user ID and group ID specified.
A port is a specific location on the server. For example, if your server has multiple services, specify the port because you cannot tell which service you want to connect to with just the IP address. If you want to use the created container as one Web service, you need to connect the container port and the host port.
docker run -it -p (Host port):(Container port)
Example) docker run -it -p 3000:3000 rails bash
=>A container is created from a Docker Image called rails and connects to the default port 3000 of rails, so localhost on the host side:You can access rails by accessing 3000.
When the container runs, it uses the host's CPU and memory. For example, if one container uses the CPU and memory to the limit, the memory may be exhausted and the entire system may go down. To avoid such risks, set CPU and memory limits for each container.
docker run -it --cpus (Number of logical cores) --memory (Memory capacity)
Example) docker run -it --cpus 4 --memory 2g ubuntu bash
=>A container is created from Docker Image called ubuntu, and the upper limit of the logical core of the CPU that can use this container is 4, and the upper limit of memory is 2 giga.
(When you want to check the CPU and memory of the container,"docker inspect (Container ID)"Enter the. )
If you want to know the CPU and memory of your own PC, you can check it by entering the following in the terminal.
sysctl -n hw.logicalcpu_max => CPU (number of logical cores)
sysctl hw.memsize => memory (byte)
Udemy
Kameleon Lecturer "Docker course taught by US AI developers from scratch"
https://www.udemy.com/share/103aTRAEAdd1pTTHoC/
There is a charge, but it was very easy for me as a beginner to understand.
We hope that this post will help beginners review.
Recommended Posts