Docker – AppDividend https://appdividend.com Latest Code Tutorials Mon, 18 Mar 2019 08:59:14 +0000 en-US hourly 1 https://wordpress.org/?v=5.1.1 https://appdividend.com/wp-content/uploads/2017/08/cropped-ApDivi-32x32.png Docker – AppDividend https://appdividend.com 32 32 How To Setup Node Express and MongoDB in Docker https://appdividend.com/2018/04/13/how-to-setup-node-express-and-mongodb-in-docker/ https://appdividend.com/2018/04/13/how-to-setup-node-express-and-mongodb-in-docker/#comments Fri, 13 Apr 2018 22:30:22 +0000 http://localhost/wordpress/?p=432 How To Setup Node Express and MongoDB in Docker

In this tutorial, we will see How To Setup Node Express and MongoDB in Docker from scratch. Docker has become a standard tool not only among DevOps and Infrastructure people but also for the daily work of any developer. This article is aimed at the Docker beginners, who want to get a fully working instance of a Node […]

The post How To Setup Node Express and MongoDB in Docker appeared first on AppDividend.

]]>
How To Setup Node Express and MongoDB in Docker

In this tutorial, we will see How To Setup Node Express and MongoDB in Docker from scratch. Docker has become a standard tool not only among DevOps and Infrastructure people but also for the daily work of any developer. This article is aimed at the Docker beginners, who want to get a fully working instance of a Node Express and MongoDB app up and running in a matter of seconds for daily work. Docker will help you spin up containers (which behave like Virtual Machines but are not that heavy) and run your app on it. It gives us the ability to port the container anywhere we want on any server we want, moving from local to production gets secure and less buggy as the environment of the container which is running the app does not change.

If you want to learn more about Node.js then check out this course NodeJS – The Complete Guide (incl. MVC, REST APIs, GraphQL)

Setup Node Express and MongoDB in Docker

We will start this tutorial by creating simple Node Express application. So let us do it.

Step 1: Install the dependencies.

Create a project folder in your main directory by the following command.

mkdir newdock

Go into the folder.

cd newdock

Install the following dependencies.

yarn add express mongodb

or

npm install express mongodb --save

I am using Yarn package manager, you can also use npm.

Step 2: Create server.js file.

First, we need to create one database file that returns the database string. So create one file db.js inside the root.

// db.js

module.exports = {
    DB: 'mongodb://mongo:27017/newdock'
}

Notice that, I have not written the localhost in the connection string. That is because we are connecting our Express application to the mongodb container. You will see later in this article.

Write the following primary express application inside a server.js file.

// server.js

const express = require('express');
const app = express();
const mongodb = require('mongodb');

const config = require('./db');
const PORT = 4000;
const client = mongodb.MongoClient;

client.connect(config.DB, function(err, db) {
    if(err) {
        console.log('database is not connected')
    }
    else {
        console.log('connected!!')
    }
});

app.get('/', function(req, res) {
    res.json({"hello": "world"});
});

app.listen(PORT, function(){
    console.log('Your node js server is running on PORT:',PORT);
});

I have created an express instance and also connect our application to the database.

Our Node application serves at Port: 4000 and when we hit the root URL(‘/’) than in response, we get the JSON data.

We have installed mongodb package that can help us to connect our app to mongodb database.

Now, our application is ready. The next step is to write the Dockerfile and docker-compose.yml file.

Step 3: Write Dockerfile inside root folder.

Inside the root folder, create a file called Dockerfile.

FROM node:7
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node server.js
EXPOSE 4000

What it does is, we will create one container called an app and that in that container, our whole project dependencies have resided. So when we deploy our container to the server, it will first install all the dependencies and then spin up the node server. The application will run on port 4000.

Step 4: Write the docker-compose.yml file.

Inside the root folder, create one file called docker-compose.yml file. Copy the following code.

version: "2"
services:
  app:
    container_name: app
    restart: always
    build: .
    ports:
      - "4000:4000"
    links:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    ports:
      - "27017:27017"

