Summary of Docker understanding by beginners ③ ~ Until proxying API using nginx ~

Introduction

I've finally started learning Docker, so I'll summarize my understanding.

Note what you learned today

Launch a container that runs express with node.js and a container that runs nginx on your local PC. Then hit the express API via nginx. I did just this with docker-compose.

Difference between last time and this time

Last time had to be ported to access the express container.

curl http://localhost:3000
Hello

スクリーンショット 2020-10-22 22.22.35.png

This time, using the proxy function of nginx, I made it possible to access the express container without specifying the port.

curl http://:localhost
Hello

スクリーンショット 2020-10-23 8.32.39.png

Overview of development

cd ~
mkdir sample

Develop in the sample directory. The contents of the directory look like this. The app and web directories are the express and nginx containers, respectively. スクリーンショット 2020-10-23 8.24.02.png

index.js


const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello').status(200);
});

app.listen(3000, () => {
  console.log('Listening on port 3000');
});

Express backend Dockerfile preparation

FROM node:alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .
# package.I don't want to do npm install after copying json every time I build.
#Therefore, after finishing the above, COPY. .Do it.
#Then the build will be executed only for the part where the code is changed.

EXPOSE 3000
CMD [ "node", "index.js" ]

Preparation of nginx.conf

Proxy requests coming to all endpoints (/) of localhost 80port (that is, http default port) in the nginx container to http: // app: 3000. Note that the domain of the app container is app under the control of Docker. By the way, this setting file name is fixed in nginx.conf. If the location is under the web directory, it doesn't matter.

nginx.conf



user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 1024;
}

http {
	# Weather Report Reverse Proxy
         server {
            listen 80;
            server_name localhost 127.0.0.1;

            location / {
                proxy_pass          http://app:3000;
                proxy_set_header    X-Forwarded-For $remote_addr;
            }

        }
}

Preparing docker-compose.yml

docker-compose.yml


version: '3'
#Specify 3 without thinking about anything
services:
  app:
    build:
      context: ./app
      # docker-compose.Seen from yml./Build according to the Dockerfile in the app
    container_name: express-app
    #Give a container name for the time being
    ports:
      - '3000:3000'
      # docker run -p 3000:With 3000 app
    volumes:
      - './app:/usr/src/app'
      #On the local PC./The app folder and inside the container~/usr/src/Synchronize the app folder.
      # docker exec -it express-You can check with app sh

  web:
    image: nginx:latest 
    #nginx:An error will occur with alpilne, so the latest version will be used.
    container_name: nginx-web
    ports:
      - '80:80'
    volumes:
      - './web/reverse_proxy/nginx.conf:/etc/nginx/nginx.conf'
      #On the local PC./web/reverse_proxy/nginx.conf and in the container~/etc/nginx/nginx.Synchronize conf.
      # ~/etc/nginx/nginx.conf is a location specification.
    links:
      - app
    depends_on:
      - app
      #Clarified the dependency that it will not work unless the app container is started.

docker-compose up and finish

docker-compose up will launch the container. If you type the curl command from your local PC, you will get a reply from the app container. As shown in the first figure.

curl http://localhost
Hello

By the way, you can also access the app container directly. Of course.

curl http://localhost:3000
Hello

Finally

I have summarized the basics during the basics. By the way, I was desperate because nginx did not start well no matter how many times I tried, but when I did docker ps, a mysterious container called k8s_controller_ingress-nginx-controller (I think it is an ingress of kubernetes) I noticed that it doesn't stop even if I docker stop. Restarting kubernetes fixed it, so if there is such a situation, I think it's a good idea to try it.

スクリーンショット 2020-10-23 9.33.31.png

Thank you very much.

Recommended Posts

Summary of Docker understanding by beginners ③ ~ Until proxying API using nginx ~
Summary of Docker understanding by beginners ② ~ docker-compose ~
Summary of Docker understanding by beginners ① ~ docker run -p ~
Summary of Docker understanding by beginners ⑤ ~ Until deploying docker container on EC2 instance ~
Summary of Docker understanding by beginners ④ ~ Until EC2 instance is started and docker is installed ~
Summary of Docker understanding by beginners ⑥ ~ Until automatic deployment of docker container to EC2 instance using CodeDeploy and CodePipeline ~
Simple installation of nginx and Docker using ansible
Summary of using FragmentArgs
Summary of using DBFlow
Until you start nginx on CentOS using Docker on Mac OS
[Java] Beginner's understanding of Servlet-②
Summary of using Butter Knife
Object-oriented summary by beginners (Java)
[For beginners] Summary of java constructor
[Rails] Introduction of Rubocop by beginners
Development of Flink using DataStream API
Summary of frequently used Docker commands
Summary of object-oriented programming using Java
Build an environment of "API development + API verification using Swagger UI" with Docker