Docker-compose for projects

How use docker-compose to run MySQL database base for development


In the past docker posts we used docker run command to run containers using docker image and also the Dockerfile to create project image.

Sometimes we need to use/run a database in container during development, in such cases docker-compose came into light.

The Docker-Compose file

Docker-compose is a file at the root of your project, which has no extension and carry some special script. In our example we are configuring MySQL database.

version: '3.1'
services:
  db: 
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: example
      MYSQL_USER: development
      MYSQL_PASSWORD: development
      MYSQL_DATABASE: blog
    ports: 
      - 127.0.0.1:3306:3306

Up and Down

To start the docker-compose use the up command from the project root

docker-compose up -d

The down command used to remove and stop for stopping the container .

Checking the docker-compose containers

For listing the containers we can use

docker-compose ps

Accessing MySQL CLI

Login into the database and create some database ( as root)

docker exec -i some-id sh -c "exec -uroot -p password"

You can also use -u username for login as normal user and can execute basic sql operations such create database, tables and CURD operations on objects etc. Note that the database uses the default port of MySQL

We can stop running a container at any time using docker stop <id/name> and resume with docker start <id/name>

Following Docker Post may deserve a good read

Create a docker image of Node-Express API – Part II

How to build Node-Express project Docker Image using Node base image


In this post which is the continuation of Node-API Docker Image ( the Links are at the end of the post), create and run container using the image we build.

Lets check the existence of the image in docker using

docker image

Create the container

To build a container to run the API we need only the Image , all the depending Image will be automatically download by Docker.

docker run --name customer -d -p 3000:3000 user-api:latest

The above command will run, customer API in detached mode on port 3000.

Stop and Resume the container

We can stop running a container at any time using docker stop <id/name> and resume with docker start <id/name>

Following Docker Post may deserve a good read

Create a docker image of Node-Express API – Part I

How to build Node-Express project Docker Image.


In this post we are create Docker Image of a NodeExpress app which can be used to deploy and run the app. The image can be used to deploy multiple container as well. Following are the requirements for this tutorial

Requirements

  • NPM
  • Nodejs
  • VSCode/Atom

Node-Express API

Lets create a folder and create file index.js and paste the following API site content

const express = require('express')
const app=express()
require("dotenv").config();

app.get('/',(req,res)=>
  res.json([{name:'manoj',email:'codehat@outlook.com'
  },
  {name:'mariah',email:'codehat@outlook.com'
  }])
)
app.listen(process.env.PORT || 3000,()=>console.log(`Server running on port ${process.env.PORT}`))

Our API is a simple USER API which shows a list of users. Let’s setup the package.json file by issuing npm command and install dependencies

Howto to stop all containers at once in docker

How to stop all containers in docker


Docker is a developer tool which help us to run applications in an isolated environment, where you can start a service, app,local host, MySQL, SQL server, mongodb etc.

Get the container id

To stop container we can use ids of container they, can be , many. First thing we need grab all the ids using the

docker pa -aq

Then use those ids as arguments to stop command as follows

docker stop $(docker ps -aq)

that’s it

Docker posts that may help you

Howto to remove all containers at once in docker

How to remove all containers in docker


Docker is a developer tool which help us to run applications in an isolated environment, where you can start a service, app,local host, MySQL, SQL server, mongodb etc.

Get the container id

To remove container we can use rm command with ids of container. First thing we need grab all the ids using the

docker pa -aq

Then use those ids as arguments to stop command as follows

docker rm $(docker ps -aq)

that’s it

Docker posts that may help you