DEV Community

Anthony Gilbert
Anthony Gilbert

Posted on • Edited on

3

How to set up Symfony & then Dockerize it.

In this post we are going to be setting up a Symfony Docker environment. If you don't know what Symfony is, it's a PHP framework.

To get started we will install PHP, in this example we will be using PHP version 7.3. To do this you can install by typing the following:
brew install php

After install PHP we need to install composer, which is the PHP package manager. To do this we can copy-paste the following into the terminal.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Enter fullscreen mode Exit fullscreen mode

You can run the following command to check if your system meets the Symfony requirements. symfony check:requirements

To create our Symfony project we will run the following command. symfony new my_project_name --full

Now to start our web server, we will run this command. symfony server:start, now your web server should be running on default port, 8000.

Now that we have our default application built, we need to install Docker. To do this you can run the following command in your terminal: brew install docker or visit the Docker website and download the executable.

Now we need to add a Dockerfile to the root of our project. You can do this by typing touch Dockerfile.

After creating your Dockerfile you will need to paste the following code into your Dockerfile. Code

# Dockerfile
FROM php:7.2-cli

RUN apt-get update -y && apt-get install -y libmcrypt-dev

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mbstring

WORKDIR /app
COPY . /app

RUN composer install

EXPOSE 8000
CMD php bin/console server:run 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

To build our php Symfony docker container, we will use the docker build command and provide a tag or a name for the container, so we can reference it later when we want to run it. The final part of the command tells Docker which directory to build from.

docker build -t symfony-tutorial .

After running the build command, you need to create the image. To do this you need to type the following: docker build -t symfony-tutorial . You can make sure that the image has been created by typing docker images into the terminal.

Now that the docker image has been created, you can run your docker image and upon doing so, it will start the web server.
Here is how you do it: docker build -t symfony-tutorial .

Now your docker environment is running on port 8000!!

Happy coding!!

-Anthony

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay