Docker Install Composer Php

2/12/2022by admin

Installing PHP Packages with Docker and Composer Written on October 13th, 2017 by Karl Hughes Package management is a method for importing code (often from open source libraries) and keeping dependencies up to date in a software development project. Hey, Thanks for gist. If I run 'docker-compose run composer install', it creates a new containter, instead of using the defined one. It works, just that you get plenty of stopped containers in kitematic with names like composercomposer1 and so on. The repo comes with a composer file, an application-level dependency manager for PHP. Since we want everything to run inside the Docker container, we will install the dependencies using the Docker composer image. This will also help us avoid having to install composer globally on the actual host machine. Next, fire up your terminal.

In this tutorial, we'll learn how we can use Docker and Compose to develop and run Laravel 8 applications locally. Following this tutorial you'll learn:

  • What's Docker and Docker-compose,
  • How to install Docker and Docker-Compose,
  • How to use Docker to install and run Laravel 8 applications.

What's Docker

Docker is a container tool that allows developers and organizations to build, run and deploy applications using containers.

Containers are standalone images that allow developers to package applications with their dependencies. They share the kernel and resources of the system where they are running and they provide isolated environments for applications.

Docker makes use of Dockerfile files to define and describe the parts and dependencies used by an application and also commands to run in order to install the dependences and start the application.

Installing Docker

start by installing Docker for your type of operating system by following one of these official guides:

What's Docker-Compose

The official documentation describes docker-compose as:

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.

Using docker-compose involves three easy steps:

  1. First, you need to define/describe your app environment with a Dockerfile so you can reproduce it anywhere.

  2. Second, you need to create docker-compose.yml file and define the services required for running your application so you can run them in multi containers.

  3. Finally you just need to run docker-compose up and Compose will start all the services for your application

Creating a Laravel 8 Project

Let's create a Laravel 8 project and then see how we can dockerize it using Docker.

We have different ways to create Laravel projects: Using Composer (PHP package manager), cloning from GitHub or downloading using cURL.

Using Composer

If you have Composer installed on your system then you can simply use the following command to create a Laravel project:

Using GitHub Repository

Alternatively if you don't want to install Composer in your system, you can also clone Laravel from GitHub using:


Installing the Project Dependencies

Now, let's install the project's dependencies. If you have Composer installed, simply run the following command from within your project's root folder:

If you don't have Composer installed on your system. You can also use Docker with the official composer image to install dependencies by running the following command from the project's root folder:

The --rm switch tells Docker to automatically clean up and remove the container after doing the task and exiting.

This is what the docs says about that:

By default a container’s file system persists even after the container exits. This makes debugging a lot easier (since you can inspect the final state) and you retain all your data by default. But if you are running short-term foreground processes, these container file systems can really pile up. If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the --rm flag:

The command: -v $(pwd):/app instructs Docker to mount the current directory on the host system retrieved using $(pwd) to /app in the container . What app? Simply because the composer tool in the container will look for a composer.json file in this folder.

After finishing the installation, either way, you'll find a vendor folder with all dependencies inside your Laravel project.

Creating a Dockerfile

Now, after installing Docker and Docker-compose and creating the laravel project let's create a Dockerfile to define our environmenet. Create a new Dockerfile file in the application directory and start by adding the first command:

Docker install composer php tutorial

Docker Install Composer Php Windows

This tells Docker to download and use the php:7.2.2-fpm image.

Next add:

This run apt-get to install the dependencies and extensions required by Laravel.

If you want to install Composer you can also add this command:

Now, add:

This tells Docker to set the working directory in the container to /app and copy the files (Laravel project) in our current folder (in our system host) to the /app folder in the container.

If you didn't install the dependencies using the previous methods you can install them using:

Now, let's run the artisan serve command using the CMD command:

This will serve the Laravel project from the port 8000.

Finally, you need to expose the port 8000 from the container using:

This is the complete Dockerfile:

Building the Docker Image

After creating the Dockerfile, you are ready to create the actual Docker image. From your terminal, navigate to the root folder of your project and run:

