I've finally started learning Docker, so I'll summarize my understanding.
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.
Last time had to be ported to access the express container.
curl http://localhost:3000
Hello
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
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.
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');
});
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" ]
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;
}
}
}
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
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
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.
Thank you very much.
Recommended Posts