Docker: Define, Build and Run

Dukhyun Ko
Webtips
Published in
7 min readJan 27, 2021

In this blog post, I’m going to share my experience with using docker to create a container for my application.

What is Docker?

Docker is an open platform for developing, shipping, and running applications. It packages up an application or service with all of its dependencies into a standardized unit a.k.a “Image”

Why use Docker?

There are a lot of individual machines and VM running with different OSes and it’s hard to stay up to date with the operating systems as well as their dependencies. This is where Docker shines and solves that problem. Docker streamlines the development lifecycle by providing standardized environments using local containers.

Define, Build and Run

** Personal Thoughts **

This portion of Docker is relatively simple. Their documentation on installation is really straight forward. I recommend reading the user manual and the troubleshoot. Gave me some good tips on how to take advantage of cool features that Docker has.

Start by Installing Docker. (link)
Installation includes Docker Engine, Docker CLI client, Docker Compose, Notary, Kubernetes, and Credential Helper.

Follow the steps that are provided in the link above and look out for the Docker menu that will appear in the status bar.

I used an existing Ruby on Rails backend that I built out already to create the container.

Here’s Docker’s guide on how to quickly compose your application into the container. (link)

1. Define Project

Image is a read-only template with instructions for creating a Docker container. To build an image, you make a Dockerfile with syntax that shows the steps needed to create the image.

Container is a runnable instance of an image. You can connect a container to one more network, attach storage to it. Container is what we will be monitoring using Datadog.

Create files needed to build the app.

Dockerfile: Define dependencies. For best practices refer here => (link).

Gemfile: Used to load rails. Before building out the image, comment out the unnecessary gems in this file.

Gemfile.lock: Empty at this point, but will be overwritten by rails new.

entrypoint.sh: entry point script that fixes Rails-specific issue that prevents the server from restarting.

2. Build the Project

** Personal Thoughts **

I had to do a lot of troubleshooting to get through this point and I’ll have another section of all the errors that I faced. One thing I want to recommend is carefully reading the documentation. Also know that the versions of the languages and frameworks you have may differ from the documentation. Make sure to double check your versions when you are creating the project with the image that you defined.

Generate Rails skeleton app.

If you defined the image in the Dockerfile and other dependencies are listed you can run

  • If you are creating a new project run this script in the terminal.
    If you already have a skeleton from an existing project, you can skip this.
docker-compose run --no-deps web rails new . --force --database=postgresql

Build the image by running this snippet in the terminal.

docker-compose build

Connect the database.

When Rails expects its database to be running local on default. So, we need to point to the database container. This snippet should be in ‘ config/database.yml ’

default: &defaultadapter: postgresqlencoding: unicodehost: dbusername: #your usernamepassword: #your passwordpool: 5development:<<: *defaultdatabase: myapp_developmenttest:<<: *defaultdatabase: myapp_test

Once the database is pointing to the correct location, create it with this script.

docker-compose run web rake db:create

3. Run the Project

When you are done setting up the database, it’s time to start up the container and check if the container is running the Docker.

docker-compose up

If you were able to successfully start the server using Docker, you will be able to see your app “RUNNING” in the container tab in Docker.

This is a very useful part of using Docker. Being able to interact with the server at the click of the button. Also, the CLI button was really useful. I didn’t have to make sure I was in the correct location to do some manual testing/creating in my application.

This log appears when you click one of your containers. Really useful to search for any specific errors or activities you are looking for.

4. Rebuilding the Project

When you make some changes to the Gemfile or the Compose file to make changes to the application, you need to “rebuild” so that the dependencies in the Docker match what you want to use in the application.

docker-compose up --build# does not do a full rebuilddocker-compose run web bundle installdocker-compose up --build# goes through a full rebuild and sync changes in Gemfile.lock

Possible Errors

** Personal Thoughts **

This is the fun part where you spend extra time trying to figure out what went wrong during the process. Here are some of the problems that I ran into and hopefully it can help. I recommend reading “Overview of Compose” to understand the whole process and Google the error lines that show up in the terminal that is more specific to your machine.

1. Ruby version

Your Ruby version is 3.0.0, but your Gemfile specified 2.6.1

This error can be common. You may have never updated the Ruby on your computer to 3.0.0, but it is possible that Docker may tell you that the version is 3.0.0. Check your ruby by running ruby -v and it will show what version your Ruby is. The problem may not be your machine, but actually the ruby that was installed into the container because when you initialize this container in the Dockerfile without specifying the version, it will download the latest version.

# FROM ruby# This should beFROM ruby:2.6.1 #The version that you have specified in your Gemfile.

When building the container, check the ruby version by using command “ ruby -v ” in the app folder that will be dockerized and match it on Dockerfile.

2. Bundler version

You must use Bundler 2 or greater with this lockfile.

ERROR: Service ‘web’ failed to build : The command ‘/bin/sh -c bundle install’ returned a non-zero code: 20

You may encounter this error if you did not clean out your Gemfile.lock before building. It is possible that it is causing some dependency issues in the container. Gemfile.lock should be empty when you build so that it will setup the appropriate version of bundler necessary for your project (link)

3. Unavailable Port

ERROR: for web cannot start service web: driver failed programming external connectivity on endpoint practice_web_1 (74f42cfcd5c8ef61c74a49fad9faa9a964eddfd24d1328ef45b361d25365b9a): Bind for 0.0.0.0:3001 failed: port already allocated

ERROR: Encountered errors while bringing up the project.

If the port is being used by another project, change the port in the docker-compose.yml or kill the app running on the server.

4. Missing File

web_1|/usr/local/bundle/gems/webpacker-4.3.0/lib/webpacker/configuration.rb95:in ‘rescue in load’: Webpacker configuration file not found /myapp/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen — /myapp/config/webpacker.yml(RuntimeError)

This command “run rails webpacker:install” within Docker may not resolve the issue. The issue actually was caused because my Gemfile had “ gem ‘webpacker’, ‘~> 4,2’ ”. This issue can be easily fixed by commenting out this line of code in Gemfile if you are not planning to use webpacker then rebuild the container.

If you are planning to use webpacker, then you must download node js along with yarn and this can be done in the Dockerfile.

RUN curl https://deb.nodesource.com/setup_12.x | bashRUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.listRUN apt-get update && apt-get install -y nodejs yarn postgresql-client

5. Location of the database

Connect the database. When Rails expects its database to be running local on default. So, we need to point to the database container. (scroll up to “connect database”)

Feel free to contact me with any questions!

--

--

Dukhyun Ko
Webtips
Writer for

Software Engineer 👨🏻‍💻 | Personal Trainer 🏋🏻‍♂️ | Commissioner 🏀