Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Docker-Compose for Project #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM php:8.1-fpm

# Copy composer.lock and composer.json into the working directory
COPY composer.lock composer.json /var/www/html/

# Set working directory
WORKDIR /var/www/html/

# Install dependencies for the operating system software
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
libzip-dev \
unzip \
git \
libonig-dev \
curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions for php
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Install composer (php package manager)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy existing application directory contents to the working directory
COPY . /var/www/html

# Assign permissions of the working directory to the www-data user
RUN chown -R www-data:www-data \
/var/www/html/storage \
/var/www/html/bootstrap/cache

# Expose port 9000 and start php-fpm server (for FastCGI Process Manager)
EXPOSE 9000
CMD ["php-fpm"]
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,48 @@ steps further. Here are some quick ideas that you might play with.
5. Allow registered users to "follow" certain authors. When they publish a new post, an email should be delivered to all followers.
6. Allow registered users to "bookmark" certain posts that they enjoyed. Then display their bookmarks in a corresponding settings page.
7. Add an account page to update your username and upload an avatar for your profile.


## Docker (with Docker-Compose) Setup

Docker and Docker Compose allow for an easier way to standup this demo application. We will create 3 containers (app, db, nginx) in order to get all this sample app stood up using Docker. In order to use this option you'll first need to make sure that you have Docker installed on your local machine.

Change directories into the app directory and run the following command: `docker-compose up -d`.

This will download the images and create the containers. We'll then need to clear the cache and generate the key as mentioned above. The easiest way to do this once the containers are up and running is to get a bash shell on the app container with the following command:

```
docker-compose exec app bash
```

Once you have a terminal on this container then you can run the following commands.
```
php artisan key:generate

php artisan config:cache
```

Next we need to connect to the database container and create a user that we can use to migrate and seed the database. This is similar to above but instead of the app container, we'll need to connect to the MySQL container.

```
docker-compose exec db bash
```
Once you have a shell on this container, connect to MySQL in order to create a MySQL user for the application to use.

```
mysql -u root -p laravel_web
Enter password: <password you create in docker-compose.yml>
```

**Important** - This must be the same details that are specified in the `.env` file.
```
GRANT ALL ON laravel_web.* TO 'laraveluser'@'%' IDENTIFIED BY '<laravel_docker_password>';

FLUSH PRIVILEGES;
```

Finally, lets get terminal access on the `app` container and use it to migrate and seed the database.

```
php artisan migrate --seed
```
Loading