Skip to main content

Deploy with Docker

Powered by Wechaty Docker Pulls Docker Stars Build Status

Docker allows you to create virtualized sandboxes to run and deploy software. Use Docker to explore ready-to-go containers without worrying about dependencies. Setting up Docker is easier than ever by just pushing a button to start running the Docker one-click app.

dockeri.co

Requirements#

  • Your OS must be Docker compatible, click here to see supported OS versions.
  • If you have installed any older versions of Docker, including docker, docker.io, or docker-engine, uninstall it.
 sudo apt-get remove docker docker-engine docker.io containerd runc

Installation#

The quick and easy way to install Docker is to get the script from get.docker.com and runs it to install the latest stable release of Docker on your system.

Docker installation docs

Getting Started after Deployment#

To get familiar with the Docker Environment, you can try Running hello-world under 3 minutes with Docker.

  1. Run the below command to pull Wechaty in docker:
docker pull wechaty/wechaty

The best practice of using Wechaty Docker is as follows:

$ cat > mybot.ts <<'EOF'import { Wechaty } from 'wechaty'
Wechaty.instance() // Singleton  .on('scan', (qrcode, status) => console.log(`Scan QrCode to login: ${status}\n${qrcode}`))  .on('login',       user      => console.log(`User ${user} logined`))  .on('message',  message      => console.log(`Message: ${message}`))  .start()EOF
$ function wechaty() {  docker run \    -t -i --rm \    --privileged \    --network=host \    -e WECHATY_LOG="$WECHATY_LOG" \    -e WECHATY_PUPPET="$WECHATY_PUPPET" \    -e WECHATY_TOKEN="$WECHATY_TOKEN" \    --mount type=bind,source="$(pwd)",target=/bot \    wechaty/wechaty:latest \    "$@"}
$ wechaty mybot.ts

Know the commands used above#

  • -t : Allocate a pseudo-TTY
  • -i : Keep STDIN open even if not attached
  • --rm : Automatically remove the container when it exits
  • --privileged : Give extended privileges to this container
  • --network=host : use the Docker host network stack
  • -e WECHATY_LOG="$WECHATY_LOG" : Pass the environment variable WECHATY_LOG into the container
  • --volume="$(pwd)":/bot : Bind current directory("$(pwd)") to '/bot' inside the container, by mounting the volume
  • --name=wechaty : Assign wechaty as the container name
  • wechaty/wechaty:latest : Image name on docker hub, here's our wechaty/wechaty with latest version
  • mybot.js : File contains code wrote by you, should be placed in current directory ./

See Also: Dockerize Wechaty for easy start #66

Examples#

Get started with various Examples written in JavaScript. Run example ChatBot is as easy as run hello world example above, as long as you are using Docker:

cd examplewechaty ding-dong-bot.js

Run Wechaty as a Hostie#

Running your wechaty as a hostie requires WECHATY_TOKEN so as to manage wechaty on the chatbot cloud manager: https://www.chatie.io

export WECHATY_TOKEN="your token here"docker run -e WECHATY_TOKEN="$WECHATY_TOKEN" wechaty/wechaty

Additional#

Onbuild#

Put this line(and only this line) to your Dockerfile:

FROM wechaty/onbuild

This image makes building derivative images easier. For most use cases, creating a Dockerfile in the base of your project directory with the line FROM wechaty/onbuild will be enough to create a stand-alone image for your project.

  1. The onbuild variant is really useful for "getting off the ground running" (zero to Dockerized in a short period of time)
  2. This onbuild variant will only install npm packages according to the package.json
  3. The npm installs devDependencies by default, which is undesirable if you're building a production image. To avoid this pass NODE_ENV as a build argument i.e. docker build --build-arg NODE_ENV=production ….

Build#

docker build -t wechaty

Next Steps#

Now that you’ve learned how to install docker and work with your Wechaty Docker. See the following guides to begin:

  1. Wechaty Getting Started: https://github.com/wechaty/wechaty-getting-started
  2. Heroku Wechaty Getting Started: https://github.com/wechaty/heroku-wechaty-getting-started
  3. Docker Wechaty GitHub repo: https://github.com/wechaty/docker-wechaty-getting-started