Docker Php Composer Install

2/13/2022by admin

Posted by Ben Watson on 7th January 2020

  1. FROM php:7.2-alpine RUN docker-php-ext-install pcntl sockets This can be built with something that tags it with a human understandable name and used as a base for other images. FROM example-php:latest as deps # install composer, app code, deps, etc here FROM example-php:latest COPY -from=deps /app /app.
  2. Composer is a dependency manager written in and for PHP.
  3. I try to work out a way to create a dev environment using docker and laravel. I have the following dockerfile: FROM php:7.1.3-fpm RUN apt-get update && apt-get install -y libmcrypt-dev.
  4. Oct 24, 2019 In Part 2, we describe how to install the Telegraf plugin as a data-collection interface with InfluxDB 1.7 and Docker. The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: MADE, Docker, Real.

This guide will show you how to create a composer project using docker-compose as the development environment. Their are a few main objectives.

Sep 24, 2020 Nginx PHP MySQL. Docker running Nginx, PHP-FPM, Composer, MySQL and PHPMyAdmin. Install prerequisites. Before installing project make sure the following prerequisites have been met.

  • Should be easy to switch PHP versions
  • Should come with composer baked in
  • Should allow easy setup for contributors
  • Should help encourage identical development environments

Complete project structure

We'll start by creating an example class for our package. It will have the namespace UniBenExampleComposerDockerCompose and should be autoloaded in to our example app code found in the app directory.

Now we can add autoloading for the package in our composer.json

Next up we'll create our docker-compose.yml file

Then create the dockerfile called app.dockerfile in the directory etc/docker for the app

For testing our cli we’re going to make a directory called app. This could just as easily be PHPUnit tests but just to keep things simple, we’ll make the directory app and have an index.php file which calls our new SayHello class.

./app/index.php

Let’s make a composer.json file in our app folder which requires our package.

Now in our ./app/composer.json add a symlink for our package which is in our ./src directory. Notice we symlink to /package even though our package code is in ./src. Not to worry, ths is where we’re gong to mount our package in the container file system using docker-compose.

Next, we add our package to the require section of the package and specify the minimum stability as @dev

Now we can mount our package and application code in to our docker-compose container. We can do this by adding the following volumes to our app service in docker-compose.yml

Let’s take a look at our container file system. You should see your application code in the container directory/src and the package code in the container directory/package

Looks good. Now we can move on to installing dependencies. We’ll add a dependency to our package just to test composer is working.

You get an error similar to this one:

This is because the base docker image we’re using doesn’t install git and related extensions. We can fix this by updating our dockerfile.

Near the top of etc/docker/app.dockerfile (before the composer install) add

This command will update the apt-get repo and install, git, zip and unzip then tidy up the image a bit so it isn’t so large.

Now we need to exit our container bash session (Ctrl/Cmd + D) and rebuild our docker image.

Now we’ve updated our dockerfile we can run composer require again for our package and see it successful installs our specified package.

Great! We know composer is working so we can move on to requiring our package in our ./app code.

Our package along with its dependencies should now be installed. Let’s update our ./app/index.php and test our class.

If everything is working running php index.php should output 'Hello, Ben!' to the console.

Now let’s streamline things a little bit by moving the composer install to the dockerfile instead of sshing in to the container and running the command our self.

Let’s copy our app code in to the /src directory of the container, mount the src package (the entire project) in to the /package directory, set our workdir to the directory we mount our app code in and then run the composer install command.

Now we need to rebuild the image using docker-compose build.

We’ll also add a default command to our dockerfile which runs our script if no command is specified using docker-compose run

Before we run our container, delete the vendor and composer lock from our app directory. This is because we want to test how a new install would work. Because our composer install is done in the composer build it should still work right?

Uh oh! Why does this happen? This is because we’ve overwritten the container /src directory with our ./app directory. So how do we get around this issue without having to ssh in to the container and run composer install manually.

Docker Php Composer InstallDocker php composer install in python

Well this is quite easy and we have two options. The first option is to simply ignore the vendor folder in our docker-compose file.

There are a few advantages and disadvantages to this method. The man advantage is that it’s really easy to do. Another advantage is that performance should be a little better because accessing many mounted files can be slower.

The big disadvantage to this method is that your IDE may complain about classes being missing if you haven’t set up a remote PHP interpreter.

How to get around missing classes issue without setting up a remote interpreter.

All we need to do is copy the vendor folder from the built image to the host.

First off, add this Makefile to your project:

Duplicate your docker-compose.yml file and call it docker-compose.install.yml then remove the volumes array.

./docker-compose.install.yml

Finally, run make install and you should see the vendor folder from the built docker image appear in your project!

Composer

Note, you can still run composer commands in the container but only the changes to composer.json and composer-lock.json will be reflected to the host. The vendor folder will remain unchanged on the host unless you remove the vendor folder exclude from docker-compose volume mounts or re-run make install.

If you need to modify files in the vendor directory, you should remove the directory exclude in your docker-compose.yml however, you should only really modify these files if you need to debug.

You can see the example project here.

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 Php Composer Install In Linux

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

Docker php composer install in linux

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:

Docker Php Composer Install

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:

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.

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.

Docker Php Composer Install In Windows

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.

Conclusion

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.

Comments are closed.