mirror of
https://github.com/bitnami/containers.git
synced 2026-03-10 15:09:17 +08:00
readme: rebased on the bitnami/bitnami-docker-rails README
This commit is contained in:
@@ -4,19 +4,19 @@ We increasingly see developers adopting two strategies for development. Using a
|
||||
|
||||
If you’ve never tried to start a project with containers before, or you have tried it and found the advice, tools, and documentation to be chaotic, out of date, or wrong, then this tutorial may be for you.
|
||||
|
||||
In this tutorial we walk you through using the Bitnami docker images during the development lifecycle of a Ruby on Express application.
|
||||
In this tutorial we walk you through using the Bitnami docker images during the development lifecycle of a Express application.
|
||||
|
||||
# Why Docker?
|
||||
|
||||
We think developers are adopting containers for development because they offer many of the same advantages as developing in VMs, but with lower overhead in terms of developer effort and development machine resources. With Docker, you can create a development environment for your code, and teammates can pull the whole development environment, install it, and quickly get started writing code or fixing bugs.
|
||||
|
||||
Docker development environments are more likely to be reproducible than VMs because the definition of each container and how to build it is captured in a dockerfile.
|
||||
Docker development environments are more likely to be reproducible than VMs because the definition of each container and how to build it is captured in a Dockerfile.
|
||||
|
||||
Docker also has a well known and standard API so tools and cloud services are readily available for docker containers.
|
||||
|
||||
# The Bitnami Approach
|
||||
|
||||
When we designed and built our development containers, we kept a the following guiding principles in mind:
|
||||
When we designed and built our development containers, we kept the following guiding principles in mind:
|
||||
|
||||
1. Infrastructure should be effort free. By this, we mean, there are certain services in an application that are merely configured. For example, databases and web servers are essential parts of an application, but developers should depend on them like plumbing. They should be there ready to use, but developers should not be forced to waste time and effort creating the plumbing.
|
||||
|
||||
@@ -24,148 +24,126 @@ When we designed and built our development containers, we kept a the following g
|
||||
|
||||
# Assumptions
|
||||
|
||||
Before you start, we are assuming that you have [Docker Engine](https://www.docker.com/products/docker-engine), [Docker Compose](https://www.docker.com/products/docker-compose) and [Docker Machine](https://www.docker.com/products/docker-machine) properly set up.
|
||||
First, we assume that you have the following components properly setup:
|
||||
|
||||
- [Docker Engine](https://www.docker.com/products/docker-engine)
|
||||
- [Docker Compose](https://www.docker.com/products/docker-compose)
|
||||
- [Docker Machine](https://www.docker.com/products/docker-machine)
|
||||
|
||||
> The [Docker documentation](https://docs.docker.com/) walks you through installing each of these components.
|
||||
|
||||
We also assume that you have some beginner-level experience using these tools.
|
||||
|
||||
> **Note**:
|
||||
> If you are using Linux as your host OS you will not need to setup a Docker Machine
|
||||
|
||||
> Docker Machine also requires a [driver](https://docs.docker.com/machine/drivers/) to create a Docker Machine VM. We'll be using the [virtualbox](https://docs.docker.com/machine/drivers/virtualbox/) driver. Please download and install the latest version of [Oracle VirtualBox](https://www.virtualbox.org).
|
||||
|
||||
Open a terminal and try these commands:
|
||||
|
||||
```bash
|
||||
$ docker version
|
||||
$ docker-compose version
|
||||
$ docker-machine version
|
||||
```
|
||||
|
||||
The above commands will display the version string for each of the docker components. Additionally since we'll be using VirtualBox to create the Docker Machine VM, you can use the following command to print the version string of VirtualBox:
|
||||
|
||||
```bash
|
||||
$ VBoxManage --version
|
||||
```
|
||||
>
|
||||
> If your host OS is Linux you may skip setting up Docker Machine since you'll be able to launch the containers directly in the host OS environment.
|
||||
|
||||
Further, we also assume that your application will be using a database. In fact, we assume that it will be using MongoDB. Of course, for a real project you may be using a different database, or, in fact, no database. But, this is a common set up and will help you learn the development approach.
|
||||
|
||||
## Create a Docker Machine
|
||||
## Download the Bitnami Orchestration File for Express development
|
||||
|
||||
> **Note**:
|
||||
> Skip this section if you are using a Linux host.
|
||||
|
||||
We'll begin by creating a new Docker Machine named `express-dev` provisioned using VirtualBox and is where our MongoDB and Express containers will be deployed.
|
||||
We assume that you're starting the development of the [Express](http://expressjs.com/) application from scratch. So lets begin by creating a directory for the application source where we'll be bootstrapping a Express application:
|
||||
|
||||
```bash
|
||||
$ docker-machine create --driver virtualbox express-dev
|
||||
$ mkdir ~/workdir/myapp
|
||||
$ cd ~/workdir/myapp
|
||||
```
|
||||
|
||||
Next, import the Docker Machine environment into your terminal using:
|
||||
|
||||
```bash
|
||||
$ eval $(docker-machine env express-dev)
|
||||
```
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> The above command should be executed whenever you create a new terminal to import the Docker Machine environment.
|
||||
|
||||
To verify that the Docker Machine up and running, use the following command:
|
||||
|
||||
```bash
|
||||
$ docker info
|
||||
```
|
||||
|
||||
If everything has been setup correctly, the command will query and print status information of the Docker daemon running in the `express-dev` Docker Machine.
|
||||
|
||||
## Download a Bitnami Orchestration File
|
||||
|
||||
For this tutorial we'll be using the orchestration file for Node + Express.
|
||||
|
||||
Begin my creating directory for our Express application source.
|
||||
|
||||
```bash
|
||||
$ mkdir ~/myapp
|
||||
$ cd ~/myapp
|
||||
```
|
||||
|
||||
Next, download the orchestration file in this directory.
|
||||
Next, download our Docker Compose orchestration file for Express development:
|
||||
|
||||
```bash
|
||||
$ curl -L "https://raw.githubusercontent.com/bitnami/bitnami-docker-express/master/docker-compose.yml" > docker-compose.yml
|
||||
```
|
||||
|
||||
The orchestration file creates a Node + Express service named `myapp`. The service volume mounts the current working directory at the path `/app` of the Express container. If the mounted directory doesn't contain application source, a new Express application will be bootstrapped in this directory, following with the NPM dependencies installation and database setup tasks will be executed before starting the Node server on port `3000`.
|
||||
|
||||
The bootstrapped application does not require any database to work but for convenience the orchestration file also includes a MongoDB database ready to be used. You can find an example of how to connect your new Express application to MongoDB in `config/mongodb.js`.
|
||||
|
||||
> We encourage you to take a look at the contents of the orchestration file to get an idea of the services that will be started for Express development.
|
||||
|
||||
## Run
|
||||
|
||||
Lets put the orchestration file to the test:
|
||||
|
||||
```bash
|
||||
$ docker-compose up -d
|
||||
$ docker-compose up
|
||||
```
|
||||
|
||||
This command will begin download the Bitnami Docker images and start the services defined in the orchestration file. This process can take a couple of minutes to complete.
|
||||
This command reads the contents of the orchestration file and begins downloading the Docker images required to launch each of the services listed therein. Depending on the network speeds this can take anywhere from a few seconds to a couple minutes.
|
||||
|
||||
> **TIP**
|
||||
>
|
||||
> View the container logs using:
|
||||
>
|
||||
> ```bash
|
||||
> docker-compose -f logs
|
||||
> ```
|
||||
After the images have been downloaded, each of the services listed in the orchestration file is started, which in this case are the `mongodb` and `myapp` services.
|
||||
|
||||
Get the IP address of the Docker Machine VM using:
|
||||
As mentioned earlier, the `mongodb` service provides a database backend which can be used for the development of a data-driven Express application. The service is setup using the [bitnami/mongodb](https://github.com/bitnami/bitnami-docker-mongodb) docker image and is configured with the [default credentials](https://github.com/bitnami/bitnami-docker-mongodb#setting-the-root-password-on-first-run).
|
||||
|
||||
The second service thats started is named `myapp` and uses the Bitnami Express development image. The service mounts the current working directory (`~/workdir/myapp`) at the `/app` location in the container and provides all the necessary infrastucture to get you started developing a data-driven Express application.
|
||||
|
||||
Once the Node HTTP server has been started, visit port `3000` of the Docker Machine in your favourite web browser and you'll be greeted by the Express welcome page.
|
||||
|
||||
Lets inspect the contents of the `~/workdir/myapp` directory:
|
||||
|
||||
```bash
|
||||
$ docker-machine ip express-dev
|
||||
```
|
||||
|
||||
Point your web browser to http://{DOCKER_MACHINE_IP}:3000 to access the Express application.
|
||||
|
||||
That’s actually all there is to it. Bitnami has done all the work behind the scenes so that the Docker Compose file “just works” to get you developing your code in a few minutes.
|
||||
|
||||
## Code and Test
|
||||
|
||||
Let's check the contents of the `~/myapp` directory.
|
||||
|
||||
```bash
|
||||
~/myapp # ls
|
||||
~/workdir/myapp # ls
|
||||
app.js config node_modules public views
|
||||
bin docker-compose.yml package.json routes
|
||||
```
|
||||
|
||||
Yay! As you can see, the Express container bootstrapped a new Express application for us in the current working directory and we can now kickstart our application development.
|
||||
You can see that we have a new Express application bootstrapped in the `~/workdir/myapp` directory of the host and is being served by the Node HTTP server running inside the Bitnami Express development container.
|
||||
|
||||
Lets go ahead and modify the look and feel of the app adding the popular [Twitter Bootstrap](http://getbootstrap.com) library.
|
||||
Since the application source resides on the host, you can use your favourite IDE for developing the application. Only the execution of the application occurs inside the isolated container environment.
|
||||
|
||||
1 - Install Bootstrap npm module and restart your application.
|
||||
That’s all there is to it. Without actually installing a single Express component on the host you have a completely isolated and highly reproducible Express development environment which can be shared with the rest of the team to get them started building the next big feature without worrying about the plumbing involved in setting up the development environment. Let Bitnami do that for you.
|
||||
|
||||
In the next sections we take a look at some of the common tasks that are involved during the development of a Express application and how we go about executing those tasks.
|
||||
|
||||
## Executing commands
|
||||
|
||||
You may recall that we've not installed a single Node.js or Express component on the host and that the entire development environment is running inside the `myapp` service container. This means that if we wanted to execute [NPM](https://www.npmjs.com/) or any other Node command, we'd have to execute it inside the container.
|
||||
|
||||
This may sound like a complex task to achieve. But don't worry, Docker Compose makes it very simple to execute tasks inside a service container using the `exec` command. The general form of the command looks something like the following:
|
||||
|
||||
```bash
|
||||
$ docker-compose exec <service> <command>
|
||||
```
|
||||
|
||||
This instructs Docker Compose to execute the command specified by `<command>` inside the service container specified by `<service>`. The return value of the `docker-compose` command will reflect that of the specified command.
|
||||
|
||||
With this information lets load the Node.js REPL in the `myapp` container:
|
||||
|
||||
```bash
|
||||
$ docker-compose exec myapp node
|
||||
```
|
||||
|
||||
To list all the NPM modules currently installed:
|
||||
|
||||
```bash
|
||||
$ docker-compose exec myapp npm ls
|
||||
```
|
||||
|
||||
How about installing the [Bootstramp](https://www.npmjs.com/package/bootstrap) NPM module:
|
||||
|
||||
```bash
|
||||
$ docker-compose exec myapp npm install bootstrap --save
|
||||
$ docker-compose restart
|
||||
```
|
||||
|
||||
2 - Now lets tell Express to serve the Bootstrap CSS files from the `/stylesheets` url.
|
||||
To inspect that the module was installed:
|
||||
|
||||
Using your favorite editor, open `app.js` and add the following line after `var app = express();`
|
||||
|
||||
```js
|
||||
app.use('/stylesheets', express.static(__dirname + '/node_modules/bootstrap/dist/css'));
|
||||
```bash
|
||||
$ docker-compose exec myapp npm ls bootstrap
|
||||
```
|
||||
|
||||
3 - Modify the HTML layout to include the new CSS file
|
||||
You get the idea..
|
||||
|
||||
Edit `views/layout.jade` and add the following line below to the `style.css` import.
|
||||
With the bootstrap NPM module installed, lets modify our Express application and use it to change the look and feel of the UI.
|
||||
|
||||
```HTML
|
||||
Add a static route for serving the Bootstrap CSS by appending the following after the line `app.use(express.static(path.join(__dirname, 'public')));` in `app.js`.
|
||||
|
||||
```javascript
|
||||
app.use('/stylesheets', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/css')));
|
||||
```
|
||||
|
||||
Next in `views/layout.jade`, import the `bootstrap.min.css` style sheet by appending the following at the same indentation level after the line `link(rel='stylesheet', href='/stylesheets/style.css')` and at the same indentation level.
|
||||
|
||||
```jade
|
||||
link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
|
||||
```
|
||||
|
||||
At this point your web app should be using Twitter Bootstrap already, so let's just add some Bootstrap specific HTML markup (jade in this case) to your home page.
|
||||
|
||||
4 - Edit `views/index.jade` and replace it for the following code:
|
||||
Lastly, modify `views/index.jade` of our application to use the Bootstrap classes so that it looks like:
|
||||
|
||||
```jade
|
||||
extends layout
|
||||
@@ -174,35 +152,19 @@ block content
|
||||
.container
|
||||
.jumbotron
|
||||
h1= title
|
||||
p My awesome website using Bootstrap CSS
|
||||
p My awesome #{title} website using Bootstrap CSS
|
||||
```
|
||||
|
||||
And thats it, point your browser to http://{DOCKER_MACHINE_IP}:3000 and you will see that the look and feel has changed.
|
||||
|
||||
From the last couple commands, you must have already figured out that commands can be executed inside the `myapp` service container by prefixing the command with `docker-compose exec myapp`.
|
||||
|
||||
Similarly,
|
||||
|
||||
**To install an npm module in your project**
|
||||
|
||||
```bash
|
||||
$ docker-compose exec myapp npm install [my-module] --save
|
||||
```
|
||||
|
||||
**To see my application logs**
|
||||
|
||||
```bash
|
||||
$ docker-compose logs -f myapp
|
||||
```
|
||||
|
||||
**To restart my application**
|
||||
The Node server should be restarted for the changes to take effect:
|
||||
|
||||
```bash
|
||||
$ docker-compose restart myapp
|
||||
```
|
||||
|
||||
## Connect to a Database
|
||||
Thats it! refresh your browser window and you'll see that the changes have taken effect.
|
||||
|
||||
## Connecting to Database
|
||||
|
||||
Express by default does not require a database connection to work but we provide a running and configured MongoDB service and an example file `config/mongodb.js` with some insights for how to connect to it.
|
||||
|
||||
From this base, you can attach your favorite ODM, i.e [Mongoose](http://mongoosejs.com/) :)
|
||||
You can use [Mongoose](http://mongoosejs.com/) ODM in your application to model your application data.
|
||||
|
||||
Reference in New Issue
Block a user