How to add swap to docker?

Posted September 20, 2018 10.7k views

Guys hi,

I am using Ubuntu Docker 17.12.0ce on 16.04 and have php app running.
It has 2 containers: prj_apache_1 and prj1_mysql_1

Problem is that mysql container is frequently crushing. Solution is to add swap space.
But most of explanation are for “regular” server not dockerized.

I do not have root@myserver:~#,

i have root@docker-s-1vcpu-1gb-nyc3-01:~#,
also i can “get in” apache using docker exec -ti prj_apache_1 bash
and mysql docker exec -ti prj_mysql_1 bash

so my question inside which containers do i run this commands: ?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hey friend!

Docker is going to use the host memory/swap, so you can just add swap to the VM or increase memory. Note that swap is kind of a bad thing if you’re frequently going OOM. You want swap to do less important things than frequently act on behalf of memory missing and needed. Increasing memory in the VM or further optimizing the MySQL configuration (or even the app that is using MySQL) will continue to be superior solutions.