0

I have installed docker toolbox recently and created a new container called "superawesomecontainer2" for training porpuses and every time I run this container I have to re-install my package.json and if I used -p 3001:3001 so I can run my server at localhost it's just not working and whenever I shut down this container and re-run it, I have to reinstall my package.json and then it won't reach to my localhost ?

some shits I have tried to solve this problem -remove my package-look.json inside that container -clean cash --force -re-install my packages -re-installing docker it self

my package.json

{
  "name": "facerecognitionapi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "^1.18.3",
    "clarifai": "^2.8.0",
    "cors": "^2.8.4",
    "dotenv": "^8.0.0",
    "express": "^4.16.3",
    "knex": "^0.15.1",
    "pg": "^7.4.3"
  },
  "devDependencies": {
    "nodemon": "^1.17.5"
  }
}

my docker file

FROM node:10.16.0

WORKDIR C:\Users\C.M\Desktop\smart-barin-api

COPY ./ ./

RUN npm install

CMD ["/bin/bash"]

docker info

E:\javascript\facerecognitionapi>docker info
Containers: 38
 Running: 0
 Paused: 0
 Stopped: 38
Images: 51
Server Version: 18.09.6
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.14.116-boot2docker
Operating System: Boot2Docker 18.09.6 (TCL 8.2.1)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 989.4MiB
Name: default
ID: LRFB:QJNW:CCDA:2Y7X:XZWB:FREX:O4JB:D7PN:KWDK:ACPH:B6FC:Y4KY
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
 provider=virtualbox
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

expect: my server runs at localhost 3001 I don't re-install my packages every time I run this container

kyrolos magdy
  • 393
  • 1
  • 4
  • 19

1 Answers1

0

I see several problems in your Dockerfile.

  • WORKDIR: refers to the directory structure inside the container. So it should not contain a Windows like path but something like WORKDIR /app.
  • COPY: since you copy all the file in the current folder to the destination folder, you must include at least in the same folder as the DockerFile: index.js and package.json.
  • EXPOSE: you have to expose the port of your server running in the container, this is done through the EXPOSE directive. For example EXPOSE 8000. Then you have to map the port when running the container -p 8000:8000. Change the port according to your needs or your configuration.
  • CMD: contains the command that will be executed at the start of the container. If it contains /bin/bash it will execute a shell and just exit if you do not run it in interactive mode. So you have to start something like CMD ["npm", "start"]

In conclusion your Dockerfile should more look like this.

FROM node:10.16.0
# create and go to the app dir
WORKDIR /app
# copy all local file to the app dir
COPY ./ ./
# install the app
RUN ["npm", "install"]
# tell docker what port to expose
EXPOSE 8000
# start the server
CMD ["npm", "start"]

And you should build and run it like this:

$ docker build -t mynodeapp:latest .
$ docker run -p 8000:8000 mynodeapp:latest

# ...
# Server listening at http://127.0.0.1:8000/

If you can also check an example here.

Romain
  • 19,910
  • 6
  • 56
  • 65