Utilizing tools from the Registry
As we've already seen it should be possible to containerize almost any project. As we are in between Dev and Ops let's pretend again that some developer teammates of ours did an application with a README that instructs what to install and how to run the application. Now we as the container experts can containerize it in seconds. Open this
https://github.com/docker-hy/material-applications/tree/main/rails-example-project project and read through the README and think about how to transform it into a Dockerfile. Thanks to the README we should be able to decipher what we will need to do even if we have no clue about the language or technology!
We will need to clone the repository, which you may have already done. After the project is done, let's start with a Dockerfile. We know that we need to install ruby and whatever dependencies it had. Let's place the Dockerfile in the project root.
# We need ruby 3.1.0. I found this from Docker Hub FROM ruby:3.1.0 EXPOSE 3000 WORKDIR /usr/src/app
Ok these are the basics, we have FROM a ruby version, EXPOSE 3000 was told at the bottom of the README and WORKDIR /usr/src/app is the convention.
# Install the correct bundler version RUN gem install bundler:2.3.3 # Copy the files required for dependencies to be installed COPY Gemfile* ./ # Install all dependencies RUN bundle install
Here I do a quick trick to separate installing dependencies from the part where we copy the source code in. The COPY will copy both Gemfile and Gemfile.lock to the current directory. This will help us by caching the dependency layers if we ever need to make changes to the source code. The same kind of caching trick works in many other languages or frameworks, such as Node.js.
The next were told to us by the README. We won't need to copy anything from outside of the container to run these.
# Copy all of the source code COPY . . # We pick the production guide mode since we have no intention of developing the software inside the container. # Run database migrations by following instructions from README RUN rails db:migrate RAILS_ENV=production # Precompile assets by following instructions from README RUN rake assets:precompile # And finally the command to run the application CMD ["rails", "s", "-e", "production"]
And finally, we copy the project and follow the instructions in the README.
Ok. Let's see how well monkeying the README worked for us:
docker build . -t rails-project && docker run -p 3000:3000 rails-project. After a while of waiting, the application starts in port 3000 in production mode.
The following three exercises will start a larger project that we will configure in parts 2 and 3. They will require you to use everything you've learned up until now. If you need to modify a Dockerfile in some later exercises, feel free to do it on top of the Dockerfiles you create here.
Go to https://hub.docker.com/ to create an account. You can configure docker hub to build your images for you, but using
push works as well.
Let's publish the youtube-dl image. Log in and navigate to your dashboard and press Create Repository. The namespace can be either your personal account or an organization account. For now, let's stick to personal accounts and write something descriptive such as youtube-dl to repository name. We will need to remember it in part 2.
Set visibility to public.
And the last thing we need is to authenticate our push by logging in:
$ docker login
Next, you will need to rename the image to include your username, and then you can push it:
$ docker tag youtube-dl <username>/<repository> ... $ docker push <username>/<repository> ...