Here, we are building two containers.

  1. app container(It is our express app)
  2. mongodb container(mongodb server).

Step 5: Build the Docker containers.

If you have not installed the Docker, then please install it using this link.

Now, open the terminal in the root of the project and type the following command.

docker-compose build

It will create the containers and put our whole code inside it. It will generate step by step. Install all the dependencies and last successfully form the containers. Now, our containers are built successfully; we need to run the container.

Step 6: Run the containers.

We can up our containers using the following command.

docker-compose up

If everything is fine, you can see something like this.

Setup Node Express and MongoDB in Docker

 

Our app is running on port 4000 and we have successfully connected to the mongodb database.

Now, remember, when you need to change the file, you need to rebuild the container, and then you run. Otherwise, your latest changes will not reflect on the container.

Go to the browser and hit this URL: http://localhost:4000/

You can see that we are getting the JSON response. We have also connected our express application to the mongodb database.

Code on Github

Finally, our How To Setup Node Express and MongoDB in Docker Tutorial is over. Thanks for taking.

The post How To Setup Node Express and MongoDB in Docker appeared first on AppDividend.

]]>
https://appdividend.com/2018/04/13/how-to-setup-node-express-and-mongodb-in-docker/feed/ 6
How To Setup Laravel In Docker Container https://appdividend.com/2018/04/12/how-to-setup-laravel-in-docker-container/ https://appdividend.com/2018/04/12/how-to-setup-laravel-in-docker-container/#comments Thu, 12 Apr 2018 16:43:59 +0000 http://localhost/wordpress/?p=418 How To Setup Laravel In Docker Container

How To Setup Laravel In Docker Container environment locally on your machine. Laradock is well-known package in the Laravel community. I will use Laradoc library to show you how to prepare Docker Container and run the laravel application in it. Docker is different from Homestead. Homestead creates a virtual machine while Docker, on the other hand, is pretty much the same […]

The post How To Setup Laravel In Docker Container appeared first on AppDividend.

]]>
How To Setup Laravel In Docker Container

How To Setup Laravel In Docker Container environment locally on your machine. Laradock is well-known package in the Laravel community. I will use Laradoc library to show you how to prepare Docker Container and run the laravel application in it. Docker is different from Homestead. Homestead creates a virtual machine while Docker, on the other hand, is pretty much the same as Vagrant, it also gives you the complete development environment but on Virtual Containers instead of Virtual Machine. Since running a virtual Container is much faster than running a full Virtual Machine, LaraDock is much quicker than Homestead. Homestead is just a command line tool that helps you to initialize and to boot a Virtual Machine via Vagrant. Where you SSH into that machine and start coding.

What is Docker Container

Docker is a computer program that performs operating-system-level virtualization also known as containerization. Docker, Inc had developed it. Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

Container image is the lightweight, stand-alone, executable package of a piece of software that includes everything you needed to run it: code, runtime, system tools, system libraries, settings.

Docker is the tool designed to make it easier to create, deploy, and run applications by using the containers.

Containers allow the developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises.

Laradock

Laradoc is a full PHP development environment for Docker.

It includes pre-packaged Docker Images, all pre-configured to provide an excellent PHP development environment.

Setup Laravel In Docker Container

First, we will install the Docker on mac, and then we will clone the repository of Laradoc and start configuring it. So let us do that first.

Install and run Docker for Mac.

For download Docker on mac, please use this link Docker Community Edition (CE) to install the stable version of docker. The Docker for Mac install package includes everything you need to run Docker on a Mac. This topic describes pre-install considerations, and how to download and install Docker for Mac.

Now, after installation, Double-click Docker.dmg to open the installer, then drag Moby, the whale to the Applications folder.

Okay, next is double click to that whale icon on the applications and it will open the installer then it will start on the right-hand side corner. You can see the Moby whale icon there.

