Symfony Application using Docker

Hicham BEN KACHOUD
5 min readNov 25, 2020

A lot of developers (including me :D) used to say the famous phrase “it works on my computer”, when they deployed their applications.

The solution is to make both environments, development & production, have the same configuration. Here Docker comes to save a developer’s life :D.

1- What is Docker?

Docker is a tool for running applications in an isolated environment by using containers.

Containers: are an abstraction at the app layer that packages code and dependencies together, it allows a developer to focus in the domain of the application and be assured that the application will run on any other Linux machine.

This article is not an introduction to docker, but if you would like a new article about docker, tell me in the comment section or refer to the docker website: here.

2- Make sure you installed Docker:

Check this link to install docker based on your OS.

For linux users, you can use those commands to install docker:

$ sudo apt-get update$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -$ sudo apt-key fingerprint 0EBFCD88$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

After executing all commands, you can check if docker is correctly installed using following:

$ sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

Docker installed correctly

3- What we need to create our symfony application:

To create our application, we need to get: PHP, mySQL, Symfony.

For creating a symfony application, I will also use syfmony cli.

4- Create Dockerfile & docker-compose.yaml:

We are going to create a new empty folder for our application:

Empty folder

Create a file with name: Dockerfile. This file contains many steps of how to create our image.

FROM php:7.4-fpm-alpineRUN apk --no-cache update && apk --no-cache add bashRUN docker-php-ext-install pdo_mysql#installer composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && php composer-setup.php && php -r "unlink('composer-setup.php');" && mv composer.phar /usr/local/bin/composer
#installer cli symfony
RUN wget https://get.symfony.com/cli/installer -O - | bash && mv /root/.symfony/bin/symfony /usr/local/bin/symfony
WORKDIR /var/www/html

We used an alpine linux image, for reducing image size. We installed composer, some php dependencies and symfony CLI.

You can check the docker hub (here) for listing images, i used the php:7.4-fpm-alpine, which is a small image php.

WODKDIR mentionne the directory path inside a container.

The second file is docker-compose.yaml. We use this file to configure the containers of which our application is composed.

version: '3.3'services:
php-fpm:
container_name: php-fpm
build: ./
ports:
- 5000:8000
volumes:
- ./:/var/www/html
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8081:80
environment:
- PMA_ARBITRARY=1

It contains 3 containers: php, mysql & phpmyadmin.

We configured the port 5000 locally to match the port of the container 8000.

5- Run docker:

Now, we created the two files, we are going to execute the following command docker-compose up -d in order to start our containers:

docker-compse.yaml

In my case, I already download the images, so it used a cache for building. However, in your case, it will take some minutes to download all specified data.

After the command is finished, execute the command: docker ps to list all running containers.

running containers

As you can see in the image above, we have 3 containers (which are specified in docker-compose.yaml file).

Let’s try to go inside a container. Use the following command with a PHP-FPM container ID:

docker exec

As you can see, we have two files created in our folder inside a container (this because we specified a volume that would share data between Host & containers or between containers).

6- Create symfony application:

After you go into a container, you can execute the following command to create your first symfony application: symfony new — full blog

symfony project

After you execute the command, you will get your symfony files. Go inside a blog folder and run symfony server using: symfony server:start -d

Go to http://localhost:8081 to show your phpmyadmin: (use mysql as server and enter the password).

phpmyadmin

Go to http://localhost:5000 to show your symfony homepage:

symfony homepage

Now you have your Symfony application running, you can create a controller or entity as you used to do.

You can generate an entity Post: php bin/console make:entity Post

<?phpnamespace App\Entity;use App\Repository\PostRepository;
use Doctrine\ORM\Mapping as ORM;
/**
*
@ORM\Entity(repositoryClass=PostRepository::class)
*/
class Post
{
/**
*
@ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
*
@ORM\Column(type="string", length=100)
*/
private $title;
/**
*
@ORM\Column(type="text")
*/
private $content;
/**
*
@ORM\Column(type="datetime")
*/
private $createDate;
/**
*
@ORM\Column(type="boolean")
*/
private $enabled;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getCreateDate(): ?\DateTimeInterface
{
return $this->createDate;
}
public function setCreateDate(\DateTimeInterface $createDate): self
{
$this->createDate = $createDate;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
}

Then generate and execute migrations:

php bin/console make:migration & php bin/console doctrine:migrations:migrate

So you will get the following result:

migrations

Conclusion

Congrats! I think you are now able to create a symfony application using docker, and you can make it works on any machine, it will be enough to install docker on the machine without facing any problems.

Github link: https://github.com/hichambenkachoud/symfony-docker

--

--