Create a Symfony 5 application with Docker(part 1)


This article is the first of a series dedicated to learn how to use docker for PHP and Symfony.

In this lesson, we will create a Symfony application in a docker context, the Symfony application won’t use a database and will be resume as a simple route displaying an html page.

We will try to use tools only inside the docker context : the versions of PHP and composer played will be the docker ones.

The tutorial will be divided in many parts.

In this first chapter, we will learn how to execute a PHP file with docker first and then with docker-compose.

You will need to install docker and docker compose.

Docker installation

To install docker, you can read the following documentation.

Docker-compose installation

You can execute the following command on Linux.

sudo apt install docker-compose

Or if you use Windows or Mac, read the following official documentation :


In your console, execute the following command:

mkdir docker-symfony5cd docker-symfony5touch Dockerfile

  • The first command create a folder,
  • the second move inside
  • and the last create a file.


Inside Dockerfile, we can write our first configuration.

FROM php:7.4-alpine

CMD echo “Hello world”

With the first line, we add an image of PHP (an alpine version), with the second, we use the command (CMD) echo from PHP to display an “Hello world” string.

Build our first image

Then, from /docker-symfony5, in our shell, we can execute the instruction :

docker build -t basic .

“docker build” is the command for building a docker image. The option “-t” is a shortcut for “tag”. The “.” at the end is important, it says that we create the image in the actual folder.

You should obtain a result like:

build a docker image

What does it means? Docker has created an image, the name (tag) of the image is “basic”, we will be able to use it now.

Execute a command

To call a build image, we use

docker run basic

When executing the command in console, you should see an “Hello work” displayed.

In fact “docker run” creates a container to use the image. The images need container to be run.

To list all existing images, you can write

docker image ls

At this step, we have been able to display a string using a container executing a docker image. The image build used another preexisting PHP image (php7.4 Alpine).

Now we will try to go further using a real PHP file and not a simple command for displaying a message.

Another massive improvement is that we will use a webserver and access our “hello world” thanks to an url.

To be able to do that, we need to solve 3 issues:

  • docker must have access to the PHP file
  • docker must launch a webserver able to read the file
  • we need to access by an url to the local of docker

Create the PHP file

First, In our Dockerfile we can remove “CMD echo “Hello world” ( we will use the command in the php file now).

Then, we create an index.php file in our “docker-basic” folder:

When we will run our project, docker will create a container, this container will need to have access to the file. We must say to docker to copy the index.php on the container.

So we add in Dockerfile the following line:

COPY index.php /


We can say that we want to copy all our files in the docker root by using “.” and replace our Dockerfile command by:

COPY . /

Our first objective is accomplished. Docker is able to access the PHP file. The next step explain how to launch a webserver.

Execute the file with the PHP webserver

It’s very easy to launch a weberserver with PHP :

php S index.php

The webserver could be reachable at the url : with the port 80.

On docker, it’s as simple as that, we just need to add the docker CMD instruction to our Dockerfile.

CMD php S index.php

Docker will lauch a local webserver, on it’s port 80.

Actually our Dockerfile is supposed to look like :

FROM php:7.4-alpine

COPY . /
CMD php -S index.php

Our index file is able to be executed by the PHP webserver.

Access the script executed by docker

For displaying our “hello world”, we have to run the docker image by specifying the port we choose to use :

docker run -p 80:80 basic

This command means, the port 80 of docker is binded with our local port 80. The first 80 is for our local port, the second for docker.

By default the URL where we can access to docker is . So if we go to :, you’re supposed to see something like :

“Hello word” on

If I write -p 81:80, I will have to go to the url to access the localhost:80 of docker.

For having a better structure for our project, we will use docker compose.

Docker-compose “is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.” (from the official website here).

In other words, it helps you run a more structured project in docker with more than one container.

First steps

To use it, we need two things :

  • create a configuration file docker-compose.yml at the root of the project
  • add a new folder/docker also at the project’s root


We write the configuration in docker-compose.yml.

First we specify the version of docker we use. Actually the last version is 3.8, so we can just write 3.

Then we will describes the services we use. We will give a name to our service “symfony” and add some configuration.

version: ‘3’
context: .
dockerfile: docker/Dockerfile
image: basic
– 80:80

Start docker-compose

To launch our service, we use the docker-compose up command.

docker-compose up

We can read a warning informing us that the image for our service has been created automatically.

All logs will be displayed in our console.If we wanna quit and stop docker-compose, we have to use CTRL+c shortcut.

If we reload the service after, this time the image is already created, docker-compose starts immediatly the “symfony” service.

We can test our configuration by going to If everything works fine, you should see your “Hello world” message.

We will see how to use a real Symfony application with docker in chapter 2. It will be published soon.

Leave a Comment

Your email address will not be published. Required fields are marked *