Compile a php application for CLI in docker and deploy it

Cyril Pereira
2 min readJul 29, 2021

Distribute your Symfony console commands with docker

The idea

I wanted to distribute a Php/Symfony application without install php.

This tutorial will work with every other php framework.

Docker is the perfect solution for that.

Dockerfile

I’m using the image php:7.4.20-zts-alpine3.13 because it’s really tiny and well packaged.

FROM php:7.4.20-zts-alpine3.13LABEL org.opencontainers.image.authors="cyril.pereira@gmail.com"ENV MY_ENV_VAR my_defaul_valueWORKDIR /appCOPY . /appCMD ["/app/bin/console"]

In the image i will copy everything except the files and directories listed in this file you need to have in your root project directory .dockerignore

.git
.gitignore
.dockerignore
README.md
Dockerfile
.env
.DS_Store
/var/cache/*
/var/log/*

Build the image

To build the image you will need to type this command

$ docker build -t myapp .

to save the image after build

$ docker save myapp | gzip > myapp.tar.gz

to load the image on other computer

$ docker load < myapp.tar.gz

You also can push this image on https://hub.docker.com

How to use

To run the application with docker, you will need to load the image one time and then you can do this :

$ docker run --env-file .env -it myapp /app/bin/console app:my-test

Every command you will create in Symfony and the default ones will be available and of course if you type nothing you will have the console list triggered

$ docker run --env-file .env -it myapp 

Deploy

First log in https://hub.docker.com

$ docker login

Then tag your image, you will need to get your image id, to found out use docker ps to get it

$ docker tag 1234567 myapp:1.0

Then push your image

$ docker push myapp:1.0

You will be able now to use the image from hub.docker.com and share it

$ docker run --env-file .env -it funkymed/myapp:1.0

--

--