Dockerize Laravel Application
Docker Basic Definition and A Simpel Tutorial
Docker is a platform that allows developers to package, distribute, and run applications in lightweight, portable containers. These containers bundle all the necessary dependencies and libraries required for the application to run, making it easier to deploy and manage software across different environments, such as development, testing, and production. Docker containers are isolated from each other and can run on any system that has Docker installed, making them highly versatile and efficient for building and deploying software.
Here are some terms you need to know when learning Docker.
- Image — A collection of read-only files (bundles) that support an application. Docker images can be created independently containing a collection of other images and/or application source code.
- Container — A lightweight, standalone, and executable software package for packaging and running applications. Includes code, runtime, system tools, and other configurations.
- Docker Client — A place to send docker command requests to the Docker API (which will then be passed to the Docker Daemon), such as the command docker pull, docker run, docker build, etc.
- Docker API — Connecting docker client with docker daemon.
- Docker Daemon — A place to manage containers, images, networks, volumes, etc. Docker daemon waits for requests/instructions from the Docker API.
- Docker Desktop — A GUI for easy interaction with the docker daemon.
- Docker Compose — Docker client works by reading docker commands in yaml files.
- Docker Registry — A place to store docker images.
- Docker Hub — Public Docker registry that can be used to store (push), search, and download (pull) images.
Basic Command
- docker create — create new container
- docker rm — delete container
- docker start — start existing docker container
- docker restart — restarting running container
- docker stop — shutdown running container
- docker run — create and run new container
- docker ps — show list of docker containers
- docker images — show list of pulled docker images
- docker pull — install/get image from Docker Hub
- docker push — store image to Docker Hub
- docker exec — access container shell/terminal
- docker cp — copy file from host to container or vice versa
- docker network — show network in docker cluster
Use Docker
- Install Docker
Install docker at https://docs.docker.com/get-docker/ according to the operating system specifications used.
- Install Docker Compose
Install docker compose at https://docs.docker.com/compose/install/ according to the operating system specifications used.
- Create Docker Hub Account
Create a Docker Hub account at https://hub.docker.com/
Dockerize Laravel App
- Set up a simple Laravel project
- Create a Dockerfile file in the project root directory, fill it with the following code
FROM php:7.4-apache
WORKDIR /var/www/html
COPY . /var/www/html
EXPOSE 80
- If you use Docker Compose, create a docker-compose.yml file in the project root directory, fill it with the following code. If port 8080 on the host computer is already being used by another application, change it to another port that is still available.
version: "3"
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80"
- Build image — Run this the command to create an image of the Laravel project
docker build -t my-laravel-app
- Run the Laravel project using the container
— Enter this command if you use Docker Compose
docker-compose up
— Or this command if you are not using Docker Compose.
docker run -p 8080:80 my-laravel-app
Containers can be accessed on the port that is defined in the docker-compose.yml file or when running the command run -p. In this case, the application can be accessed at http://localhost:8080.
- Store image to Docker Hub
So that the image can be accessed and used by other computers, the image must be stored in the registry, namely Docker Hub.
— Give a tag to the image that will be saved to Docker Hub using the command as in the example below.
docker tag image-name dockerhub-username/image-name
Change the dockerhub-username and image-name to your actual environment name. For example:
docker tag my-laravel-app sekarmk03/my-laravel-app
— Login to Docker Hub from your terminal
Run the following command in the terminal then enter the Docker Hub account username and password.
docker login
— Push/Store image to Docker Hub
Run the command below to push the image or save the image to Docker Hub. Don’t forget to change your username and adjust the name of the image you want to push. After pushing, the image becomes public and can be accessed by other people.
docker push sekarmk03/my-laravel-app
- Get image from Docker Hub
To retrieve and use an image from Docker Hub, simply run the command below.
docker pull dockerhub-username/image-name
Change the dockerhub-username and image-name to your actual environment name. So, for me it’s become:
docker pull sekarmk03/my-laravel-app
- Run the image in container
Run the image into a container with one of the following commands.
— Enter this command if you use Docker Compose
docker-compose up
— Or this command if you are not using Docker Compose.
docker run -p 8080:80 my-laravel-app