The first minimal thing to understand are images. Images provide you some utilities, there are images for mysql, php, nginx and almost everything.
Here, we will use the official PHP image. Base images created by maintainers can be found in the docker hub.
When you decide to use an image, this one will be downloaded locally on your host. You don't want to download it everytime you want to use it.
docker pull php:7
Now if you run
docker images
You'll see a line
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
php 7 <IMAGE_ID> X seconds ago X MB
We have a local copy of the php 7 image, we can now use it.
A container is an instance of an image. Imagine that an image is a Class, and your container an Instance of this class. You can create a container from an image with
You can either specify a command at the end or use the default command of the image.
For PHP, the default command is php -a
(launch the interactive shell)
docker create -i --name php-container php php -a
Tip: Specifying a name will allow you to run more easily your container later.
Tip: The php -a
command being interactive, you have to specify the -i
flag to inform docker to keep the standard input (STDIN) opened.
Now if you run
docker ps -a
You'll see a line
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e59ce738fb34 php "php -a" 5 seconds ago Created php-container
The container has been created. But it's not running. So the status is only "Created"
You can start a container with
docker start <CONTAINER_NAME>
If you decide to run again docker ps -a
, you'll see a little change
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e59ce738fb34 php "php -a" 10 minutes ago Up 3 seconds php-container
The container has been started and is running but we can do anything, why? The first option we can use is �--attach=true
� (or -a
) which attach STDOUT/STDERR and forward signals. We need to stop the container before restarting it.
docker stop php-container
docker start -a php-container
We can see that the command php -a
is well running but doesn't use what we type. This is again because the php -a
is interactive (it requires input from the user). At the container creation, we asked docker to keep the STDIN opened. Now we have to specify that we want to map terminal STDIN to the container STDIN by adding the --interactive=true
(or -i
option)
First you have to kill your terminal window. Then stop the container and restart it.
docker stop php-container
docker start -a -i php-container
Now you can run your container every time you need and use it, and exit it with Ctrl+d.
docker run -i php php -a
You don't need to specify the -a
flag to run command as it's the default value.
Récap
docker create - Crée un container à partir d'une image. Possibilité de lancer une commande custom
docker start - Démarre un container
docker exec - Lance une commande dans un container lancé (sinon fail)
docker run - Crée un container à partir d'une image et le démarre. Possibilité de lancer une commande custom
Executing the PHP shell is nice, but what if we want more ? In this part, we will run a Symfony2 application in a container.
docker create -v /Users/matthieuauger/www/dsymfony:/mnt -w /mnt -p 8000:8000 php:7 bin/console server:run 0.0.0.0:8000
version: '2' services: server: image: php:7 working_dir: /mnt volumes: - .:/mnt ports: - '8000:8000' command: bin/console server:run 0.0.0.0:8000
===========================
Docker can run on virtual machines. To create a new virtual machine, you can use docker-machine
docker-machine create --driver virtualbox docker-vm
Docker uses environment variables to know on which virtual machine it should run. You can see the required variables by running
docker-machine env docker-vm
In order to effectively use it, you can eval the output of this command.
eval "$(docker-machine env docker-vm)"
===========================
Run a nginx container
docker run -ti --rm nginx
or
docker run -tid nginx
- -t : Use a TTY
- -i : Is interactive
- --rm : The container is deleted after the run
- -d : Run in backgrounad
============================
Dev stack.
Web image: busybox volumes: .:/var/www/app /vendor