Skip to content

Latest commit

 

History

History
103 lines (76 loc) · 2.9 KB

3. Creating the Dockerfile and Running!.md

File metadata and controls

103 lines (76 loc) · 2.9 KB

Creating the Dockerfile

Remember in the last tutorial when we had ran touch Dockerfile to create a file in our project called Dockerfile? Well this is where Docker gets the information it needs to create a new container for our bot.

Opening the Dockerfile, it should be blank. Well, let's get started!

The first thing we need to do is define the image (or operating system/project) our bot will be using.

Since we use the latest from NodeJS, our first line should be:

FROM node:latest

Now we need to create a directory from our bot to run inside (which is called the working directory)

# Create the directory!
RUN mkdir -p /usr/src/bot
WORKDIR /usr/src/bot

That's great and all, but we also need to tell Docker where our bot is.

# Copy and Install our bot
COPY package.json /usr/src/bot
RUN npm install

But.. that just installs the packages. We need to now copy our project over as well. So let's add that as well!

# Our precious bot
COPY . /usr/src/bot

Our bot is now inside a container! Nyah!~~~~

.. but wait. Where and how is it going to run? Well now, we need to tell docker how to run the bot. Duh! Inside the Dockerfile, you'll add the following:

# Start me!
CMD ["node", "index.js"]

Huzzah! We have successfully created a docker file for our bot. Now our bot container will know what to do on startup and be all happy and plush.

Now let's take a look at the finished product!

FROM node:latest

# Create the directory!
RUN mkdir -p /usr/src/bot
WORKDIR /usr/src/bot

# Copy and Install our bot
COPY package.json /usr/src/bot
RUN npm install

# Our precious bot
COPY . /usr/src/bot

# Start me!
CMD ["node", "index.js"]

Building our container

Now, inside our bot's directory we need to now build this nice little cozy container for our bot.

We will be executing:

$ docker build -t my-bot .

If you want a sanity check, you can run docker images and it should be visible under that list.

Running our container

Running our bot with -d runs the container in detatched mode (as in it runs in the background). If you want to see what is happening, remove that option. Let's get to it! Running the following:

$ docker run -d my-bot

Will deploy our bot to it's own nice little server to play with.

More information

If you want more of a sanity check here are some following commands you can run!

# Get the container!
$ docker ps

# Print the logs
$ docker logs <our container's ID>

That will give us our information and current running logs.

If you need to get inside the container (and it's tempting, I know) you can run:

$ docker exec -it <container id> /bin/bash

That will put us inside our bot's little world so we can play around with it.

In the next tutorial, I will cover docker-compose and setting up our bot with it's own private little PostgreSQL container!