docker-api
docker-api copied to clipboard
Need example on how to pass params
Hi! It is not clear how to pass params to match with 'docker run' command: how to pass port mappings, '--shm-size' etc. Please add some examples on it.
@vitamon you may find the docker engine api docs helpful.
It's worth noting that while the docker CLI supports docker run ...
, the engine requires you to first create a container (where you would specify your options), and then start that container.
For example, you could start a mysql container on port 3306 with a shm size of 128MB using the following code:
const { Docker } = require('node-docker-api');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
docker.container
.create({
name: 'mysql', // equivalent to --name=mysql for `docker run`
Image: 'mysql:5.7.19',
Env: ['MYSQL_ROOT_PASSWORD=rootpassword', 'MYSQL_DATABASE=example_database'], // Boilerplate for mysql
HostConfig: {
PortBindings: {
'3306/tcp': [
{
HostIp: '0.0.0.0',
HostPort: '3306' // Bind the container to port 3306 on the host
}
]
},
ShmSize: 128000000 // Set the shm size to 128MB
}
})
.then(container => container.start());
Note: If you do not have the mysql:5.7.19
image locally, you must first pull it with docker pull mysql:5.7.19