Build Docker for the project using the oldest technology

Tram Ho

Hi everyone, today I came here again. If there are articles related to Devops, I will do this post.

Normally, in my previous articles, people often said they used to use Docker to build the environment. Then there is a docker-compose.yml file in the project.

Yes, everyone who knows about docker knows what the file is for, and there is something in it, especially those images already on dockerhub, images that install everything from php, mysql, git, composer ….. Dev just need to use it on docker-compose and it’s done like a horse.

But you try to think, you are a Dev, you do not always have to do new projects like laravel 6 .. then mysql 8 …, you have to use customer technologies from the napoleong times. have never met. So now you have to create your own image, can not be used on dockerhub anymore. You have to configure it yourself to link the necessary containers together, or put them on a network to run, Many users of Sun’s images are familiar, so they don’t pay attention to the problems.

Especially ae Sun often uses the company’s existing images such as framgia / php-fpm, framgia / workspace, framgia / nginx …

Well come to this article I will guide people to create their own images. To ensure that every project, with the best banana technology, can be guaranteed without having to worry about installing those technologies on your own host machine.

Ok, got it let’s go

Step 1: Create a laravel project

This, people keep running up to laravel’s doc to download, but download the old version like 5.1, Let me try to build docker with low php version.

composer create-project laravel / laravel demo “5.1. *”

Done then

Open composer.json file to see what this laravel version needs to run

Well nothing, just php above 5.6.4 à okay =))

Step 2: Create a container with docker-compose

We will create the following files and folders.

  • docker-compose.yml: install services running in docker, each service will perform 1 function such as web server, web server
  • Docker directory, in this directory there are subdirectories such as php, nginx, mysql, in these sub directories contain Dockerfile and conf files to build the environment (nginx)

Create a service app (php-fpm)

Inside

  • version 2: version of docker-compose, here I choose 2
  • context: the directory where dockerfile is located
  • dockerfile: specifies which image to install on the service, in which status container will have pre-installed directories …
  • working_dir: similar to the specification of docker run, the directory selected in the container to run binaries files. The default is root
  • volumes: mount the ./ directory (containing source code) on the host machine into / var / www on the container. This allows us to change the source code when the container is running at runtime.
  • networks:
    • app-network. Normally people need links or depend_on to the right services, I do not do it that way, I just need to let these containers run on the same network because I defined ‘app-network’ so they can communicate. okay together.

The Dockerfile file specifies this laravel5_php container

In this file I will use the image php: 7.2-fpm available on dockerhub, people can install it on their own. I tried demo version 5.6 but there is a library that reuqire php on 7.1 so I always use 7.2 😢😢😢

The rest is to install one more libraries I install such as git, zip, vim … so that when I exec into this container, I can code or fix anything in this container.

And the most important part

EXPOSE 9000

I expose out a port 9000 so that nginx can know and communicate it.

Create Nginx service

We will need a WebService to handle incoming requests and take them to laravel for processing. In addition to nginx we can choose apache

Inside

  • ports: – 8090: 80: Map port 8080 on the virtual machine to port 80 on the container.
  • The Dockerfile file specifies this laravel5_nginx container

Very simple, I use nginx already available on dockerhub. and the most important part is how to configure nginx to point to the public folder in my project, right there, and that’s this section.

It will add the vhost.conf file to the etc / nginx / conf.d / default.conf directory of the laravel5_nginx container.

And the most important part of this file is for the nginx guy to listen to port 9000 of the php container above me expose right.

This is it.

Create service database (mysql and phpmyadmin)

These are not hot, bro,

  • image: mysql: 5.6 Compared with the above services, there is a dockerfile for the installation specification for this service database, just pull the mysql image to be enough. Other necessary specifications are included in this image so we won’t need to write them separately.
  • ./docker/mysql/dbdata:/var/lib/mysql: This is just to mount the container’s mysql directory with the dbdata directory to log sql data. Make sure when the container data is down, but rest assured that the data has been mounted to the host machine directory
  • enviroment: Environmental variables. With the above definition, mysql will give us 1 database and 1 such user. (If not specified, there will be only 1 root user and default database of mysql)

Normally on the host machine mn usually use phpmyadmin or adminer to access the db, right, to docker, mn can also use these guys, just use the images available: v

OK, that’s good, since I haven’t seen how the containers link to all this time, why can’t I see words like the link, depends_on , I’m not doing it here. I created them a network and let them run on the network that’s ok

  • networks:
    • app-network. Normally people need links or depend_on to the right services, I do not do that, I just need to let these containers run on the same network because I defined ‘app-network’ so they can communicate. okay together.

Ok that’s it The full docker-compose file will be:

Then time to run

After running, run

To be able to see the log, it is recommended to run this sentence for the first time to see if the log is out of the container or not, see if it fires a log or not.

After running, try to check

and see the result And now everyone accesses the php container with the command

then run normal commands like when people are setting up projects

edit db_host ..

After you have finished, everyone runs to the address locahost: 8090 to see the results Ok, delicious rice

Conclude

I only came here, I showed the most basic techniques so that I can confidently build any project that people are assigned to. This level is afraid of what is not it =)) Every project will rock

Share the news now

Source : Viblo