How to use Docker for local web app development

In this article you will learn how to use Docker for local application development

We are going to see how we can use Docker to run our application in Docker container but still able to code from local editor

Create Rails app

 $ rails new next_big_app

Design a Dockerfile

We need to pull Ruby image as we are developing a Rails app

FROM ruby:2.6.5

Update the Docker container with latest packages info

RUN apt-get update && apt-get install apt-transport-https

Install Node.js

# Install Node.js
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get update && apt-get install -y nodejs
# Install yarn
ADD https://dl.yarnpkg.com/debian/pubkey.gpg /tmp/yarn-pubkey.gpg
RUN apt-key add /tmp/yarn-pubkey.gpg && rm /tmp/yarn-pubkey.gpg
RUN echo 'deb https://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y --no-install-recommends yarn

Set workdir

WORKDIR /next_big_app

Copy gemfiles from local app to container workdir
Install bundler in container

COPY Gemfile /next_big_app
COPY Gemfile.lock /next_big_app
COPY package.json /next_big_app
COPY yarn.lock /next_big_app
RUN gem install bundler --conservative

Copy all sources of current directory to work directory

COPY . /next_big_app

Run bundle install

RUN bundle install --verbose --jobs 20 --retry 5 && \
    yarn install --check-files

Expose app PORT and set entry command to run the app

EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

Our Dockerfile would look like this

FROM ruby:2.6.5

RUN apt-get update && apt-get install apt-transport-https

# Install Node.js
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get update && apt-get install -y nodejs

# Install yarn
ADD https://dl.yarnpkg.com/debian/pubkey.gpg /tmp/yarn-pubkey.gpg
RUN apt-key add /tmp/yarn-pubkey.gpg && rm /tmp/yarn-pubkey.gpg
RUN echo 'deb https://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y --no-install-recommends yarn


WORKDIR /next_big_app

COPY Gemfile /next_big_app
COPY Gemfile.lock /next_big_app
COPY package.json /next_big_app
COPY yarn.lock /next_big_app
RUN gem install bundler --conservative

COPY . /next_big_app

RUN bundle install --verbose --jobs 20 --retry 5 && \
    yarn install --check-files


EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

Now we have created a Dockerfile. Let's create a docker-compose file to specify our app service.

Create Docker compose file


We will use docker-compose to start our app. docker-compose also makes it easy to set additional configuration when we run the app.

As you see, we say

version: '2'

Then we specify services

services:
  web:

In the web part we put our options for docker-compose

Set path to build Dockerfile from. We set it as . as our Dockerfile resides in current directory

  web:
    build: .

Then we specify our image

    image: anil/next_big_app

We need to set our local source directory as shared volume to our workdir

    volumes:
      - .:/next_big_app

Finally we map the local port with our container port

    ports:
      - "3000:3000"

This is how our docker-compose.yml would look finally

version: '2'
services:
  web:
    build: .
    image: anil/next_big_app
    volumes:
      - .:/next_big_app
    ports:
      - "3000:3000"

Now we need to start our application

Run docker-compose rm to remove any stopped service containers.

$ docker-compose rm
No stopped containers

Now run docker-compose up


$ docker-compose up
Creating next_big_app_web_1 ... done
Attaching to next_big_app_web_1
web_1  | => Booting Puma
web_1  | => Rails 6.0.2.2 application starting in development
web_1  | => Run `rails server --help` for more startup options

You should be able to use your favorite editor to change source code and run the app inside the container now.

Anil Wadghule

Anil Wadghule