Using the -t switch we can specify the tag/name of the Docker image. For the dot means the current folder will be used as the context for the image to be built. Remember the COPY command in the Dockerfile? its source folder is this folder so we can copy the files from our project to the container file-system.

Install Laravel

Running the Docker Image Using Docker

After building the Docker image (my-laravel-image) you can now run it using:

You can now access your application from your browser at http://localhost:8000.

Running the Docker Image Using Docker-compose

Docker-compose is more useful to build multi containers for the same application but it can also used to run a single Docker image. So first, create a docker-compose.yml file in the root folder of your project and add the following content:

We are defining a volume using ./:/var/www which simply mount our current directory ./ in the host system to /app in the container. This will allow you to work on the source code in your project's folder and changes will be reflected in the container file-system (in our case /app) without executing any other commands.


In this tutorial, we've seen how to use Docker to create a development environment with Laravel 8. We've seen how to use both Docker and docker-compose to run our Laravel 8 application in an isolated container.


Before you start, you need to have some certain prerequisites in place:

  • PHP 7.1.3+
  • Local machine running the latest version of Docker.

Get Started

Let's create a Symfony project and then see how we can dockerize it using Docker.

Symfony Requirements

In this tutorial we will use Symfony 4. These are the technical requirements to run it:

  • PHP version: 7.1.3 or higher
  • PHP extensions: (all of them are installed and enabled by default in PHP 7+)
    • Ctype
    • iconv
    • JSON
    • PCRE
    • Session
    • SimpleXML
    • Tokenizer

Step 1 - Install Symfony

Symfony utilizes Composer to manage its dependencies, similar to Laravel. So, before using Symfony, make sure you have Composer installed on your machine.

Install Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. To a install it quickly, just open up a terminal and run this command:

Composer is multi-platform and it run equally well on Windows, Linux and macOS. Here you will get more information regarding how to install composer in each platform:

To make things simple, Symfony provides a tool to quickly check if your system meets these requirements. In addition, the tool provides recommendations if applicable. So run this command to install the tool:

Once you have fixed all the reported issues, uninstall the requirements checker to avoid leaking internal information about your application to visitors:

Step 2 - Create a Symfony Project

You can create a symfony skeleton project by issuing the Composer create-project command in your terminal:

The project directory will contain the following folders:

  • config: holds config files
  • src: where we place our PHP code
  • bin: contains executable files
  • var: where automatically-created files are stored (cache, log)
  • vendor: contains third-party libraries
  • public: contains publicly accessible files

Then, to install all dependencies and run the project, you need to run the following commands:

You will see your Symfony server running on:

Congratulations, you’ve created a Symfony project!

Step 3 - Dockerize the Project

Setup Docker

Before creating a container for the Symfony application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions:

Make the docker App image

The next stage to improve your docker php development workflow is adding a Dockerfile to your project. The structure of a Dockerfile can be considered a series of instructions on how to build your container/image.

Start the Dockerfile by creating an empty file named Dockerfile in the root of your project. Then, complete each instruction according to the following example:

Building and Running the Container

Building the container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile and leave you with a container that is ready to start.

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

The final step is to run the container you have just built using Docker:

The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine. After you run this command, you should be able to visit http://localhost:8000 in your browser.

You can see the Docker containers that are currently running on your system (along with their Container IDs) with:

To turn off your Docker container, run:

Push to cloud

1. Create your app

In order to install your docker php example, just create a new app via cli or admin panel and set a port to 8000.

2. Push your docker container

3. Set up resources

4. Logs and app status

5. Release your app

After to click on the release button, your php docker tutorial will be deployed, Just click on the generated URL and you will get your app running.

Docker Install Composer Php Tutorial

Now you can deploy your Symfony app without a massive build time.

Bonus 1: SSL certificate for HTTPS

It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.

Docker Php Image Install Composer

Bonus 2: Autoscaling

With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.

Now you can deploy your Symfony app without a massive build time.

Comments are closed.