Codeship Using Docker and Codeship For NodeJS Development
Codeship Using Docker and Codeship For NodeJS Development
-2-
Share this
Codeship Guid
e
The first part of this eBook will cover developing and testing
a NodeJS app with Docker compose. The second part will
focus on how to further create a CI/CD pipeline for your app
using Codeship.
-3-
Share this
Codeship Guid
e
-4-
Share this
Codeship Guid
e
1 FROM node:7.7.2-alpine
-5-
Share this
Codeship Guid
e
1 FROM node:7.7.2-alpine
2
3 WORKDIR /usr/app
4
CODE
5 COPY package.json .
6 RUN npm install --quiet
7
8 COPY . .
-6-
Share this
Codeship Guid
e
You can now run docker build . again, and see the
results:
-7-
Share this
Codeship Guid
e
-8-
Share this
Codeship Guid
e
1 version: '2'
2 services:
3 web:
4 build: .
5 command: npm run dev
6 volumes:
7 - .:/usr/app/
8 - /usr/app/node_modules
9 ports:
CODE
10 - "3000:3000"
11 depends_on:
12 - postgres
13 environment:
14 DATABASE_URL: postgres://todoapp@postgres/todos
15 postgres:
16 image: postgres:9.6.2-alpine
17 environment:
18 POSTGRES_USER: todoapp
19 POSTGRES_DB: todos
-9-
Share this
Codeship Guid
e
- 10 -
Share this
Codeship Guid
e
- 11 -
Share this
Codeship Guid
e
This will tell you the name of the services, the command
used to start it, its current state, and the ports.
Notice nodejsexpresstodoapp_web_1 has listed the port
as 0.0.0.0:3000->3000/tcp . This tells us that you can
access the application using localhost:3000/todos on
the host machine.
2
3 []
- 12 -
Share this
Codeship Guid
e
Feel free to check out the source code, play around a bit,
and see how things go for you.
- 13 -
Share this
Codeship Guid
e
1 /> docker-compose -p tests run -p 3000 --rm web npm run watch-tests
- 14 -
Share this
Codeship Guid
e
Codeship account
Codeship Jet CLI
Heroku account
GitHub, Bitbucket, or GitLab account
- 15 -
Share this
Codeship Guid
e
- 16 -
Share this
Codeship Guid
e
1 web:
2 build: .
3 links:
4 - postgres
5 environment:
6 DATABASE_URL: postgres://todoapp@postgres/todos
CODE
7 cache: true
8 postgres:
9 image: postgres:9.6.2-alpine
10 environment:
11 POSTGRES_USER: todoapp
12 POSTGRES_DB: todos
- 17 -
Share this
Codeship Guid
e
1 - type: parallel
2 steps:
3 - name: lint
4 service: web
CODE
Bin scripts
Now that things are more automated, we end up
introducing a small race case that we can easily get
around with some simple scripts. As the project is built,
the integration tests require that Postgres is running
to perform the migrations. We can't guarantee that will
happen with the codeship-services.yml file alone. One
approach is to check that the database is available before
starting the migration.
- 18 -
Share this
Codeship Guid
e
wait-for postgres
The contents of the wait-for-postgres script are the
following:
1 #!/bin/sh
2 # wait-for-postgres
3 set -e
4
5 TIMEOUT=60
6 COUNT=0
7
CODE
- 19 -
Share this
Codeship Guid
e
CODE
ci
The ci script is as follows:
1 #!/bin/sh
2 set -e
3
CODE
4 bin/wait-for-postgres
5 time npm run migrate
6
7 time $1
Not too much heavy lifting here. This script runs the
wait-for-postgres script to finish up, then it will
perform the migration, and the last line takes the first
parameter and will run that. This allows me to change a
line in codeship-steps.yml to run this script first.
1 - name: tests
CODE
2 service: web
3 command: bin/ci "npm test -- --forceExit" # change the command here
- 20 -
Share this
Codeship Guid
e
If you see the final result above, you have set everything
up correctly. If you receive instead type:STEP_FINISHED_
TYPE_ERROR , something didn't go right and you should
check some things. Codeship Jet CLI will produce a log
that you can review to try to locate the problem. Having
the ability to catch any errors prior to pushing to your
repository makes this a particularly powerful tool in the
Codeship arsenal.
- 21 -
Share this
Codeship Guid
e
Create project
When your repo is ready to go, you can now set up the
Codeship project.
- 22 -
Share this
Codeship Guid
e
- 23 -
Share this
Codeship Guid
e
- 24 -
Share this
Codeship Guid
e
- 25 -
Share this
Codeship Guid
e
Make sure you have added all of your files to your repo.
Review your codeship-services.yml and
codeship-steps.yml files.
Run Codeship Jet CLI locally to double-check it works
locally.
- 26 -
Share this
Codeship Guid
e
1. Click Resources.
2. Under Add-ons, search for postgres .
3. In the results dropdown, click Heroku Postgres.
4. Leave the selection as Hobby Dev – Free, then click
Provision.
- 27 -
Share this
Codeship Guid
e
1. Click Projects, then the project you are currently working in.
2. Click Settings, then click General.
3. Locate and copy the AES Key .
- 28 -
Share this
Codeship Guid
e
NOTE: Make sure that both codeship.aes and deployment.env are ignored in
your .gitignore file. Since they contain sensitive data, you don't want these to be
pushed into your repository.
1 web:
2 build: .
CODE
3 links:
4 - postgres
- 29 -
Share this
Codeship Guid
e
5 environment:
6 DATABASE_URL: "postgres://todoapp@postgres/todos"
7 cached: true
8 postgres:
9 image: postgres:9.6.2-alpine
10 environment:
CODE
11 POSTGRES_USER: todoapp
12 POSTGRES_DB: todos
13 deploy: # added this service
14 image: codeship/heroku-deployment
15 encrypted_env_file: deployment.env.encrypted
16 volumes:
17 - ./:/deploy
1 - type: parallel
2 steps:
3 - name: lint
4 service: web
CODE
- 30 -
Share this
Codeship Guid
e
11 service: deploy
12 command: codeship_heroku deploy /deploy nodejs-express-todoapp
- 31 -
Share this
Codeship Guid
e
1 git add .
CODE
- 32 -
Share this
Codeship Guid
e
Conclusion
Docker
Docker Community Edition
Dockerfile Reference
Docker Compose Overview
Docker Compose CLI
- 33 -
Share this
Codeship Guid
e
Codeship Pro
Codeship Pro Documentation
Codeship Jet CLI Docs
Encrypting Environment Variables
Codeship Steps Docs
Codeship Services Docs
Article Resources
Using Docker Compose for NodeJS Development
NodeJS Express Todo App Repo
Alpine Based Docker Images Make a Difference in Real
World Apps
- 34 -
Share this
Codeship Guid
e
- 35 -
Share this
Codeship Guid
e
About Codeship.
Codeship is a hosted Continuous Integration service that fits all your needs.
Codeship Basic provides pre-installed dependencies and a simple setup UI
that let you incorporate CI and CD in only minutes. Codeship Pro has native
Docker support and gives you full control of your CI and CD setup while
providing the convenience of a hosted solution.