Deploy with Docker
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.
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
, ordocker-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.
- To install Docker using the repository, visit How to install using repository.
- To install Docker from a package, visit How to install using a package.
Getting Started after Deployment
To get familiar with the Docker Environment, you can try Running hello-world under 3 minutes with Docker.
- 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 variableWECHATY_LOG
into the container--volume="$(pwd)":/bot
: Bind current directory("$(pwd)"
) to '/bot
' inside the container, by mounting the volume--name=wechaty
: Assignwechaty
as the container namewechaty/wechaty:latest
: Image name on docker hub, here's our wechaty/wechaty withlatest
versionmybot.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 example
wechaty 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.
- The
onbuild
variant is really useful for "getting off the ground running" (zero to Dockerized in a short period of time) - This
onbuild
variant will only install npm packages according to thepackage.json
- 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:
- Wechaty Getting Started: https://github.com/wechaty/wechaty-getting-started
- Heroku Wechaty Getting Started: https://github.com/wechaty/heroku-wechaty-getting-started
- Docker Wechaty GitHub repo: https://github.com/wechaty/docker-wechaty-getting-started