1. 拉取镜像
先从 Docker Hub
上把 Node
镜像 pull
到本地.
docker pull node
$ docker pull node Using default tag: latest latest: Pulling from library/node 61be48634cb9: Pull complete fa696905a590: Pull complete b6dd2322bbef: Pull complete 32477089adb4: Pull complete febe7209ec28: Pull complete 4364cbe57162: Pull complete 437859acfd49: Pull complete d8268e1e433b: Pull complete Digest: sha256:00a7fb3df8e94ed24f42c2920f132f06e92ea5ed69b1c5e53c4bb3d20e85a3e2 Status: Downloaded newer image for node:latest
2. 下载成功后,终端中查看
docker images
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE node latest a2b9536415c2 2 weeks ago 674MB ...
3. 交互模式运行镜像,使用 -v
映射当前目录到容器中的 workspace
目录
docker run -ti --name node-1 -v $(pwd):/workspace node bash
$ docker run -ti --name node-1 -v $(pwd):/workspace node bash root@7dc31bb3344b:/# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr workspace root@7dc31bb3344b:/# cd workspace/ root@7dc31bb3344b:/workspace# cd Project-Server/ root@7dc31bb3344b:/workspace/Project-Server# ls JS WeWoAppEngine package.json server.js root@7dc31bb3344b:/workspace/Project-Server# cd JS/ root@7dc31bb3344b:/workspace/Project-Server/JS# ls call.js index.js package.json yarn.lock
4.查看 node
yarn
npm
版本号
root@7dc31bb3344b:/workspace/Project-Server/JS# node -v v10.12.0 root@7dc31bb3344b:/workspace/Project-Server/JS# yarn -v 1.10.1 root@7dc31bb3344b:/workspace/Project-Server/JS# npm -v 6.4.1
5. 在项目前端工程目录下执行 yarn install
安装依赖
root@7dc31bb3344b:/workspace/Project-Server/JS# yarn install yarn install v1.10.1 [1/4] Resolving packages... [2/4] Fetching packages... [3/4] Linking dependencies... [4/4] Building fresh packages... warning Your current version of Yarn is out of date. The latest version is "1.12.1", while you're on "1.10.1". info To upgrade, run the following command: $ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash Done in 5.86s.
6. 运行程序脚本
node index.js
root@7dc31bb3344b:/workspace/Project-Server/JS# node index.js running ...
npm run serve 运行前端项目, 需要映射对应的端口,否则无法在容器外访问
docker run -ti -p 8080:8080 --name node-1 -v $(pwd):/workspace node bash
Node 官方镜像地址: hub.docker.com/r/_/node/