Starting with Docker

I have decided to delve into the world of Docker. The Docker documentation has been good to get it installed and working, but I’m not sure where to find help for creating or installing a LAMP container.

@benanamen recently shared how he set it up here – Docker LAMP Stack With Composer PSR-4 Autoloading – Apache Server

Most notably you don’t want a single LAMP container. You want a container with an HTTP server (NGINX or Apache), a PHP container and a MySQL container. This is different from VMs where you want to install all these things in a single VM.


Cool, thanks!

What I tend to do is create a Dockerfile and inherit from an nginx image first then install what I need. Here’s a small snippet of what I tend to do, it’s not the full Dockerfile, but should get you somewhere.

# Import from base image out there on the Docker hub
FROM nginx:1.25.2

# Change to the root user
USER root

# Run these commands
	# Update the system
	# Install lsb-release, apt-transport-https, ca-certificates, wget, sudo, htop, openssh-server, vim, and net-tools
	# Downloads the gpg keys for PHP on the Debian system
	# Update the system
	# Install PHP 8.3 and various other PHP extensions
	# Download composer and move composer to the /usr/local/bin folder

RUN apt-get -y update ; apt-get -y install software-properties-common lsb-release apt-transport-https ca-certificates wget sudo htop openssh-server vim net-tools zip git ; wget -O /etc/apt/trusted.gpg.d/php.gpg ; echo "deb $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list ; apt-get -y update ; apt-get -y install php8.3-fpm php8.3-mysql php8.3-pgsql php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip php8.3-gd php8.3-ldap ; cd ~/ ; php -r "copy('', 'composer-setup.php');" ; php composer-setup.php ; php -r "unlink('composer-setup.php');" ; sudo mv composer.phar /usr/local/bin/composer

nginx’s default HTML folder is located at /usr/share/nginx/html/ for this image. I would suggest modifying the default file at /etc/nginx/conf.d/ to point to somewhere you’re comfortable with and also edit the PHP files at /etc/php/8.3/.

Then to build the Dockerfile, I do something like this where localhost is the image name and 0.1 is the tag.

docker build -t localhost:0.1 .

Then to create the actual container, I do something like

docker run --detach \
  --hostname \
  --name localhost \
  --restart always \
  --publish 80:80 --publish 443:443 --publish 22:22 \
1 Like

Thanks @spaceshiptrooper. I’m running Docker on Windows with WSL. I’m puzzled as to whether I should be creating files/folders and running commands in Windows or in the Linux subsystem…

Well it doesn’t really matter how you create them. It’s all going to be treated the same at the end.

1 Like