I've finally started learning Docker, so I'll summarize my understanding.
Launch a container that runs express in node.js. I did just this with docker-compose.
cd ~
mkdir sample && cd sample
yarn init -y
yarn add express
touch index.js
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');
});
node index.js
Of course, if you hit the curl command locally, you'll get a reply.
curl http://localhost:3000
Hello
For the time being, I will plot the current situation. For the time being.
To create a docker image of node.js see here. This time is copy. Create the following Dockerfile directly under the sample directory.
Dockerfile
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" ]
And since it is painful if node_modules is also copied, create .dockerignore
as well.
touch .dockerignore
echo 'node_modules' >> .dockerignore
By the way, if you come this far, you don't have to use docker-compose separately.
docker build -t app .
docker run -p 3000:3000 app
If you do, the container will start up. This time, instead of using the above command, launch the container with docker-compose.
Create the following docker-compose.yml directly under the sample directory.
docker-compose.yml
version: '3'
#Specify 3 without thinking about anything
services:
#Describe information about the container. By writing multiple container information here, you can start multiple containers at the same time.
app:
#Launch a container called app (with DNS namespace).
build:
#Information at build time is described here. If you have a Dockerfile, you can refer to it, otherwise write it directly here.
context: .
#This docker at build time-compose.Same directory as yml (.) Refer to the Dockerfile.
#If there are multiple Dockerfiles, it is necessary to specify the directory separately in the context, or specify the name as docokerfile: XXX.
container_name: express-app
#Give it an arbitrary container name. You can use this container name to access the container with the following command
# docker exec -it express-app sh
ports:
- '3000:3000'
# docker run -p 3000:It has the same meaning as 3000.
volumes:
- './:usr/src/app'
#Current directory of local PC (./) And the container~/usr/src/Synchronize the app directory
#Also convenient for development.
docker-compose up
will launch the container.
If you type the curl command from your local PC, you will get a reply from the container. As shown in the first figure.
curl http://localhost:3000
Hello
Do docker-compose down
to stop the container. If you rewrite the contents of ʻindex.js, rebuild it with
docker-compose build`.
Also, if you do docker-compose up -d
, the container will start up in the background, but at this time it may be difficult to see the console.log
in the container, so I like the foreground.
I have summarized the basics during the basics. Thank you very much.
Recommended Posts