Wednesday, July 24, 2019

Simple docker-compose example

Create a barebone Node.js app by following this: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/


Then update this section of code:

// App
const app = express();
app.get('/', (req, res) => {
  const message = `Hello world: ${process.env.GREETING_MESSAGE}`;
  res.send(message);
});


Create docker-compose.yml:

version: '3'
services:
  spiderman: 
    build: .
    ports:
        - "80:8080"
    environment:
        GREETING_MESSAGE: 你好世界


To test:
$ docker-compose up -d

Output:
Developers-iMac:experiment-nodejs-docker-compose dev$ docker-compose up -d
Creating network "experiment-nodejs-docker-compose_default" with the default driver
Building spiderman
Step 1/7 : FROM node:10
 ---> e05cbde47b8f
Step 2/7 : WORKDIR /usr/src/app
 ---> Running in 95c12e65805a
Removing intermediate container 95c12e65805a
 ---> be613c39b5ab
Step 3/7 : COPY package*.json ./
 ---> 619110050a58
Step 4/7 : RUN npm install
 ---> Running in e32232fc4928
npm WARN docker_web_app@1.0.0 No repository field.
npm WARN docker_web_app@1.0.0 No license field.

added 50 packages from 37 contributors and audited 126 packages in 1.148s
found 0 vulnerabilities

Removing intermediate container e32232fc4928
 ---> 92b713b4c799
Step 5/7 : COPY . .
 ---> ee82daff1c05
Step 6/7 : EXPOSE 8080
 ---> Running in a797e79b2f85
Removing intermediate container a797e79b2f85
 ---> 838f931be1ea
Step 7/7 : CMD [ "node", "server.js" ]
 ---> Running in c3b932bb7e0c
Removing intermediate container c3b932bb7e0c
 ---> 5a0d42751576
Successfully built 5a0d42751576
Successfully tagged experiment-nodejs-docker-compose_spiderman:latest
WARNING: Image for service spiderman was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating experiment-nodejs-docker-compose_spiderman_1 ... done


List the created docker image (think OOP's class):
Developers-iMac:experiment-nodejs-docker-compose dev$ docker images
REPOSITORY                                   TAG                 IMAGE ID            CREATED             SIZE
experiment-nodejs-docker-compose_spiderman   latest              5a0d42751576        3 minutes ago       906MB
node                                         10                  e05cbde47b8f        6 weeks ago         904MB



List the docker image's instance (think class' instance):
Developers-iMac:experiment-nodejs-docker-compose dev$ docker ps
CONTAINER ID        IMAGE                                        COMMAND                  CREATED             STATUS              PORTS                  NAMES
35c7818a4704        experiment-nodejs-docker-compose_spiderman   "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->8080/tcp   experiment-nodejs-docker-compose_spiderman_1


Visiting 0.0.0.0 (a.k.a. 127.0.0.1, a.k.a. localhost) on browser:



No comments:

Post a Comment