FROM node:latest
WORKDIR /app
COPY . .
ENV PORT=4000
RUN npm install
EXPOSE $PORT
ENTRYPOINT ["node", "app.js"]
NB: Entrypoint should be the start command for your app
Docker Commands
docker build -t paulvitalis/node:latest .
- Builds an imagedocker run paulvitalis/node
- Runs a docker imagedocker run -p 5000:4000 paulvitalis/node
- Runs a docker image on the specified portdocker run -d paulvitalis/node
- Runs a docker image in daemon modedocker ps
- Lists all the running containersdocker rm [id]
- Removes a certain containerdocker images
- Lists all the running imagesdocker stop [id]
- Stops a certain running containerdocker exec -it [id] bash
- Starts a docker interactive bash of the specific containerdocker run --name sidehustle-image -p 4680:3000 -d paulvitalis/sidehustle-image
- Runs a docker image on the specified port in daemon mode
Build containers by giving them names as follows:
docker run -d -p 8000:3000 --name my-container paulvitalis/node
Chain commands to stop, remove, build and run a container again e.g
docker stop my-container && docker rm my-container && docker build -t chrisnoring/node . && docker run -d -p 8000:3000 --name my-container chrisnoring/node
But that is not efficient. We, therefore, use volumes Volumes or data volumes is a way for us to create a place in the host machine where we can write files so they are persisted.
Creating volumes
docker volume create [name of volume]
To list out volumes
docker volume create [name of volume]
To remove unused volumes
docker volume prune
To remove a single volume
docker volume rm [name of volume]
To inspect volumes
docker inspect [name of volume]
Used in conjuncture with running a container it would look like this for example:
docker run -d -p 8000:3000 --name my-container --volume [name-of-volume]:/[directory in container] paulvitalis/node
To locate your volume
docker exec -it my-container bash
Next you navigate to the directory that you specified
Steps
- Create a directory, let’s create a directory /logs
- create a file, let’s create a file logs.txt and write some text in it
- run our container, let’s create a mount point to our local directory + /logs
The run command is as follows
docker run -p 8000:4000 --name my-container --volume $(pwd)/logs:/logs paulvitalis/node
The first argument is $(pwd)/logs which means our current working directory and the subdirectory logs. The second argument is /logs which means we are saying mount our host computers logs directory to a directory with the same name in the container.
If we edit the logs.txt file and run cat logs.txt we notice a different outcome
Run docker kill my-container && docker rm my-container
first
Thereafter we need to rerun our container this time with a different volume argument namely
--volume $(PWD):/app
The whole command looks as follows:
docker run -p 8000:4000 --name my-container --volume $(pwd):/app paulvitalis/node
When we now add a route in our app or make a change and run the app, our changes will be reflected NB: Ensure you have nodemon running
Commands
- Make sure that your mysql server is runnin.
brew services start
- Connect to a database by running
mysql -uroot
- Once in the mysql terminal, you can create a database by typing:
CREATE database_name
- Select the new database name to query from it by typing:
USE database_name
- Create a table and link it to our database
- The problem we get that our container is up and running despite the error it threw
- So we bring down the container first using:
docker rm mysql-db
- Now we can run it again with the database set as above:
docker run -d -p 8001:3006 --name mysql-db -e MYSQL_ROOT_PASSWORD=complexpassword mysql
- To connect to it from the outside, we connect to it on 0.0.0.0:8001:
mysql -uroot -pcomplexpassword -h 0.0.0.0 -P 8001
Connecting to database from Node.js
- First we install the npm package for MYSQL:
npm install mysql
- Then we add the following code to the top of our app.js file:
Linking
- To link a container to a database, We update the app.js to look as follows
- We build our image
docker build - t paulvitalis/node .
- Then we run our image
docker run -d -p 8000:3000 --name my-container --link mysql-db:mysql paulvitalis/node
- To see the output we run
docker logs my-container