Setup Guides

Setting Up Docker and Laravel Sail on Ubuntu

Saturday, September 21, 2024

// 2 min read

Ubuntu 22.04 Laravel 11 Docker
Setting Up Docker and Laravel Sail on Ubuntu

Introduction

This guide aims to assist you in setting up to set up Docker and Sail for Laravel local development environment on Ubuntu.

Install Docker

The first thing to do is to add the official Docker GPG key to your Ubuntu system so that you can install Docker from its official repository:

install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Then you can add the official Docker repository:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

Finally, you can install the Docker package:

sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 

Add your user to the Docker group

In order to be able to use Docker without sudo you can add your user to the Docker group.

sudo usermod -aG docker $USER

For the group change to take effect, you'll need to log out and log back in, or you can run the following command to apply the group change without logging out:

newgrp docker

Testing the installation

Once Docker is installed, you can verify the installation by issuing the command:

docker version

You should see something like this:

Docker Version

After check if you can run a Docker command by pulling down the hello-world image:

docker pull hello-world

Create Laravel Project

Create your new Laravel application:

curl -s https://laravel.build/my-app | bash

Then navigate to the new application directory and start Laravel Sail:

cd my-app
./vendor/bin/sail up

I recommend creating a shell alias, so you do not need to type vendor/bin/sail all the time.

alias sail='sh $([ -f sail ] && echo sail || echo vendor/bin/sail)'

Also, you can use the detached mode (-d), so the container runs in the background, and you can keep using your terminal window. Now you can just run the following command:

sail up -d

To stop the container run:

sail down

Once the application's Docker containers have started, you should run your application's database migrations:

sail artisan migrate

Now, you can visit http://localhost:8000 in your browser to see your app is up and running.

Laravel running on localhost