Download the PHP package diego-ninja/docker without Composer
On this page you can find all versions of the php package diego-ninja/docker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download diego-ninja/docker
More information about diego-ninja/docker
Files in diego-ninja/docker
Package docker
Short Description Run and manage docker containers in your php code
License MIT
Homepage https://github.com/diego-ninja/docker
Informations about the package docker
Manage docker containers with PHP
This package provides a nice way to start docker containers and execute commands on them.
``
Differences
This package is a fork of spatie/docker with some adaptations to fit my particular needs.
As major differences, this package drops php 7.x support and gives the possibility to use the library with already created containers. Also, all methods that return a Symfony process object now accept an $async flag to run the process asynchronously, using the start method instead of run method. Finally, the start method now accepts an optional callable, if present the callable will be called continuously as the container runs, this callable will take the Symfony Process object as its only parameter.
`
Installation
You can install the package via composer:
Usage
You can get an instance of a docker container using
By default, the container will be daemonized, and it will be cleaned up after it exists.
Customizing the docker container
Prevent daemonization
If you don't want your docker being daemonized, call doNotDaemonize
.
Prevent automatic clean up
If you don't want your docker being cleaned up after it exists, call doNotCleanUpAfterExit
.
Privileged
If you want your docker being privileged, call privileged
.
Custom shell
If the bash
shell is not available in your docker image, you can specify an alternative shell.
Naming the container
You can name the container by passing the name as the second argument to the constructor.
Alternatively, use the name
method.
Mapping ports
You can map ports between the host machine and the docker container using the mapPort
method. To map multiple ports, just call mapPort
multiple times.
Environment variables
You can set environment variables using the setEnvironmentVariable
method. To add multiple arguments, just call setEnvironmentVariable
multiple times.
Setting Volumes
You can set volumes using the setVolume
method. To add multiple arguments, just call setVolume
multiple times.
Setting Labels
You can set labels using the setLabel
method. To add multiple arguments, just call setLabel
multiple times.
Adding Commands
You can add commands using the setCommands
method.
These commands will be placed at the end of to the docker run
command.
Add optional arguments
If you want to add optional arguments to the docker run
command, use setOptionalArgs
method:
These arguments will be placed after docker run
immediately.
Automatically stopping the container after PHP exists
When using this package in a testing environment, it can be handy that the docker container is stopped after __destruct
is called on it (mostly this will happen when the PHP script ends). You can enable this behaviour with the stopOnDestruct
method.
Attaching a network to the container
If you want to attach the container to a docker network, use network
method:
Specify a remote docker host for execution
You can set the host used for executing the container. The docker
command line accepts a daemon socket string. To connect to a remote docker host via ssh, use the syntax ssh://username@hostname
. Note that the proper SSH keys will already need to be configured for this work.
Specify an alternative command to execute
Upon startup of a container, docker will execute the command defined within the container. The command
method gives the ability to override to default command to run within the container.
Getting the start command string
You can get the string that will be executed when a container is started with the getStartCommand
function
Changing the start command timeout
You can change the timeout for the start command with the setStartCommandTimeout
function (the default is 60s).
Available methods on the docker container instance
Executing a command
To execute a command on the container, use the execute
method.
You can execute multiple command in one go by passing an array.
To change the process timeout you can pass a third parameter to the execute
method (the default is 60s).
The execute method returns an instance of Symfony/Process
.
You can check if your command ran successfully using the isSuccessful
$method
You can get to the output using getOutput()
. If the command did not run successfully, you can use getErrorOutput()
. For more information on how to work with a Process
head over to the Symfony docs.
Installing a public key
If you cant to connect to your container instance via SSH, you probably want to add a public key to it.
This can be done using the addPublicKey
method.
It is assumed that the authorized_keys
file is located in at /root/.ssh/authorized_keys
. If this is not the case, you can specify the path of that file as a second parameter.
Note that in order to be able to connect via SSH, you should set up a SSH server in your dockerfile
. Take a look at the dockerfile
in the tests of this package for an example.
Adding files to your instance
Files can be added to an instance with addFiles
.
Get the docker inspect information
The json decoded array from the docker inspect command can be retrieved with inspect
.
Adding other functions on the docker instance
The Ninja\Docker\ContainerInstance
class is macroable. This means you can add extra functions to it.
``
Testing
Before running the tests for the first time, you must build the spatie/docker
container with:
`
Next, you can run the tests with:
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
Credits
- Ruben Van Assche
- Freek Van der Herten
- Diego Rin Martin
- All Contributors
License
The MIT License (MIT). Please see License File for more information.