If you have not created your docker id then you should create one, it is free. Now, verify the docker’s version using the following command.

docker -v

My version is: Docker version 18.03.0-ce, build 0520e24

Quick Note

If you are using Laravel Valet, then please stop the Laravel Valet because otherwise valet’s nginx and docker’s nginx server will crash at the same port. So before the beginning of the installation process, please close all the nginx and mysql servers. You can stop it using the following command.

valet stop

Other installation Softwares

We will install the different containers. NGINXPHPComposerMySQLRedis and Beanstalkd

Step 1: Install The Laravel 5.6 in your directory.

You can install the Laravel in your regular project directory using the following command.

composer create-project laravel/laravel doc --prefer-dist

Now, go into the project folder.

cd doc

Open the folder in Visual Studio Code using the following command. You can choose your favorite editor.

code .

Now, we need to change the database settings in the .env file.

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=doc
DB_USERNAME=root
DB_PASSWORD=root

Put your database name. Note that, here we are using the host as mysql and in general scenario, we are taking host as localhost or 127.0.0.1. Just put the above snippet inside the .env file, and you are good to go.

Step 2: Clone the Laradoc repository.

Clone the following repository in the Laravel project. Remeber, you have to clone the repo. Inside the Laravel project and in our case it is doc folder.

git clone https://github.com/Laradock/laradock.git

It will create the folder called laradock. Now, your project structure looks like this.

 

Laradoc Tutorial

Go into the laradock folder.

cd laradock

Step 3: Rename the files.

Rename env-example to .env.

cp env-example .env

Step 4: Run your containers.

Type the following command in your terminal.

docker-compose up -d nginx mysql phpmyadmin redis workspace

If it is your first time, then it will take 15-20 minutes to install all the containers one by one and create the docker container environment.

It will start the nginx server, mysql serverphpmyadmin.

If it you face any issues related to yarn then merely install the yarn and rerun the docker-compose command.

You can list your running containers using the following command.

docker ps

Also, you can restart your containers using the following command.

docker restart $(docker ps -q)

You can kill all the containers using the following command.

docker kill $(docker ps -a -q)

You can stop all the containers using the following command.

docker stop $(docker ps -aq)

Now, again restart your container, if you have stopped your container.

docker-compose up -d nginx mysql phpmyadmin

Switch to the browser and go to the following URL: http://localhost

laravel docker tutorial

You can see that our Laravel 5.6 is running on that local server. Yikes!! We are now running the Laravel inside the Docker Container.

Access phpMyAdmin

We can access the phpMyAdmin at 8080 port on the browser. http://localhost:8080

It will open up the login page of phpmyadmin and remember, your server is mysql, the username is root, and password is root. 

Create the database, whichever the name, you have written inside the .env file.

Now, you can access the whole laravel project directory using the command.

docker-compose exec workspace bash

Now, go into that project folder, which is a doc. It seems to be weird because we are already inside the doc >> laradoc. But after the above command, you still need to go doc. It is like, we are connected to the remote server, but the reality is that, we are on our local project. So it seems like, we have created a virtual environment for laravel, but in reality, we have just created small containers. That is why Docker is fast then Homestead. Now, go into the doc directory.

cd doc

Now, type the following command to check the folders in this directory.

ls -la

You can see that the structure is similar to Laravel structure. So we are at the root of our project, now migrate the schema to the database using the following command.

php artisan migrate

If you can migrate it correctly then, all of your configurations are working correctly, and you have installed Laravel successfully on Docker.

If you get any error, please verify your mysql server, database name, username, and password.

 

docker phpmyadmin tutorial

So, we have successfully installed Laravel on Docker. So our How To Setup Laravel In Docker Container Tutorial is over. Thanks for taking. Hope you like it. Comments are always welcome.

The post How To Setup Laravel In Docker Container appeared first on AppDividend.

]]>
https://appdividend.com/2018/04/12/how-to-setup-laravel-in-docker-container/feed/ 7