Environment construction with Docker for beginners

Since we built the environment using docker, we will introduce the procedure. Since I built the environment with docker for the first time, I hope it will be helpful as much as possible.

Environment
environment version
PHP 7.0.27
FW CakePHP3.2.13
Apache 2.2.15
OS CentOS6.9
DB MySQL5.6.39

Procedure 1. Docker folder DL 2. Git Clone 3. Enter the Docker container 4. Start Apache 5. Document root settings 6. Composer install 7. Start mysql 8. Create database 9. App.php settings 10. Database connection

Let's build the environment

Docker folder DL This time, I downloaded the originally prepared DockerFile, yml file, etc. all at once. Below is the folder structure.
-/
 |- docker-compose.yml
 |- docker
    |- db
     |   |- Dockerfile
     |   |- files
     |      |- entry.sh
     |      |- my.cnf
    |     
    |
    |- web
        |- Dockerfile
        |- files
           |- composer.phar
           |- dev.conf
           |- php.ini
           |- ssl.conf
 

Git Clone

Get the project on github. The folder structure is as follows.
-/
 |- project
 |- docker-compose.yml
 |- docker

Enter the Docker container

Create an image and start the container before entering the container. Do the following on the command line:

  • Build
docker build
 // You can get the image from docker file by building.
  • Start container
$ docker-compose up -d
 // Start the container.

You can build and start the container at the same time with the following command.

  • Build and start container
$ docker-compose up -d --build
 // Start the container by building the image.
$ docker ps
 //起動中のコンテナの確認

About option -d
$ docker-compose up
 // You cannot work in the same terminal because you are starting the container in the terminal.

$ docker-compose up -d // Since the container is started in the background, you can work in the same terminal.

Enter the container with the following command.

$ docker exec -it CONTAINER ID /bin/bash
 // The CONTAINER ID can be confirmed by doing docker ps.

Apache startup In the web container, start Apache with the following command.
service httpd start
 Apache startup

service httpd stop
 Apache stopped

service httpd restart
 Apache restart

/etc/init.d/httpd status
 Apache status check

Document root settings Check the document root settings in httpd.conf in the web. With this directory structure, there seems to be no problem with var / www / html.

composer install

Could not composer install. I thought it was an access permission issue, so I changed it to access permission 777. Regarding access authority, I referred to the following article. https://qiita.com/t-a-run/items/239ed690ece7a011804a

I was able to execute the following. By the way, composer install was done in the project folder.

compser install

If you access in this state, a database error will be displayed in the browser. The content of the error is that no such (connected) file can be found.

database error(SQLSTATE[HY000] [2002] No such file or directory

Start mysql Run mysql. When you enter the web container, you will enter the db container as well.
service mysqld status
 //mysqlの起動状態の確認
service mysqld start
 //mysqlの起動

Confirm your initial username and password before logging in. Check mysqld.log in the DB container.

cat mysqld.log

Get the user name password written in A temporary password is ~. Username: root

Log in to the database.

 docker exec -it container ID mysql -u root -p

Reset password-> Create database-> Create table.

app.php settings At the time of git clone, app.php is not included. Let's create it.
-/
 |- project
    |- config
        |-app.php
How to write app.php can be found by searching on the net. This time, I copied the file used elsewhere and edited the contents. I mainly touch the database.
 'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            /**
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',
            'username' => 'root',
 'password' =>'password set when creating the database',
 'database' =>'Password set when creating database',
 'port' => port number,
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,

cat mysqld.log Get the password written in A temporary password is -> Restart mysql in db container, login-> password reset-> create database-> create table->

Database connection I ran a connection test with a quick connection using sequel pro. The connection has been completed successfully.

I searched for localhost in my browser and it opened successfully.

References

Understand the contents of docker-compose.yml https://futureys.tokyo/lets-understand-contents-of-docker-compose-yml/

Docker compose Kotobuki Hands-on https://qiita.com/TsutomuNakamura/items/7e90e5efb36601c5bc8a

How to write and use Dockerfile https://blog.codecamp.jp/docker-file-how-to

Enter the shell of a running docker container https://qiita.com/sekizo/items/27cc9b406332afc674f6

Database creation https://noumenon-th.net/programming/2019/04/01/docker-entrypoint-initdb01/

How to write permissions to set with chmod https://qiita.com/irasally/items/6ebc3c68e22905fb7330

Recommended Posts

Environment construction with Docker for beginners
[Docker] Rails 5.2 environment construction with docker
React environment construction with Docker
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Docker environment construction
GPU environment construction with Docker [October 2020 version]
Rails environment construction with Docker (personal apocalypse)
Laravel development environment construction with Docker (Mac)
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Laravel + MySQL + phpMyadmin environment construction with Docker
Rails Docker environment construction
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Environment construction procedure for using PowerMock with JUnit
Environment construction command memo with Docker on AWS
CI / CD practice for beginners --Part1 --Environment construction
[Ubuntu 18.04] Environment construction for using PyTorch with RTX3090
Rails6 [API mode] + MySQL5.7 environment construction with Docker
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
Pytorch execution environment with Docker
MySQL 5.7 (Docker) environment construction memo
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
How to build docker environment with Gradle for intelliJ
Stable development environment construction manual for "Rails6" with "Docker-compose"
[Java] Environment construction procedure for developing struts 1.3 with Eclipse
Database environment construction with Docker in Spring boot (IntellJ)
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Environment construction for Servlet application development
Beginners install docker for mac and prepare php7.0 operating environment
Laravel + Docker Laradock usage environment construction
Build PlantUML environment with VSCode + Docker
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Build environment with vue.js + rails + docker
Easy environment construction of MySQL and Redis with Docker and Alfred
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
[Folio LSP] Roughly Docker (for beginners)
Build Rails environment with Docker Compose
Create SolrCloud verification environment with Docker
Rails on Docker environment construction procedure
Create Laravel environment with Docker (docker-compose)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Jupyter's Docker environment for running TensorFlow
[Java & SpringBoot] Environment Construction for Mac
Build docker + laravel environment with laradock
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
(For myself) Try creating a C # environment with docker + code-server, cloud9
PostgreSQL environment construction with Docker (from setup to just before development)
Build a development environment for Django + MySQL + nginx with Docker Compose
Alert slack with alert manager in Docker environment
SQL statement learning ~ Environment construction ~ Docker + MySQL
Building Rails 6 and PostgreSQL environment with Docker
Sapper × Go (echo) × Docker development environment construction
Build a PureScript development environment with Docker
Create Rails 6 + MySQL environment with Docker compose