Laravel in Docker on the Cloud – Part 2

Laravel in Docker on the Cloud Part 2

Continue from Part 1

Now the problem we face now is that we need to stop and re-run the docker image whenever there’s any changes in the Laravel code.

To solve this, we can mount the project root into the Docker container’s volume,
so it will get the latest code whenever we save the code. To do this, run docker with any of these commands:


# Mount with full path
docker run -d -p 8000:80 -v /path/to/laravel/project/root:/var/www laravel01

# Mount with current working directory:
docker run -d -p 8000:80 -v $PWD:/var/www laravel01

So we would continue development, until a point that we need to deploy the docker to a staging server,
the main selling point of Docker is identical development / staging / production environment,
what we need to do is to install Docker in the staging server, and run the docker image we built in development machine.

But before that, we will need to build and push our docker image to a repository.
There’s a bunch of repository service provided by cloud hosters like Google, AWS.
We will be using the https://hub.docker.com/ in this article, you should have an Docker account already, since you need an account to download and install Docker.

1. in your development machine, run this command in terminal:
docker login

2. Build the docker image with your username as prefix, and then push it to Docker Hub:
cd /path/to/your/laravel/project
docker build . -t howmun/laravel01
docker push [username]/laravel01

3. Create a cloud server with Linux OS (we use Ubuntu 18.04), and install Docker CE on it,
extensive guide is available in the official Docker site:
https://docs.docker.com/install/linux/docker-ce/ubuntu/

4. Pull and run the Docker Image from Docker Hub:

# Note that we have changed port 8000 to 80, so the staging server will be listening to port 80
docker run -d -p 80:80 --name="laravel-staging-v0.1" [username]/laravel01:latest

5. View the staging site at http://[your public IP]

6. Now let say you have added some new features in Development, and want to push them to staging server:
# In Development server build and push again
docker build . -t howmun/laravel01
docker push [username]/laravel01

# In Staging server pull the latest version
docker pull [username]/laravel01
docker stop laravel-staging-v0.1
docker rm laravel-staging-v0.1
docker run -d -p 80:80 --name="laravel-staging-v0.2" [username]/laravel01:latest

There’s still a lot more topics to cover, take DB for instance,
DB will be hosted externally, and we will need to configure the connection details differently for Development vs. Staging vs. Production.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *