How could I limit memory and cpu usage in Docker globally - docker

I'm using Docker with Ubuntu in a development environment, but I noticed that docker can use all the resources of the host machine, is there any way I can limit this without having to configure each of the containers?
I tried to configure the docker daemon.json example:
{
"cpus": 1,
"memory": "4096m"
}

Related

Increase docker engine memory on Unix

I run a docker build on my unix build machine and therefore I need more than 2GB Memory (default value of docker engine). I got the build working on my Mac with the docker Desktop UI in the settings as you can see it in the image.
How is this possible in Unix?
If you want to increase the allocated memory and CPU while running a container you can try this:
docker run -it --cpus="2" --memory="4096m" ubuntu /bin/bash
You can also do the same while deploying services using docker-compose
More infos:
https://docs.docker.com/config/containers/resource_constraints/
How to specify Memory & CPU limit in docker compose version 3

Why minikube runs as a container itself?

While playing around with Docker and orchestration (kubernetes) I had to install and use minikube to create a simple sandbox environment. At the beginning I thought that minikube installs some kind of VM and run the "minified" kubernetes environment inside the same, however, after the installation listing my local Docker running containers I found minikube running as a container!!
Why minikube itself run as a Docker container? and how can it runs other containers?
Experimental Docker support looks to have been added in minikube 1.7.0, and started becoming the default runtime in minikube 1.9.0. As I'm writing this, current is 1.15.1.
The minikube documentation on the "docker" driver notes, particularly on a native-Linux host, there is not an intermediate virtual machine: if you can run Kubernetes in a container, it can use the entire host system's resources without special configuration or partitioning. The previous minikube-on-VirtualBox installation required preallocating memory and disk to the VM, and it was easy to get those settings wrong. Even on non-Linux hosts, if you're running Docker Desktop, sharing its hidden Linux VM can improve resource utilization, and you don't need to decide to allocate exactly 2 GB RAM to Docker Desktop and exactly 4 GB to the minikube VM.
For a long time it's been possible, but discouraged, to run a separate Docker daemon inside a Docker container; similarly, it's possible, but usually discouraged, to run a multi-process init system in a container. If you do both of these things then you can have the core Kubernetes components (etcd, apiserver, kubelet, ...) inside a single container pretending to be a Kubernetes node. It also helps here that Kubernetes already knows how to pull Docker images, which minimizes some of the confusing issues with running Docker in Docker.

How do I link 2 containers running in a AWS ECS task

I am new to ECS and I am trying to deploy a couple of containers in a ECS task using Fargate.
I have 1 container running that uses Angular2 and is running on nginx, the other container is the backend and is running on Springboot and uses the port 42048.
I am using the awsvpc network with Fargate and I have to do it that way.
The Angular app communicates with the backend using localhost:42048/some_url and it works fine in my local docker but in AWS the front-end doesn't find the backend. Currently I have my ports mapped with 80 for the front end and 42048 for the backend and the front-end when deployed locally was able to find the backend as localhost:42048
Any help would be appreciated. Thank you
linking is not allowed in AWSVPC.
You can do linking only in network mode when its set to bridge.
links
Type: string array
Required: no
The link parameter allows containers to communicate with each other
without the need for port mappings. Only supported if the network
mode of a task definition is set to bridge. The name:internalName
construct is analogous to name:alias in Docker links. Up to 255
letters (uppercase and lowercase), numbers, hyphens, and underscores
are allowed. For more information about linking Docker containers, go
to
https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/.
This parameter maps to Links in the Create a container section of the
Docker Remote API and the --link option to docker run.
Note
This parameter is not supported for Windows containers or tasks using the awsvpc network mode.
Important
Containers that are collocated on a single container instance may be able to communicate with each other without requiring links or host port mappings. Network isolation is achieved on the container instance using security groups and VPC settings.
task_definition_parameters
In network mode, you have to define two containers in the same task definition and then mentioned the name of the container in the link.
And then Mentioned the name of backend container in frontend container.
With Fargate, If you want to access your backend using localhost:42048, then you can try configuring your Frontend and Backend in the same Task definition. While deploying the task, all the containers defined in the same task definition would run in the same underlying host and we can access it using localhost.
Remember that Fargate storage is ephemeral and your backend shouldn't maintain application state in the container.
...
"containerDefinitions": [
{
"name": "frontend",
"image": "my-repo/angularapp",
"cpu": 256,
"memory": 1024,
"essential": true,
"portMappings": [ {
"containerPort": 8080,
"hostPort": 8080
}
]
},
{
"name": "backend",
"image": "my-repo/springboot",
"cpu": 256,
"memory": 1024,
"essential": true,
"portMappings": [ {
"containerPort": 42048,
"hostPort": 42048
}
]
}
]
...
But I'm afraid this approach isn't suitable for production grade.

How docker allocate memory to the process in container?

Docker first initializes a container and then execute the program you want. I wonder how docker manages the memory address of container and the program in it.
Docker does not allocate memory, it's the OS that manages the resources used by programs. Docker (internally) uses cgroups which is a kernel service. The reason that ps command (on the host) won't show up processes running in a container, is that containers run in different cgroups which are isolated from each other.
Rather than worrying about the docker memory, you would need to look at the underlying host (VM/instance) where you are running the docker container. The number of containers is determined by a number of factors including what your app is that runs on the container.
See here for the limits that you can run into Is there a maximum number of containers running on a Docker host?

How many CPUs does a docker container use?

Lets say I am running a multiprocessing service inside a docker container spawning multiple processes, would docker use all/multiple cores/CPUs of the host or just one?
As Charles mentions, by default all can be used, or you can limit it per container using the --cpuset-cpus parameter.
docker run --cpuset-cpus="0-2" myapp:latest
That would restrict the container to 3 CPU's (0, 1, and 2). See the docker run docs for more details.
The preferred way to limit CPU usage of containers is with a fractional limit on CPUs:
docker run --cpus 2.5 myapp:latest
That would limit your container to 2.5 cores on the host.
Lastly, if you run docker inside of a VM, including Docker for Mac, Docker for Windows, and docker-machine, those VM's will have a CPU limit separate from your laptop itself. Docker runs inside of that VM and will use all the resources given to the VM itself. E.g. with Docker for Mac you have the following menu:
Maybe your host VM has only one core by default. Therefore you should increase your VM cpu-count first and then use --cpuset-cpus option to increase your docker cores. You can remove docker default VM using the following command then you can create another VM with optional cpu-count and memory size.:
docker-machine rm default
docker-machine create -d virtualbox --virtualbox-cpu-count=8 --virtualbox-memory=4096 --virtualbox-disk-size=50000 default
After this step you can specify number of cores before running your image. this command will use 4 cores of total 8 cores.
docker run -it --cpuset-cpus="0-3" your_image_name
Then you can check number of available core in your image using this command:
nproc

Resources