Github Codespaces: Code on the go with Laravel in Docker Container

Rakibul Islam
3 min readFeb 27, 2023

--

GitHub Codespaces is a cloud-based development environment that enables developers to code on the go, from anywhere and on any device. With Codespaces, you can set up a development environment quickly and easily, without the need for local installations. In this article, we will explore how to use GitHub Codespaces to develop a Laravel application in a Docker container.

Docker containers provide a lightweight and efficient way to package and run applications, and by using Codespaces, you can develop your Laravel application in a Docker container from anywhere with an internet connection.

This article will guide you through the process of setting up a Codespace with Laravel in a Docker container, enabling you to develop and deploy your application with ease.

Step 1: Create a Github Repository from Laravel Docker Template

The first step is to create a new GitHub repository for your Laravel application. Browse https://github.com/rakibdevs/laravel-docker. This is a generic docker environment for Laravel local development. Click Use this template . This will create a repository with minimal Docker configuration.

Step 2: Create a New Codespace

Next, navigate to the “Codespaces” tab on the GitHub repository page and click on the “New Codespace” button.

Step 3: Build Docker Container

In Codespaces, Open a new terminal and run the following commands:

$ docker compose build

This command will download all the necessary dependencies and build the Docker image according to the specifications in the Dockerfile.

Once the build is complete, run the following command to start the Docker container:

$ docker-compose up -d
Build Docker Container

Step 4: Install Laravel

Run the following command to create a new Laravel project inside a src directory:

$ docker compose exec php composer create-project --prefer-dist laravel/laravel .

This command will install all the necessary dependencies and create a new Laravel project inside the src directory.

Now copy .env.example to .env by following this command:

$ cp src/.env.example src/.env

and update database confiiguration:

DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=1234

Now install necessary composer packages for Laravel by following this command. You can do this inside docker container.

$ docker compose exec app
$ composer install
$ php artisan key:generate

Step 5: Access Webpage

Ensure that the Docker container is running by running the command `docker ps` in your terminal or command prompt. This command will display a list of all the running Docker containers on your system.

Check the available ports in Codespaces:

Browse port 8999 and you will see a preview of your Laravel Application.

Laravel Application Preview

If you want to migrate your database please run the following commands:

$ docker compose exec php php artisan migrate

In conclusion, GitHub Codespaces provides a convenient and powerful cloud-based development environment that allows developers to code on the go, without the need for local installations. With the ability to create development environments quickly and easily, developers can focus on writing and testing their code instead of worrying about infrastructure setup.

--

--

Rakibul Islam
Rakibul Islam

Written by Rakibul Islam

Passionate PHP Expert, Love to make things that make a difference! You can find me here, https://www.linkedin.com/in/rakibhstu/

No responses yet