1. Feel the convenience of Docker
Project source https://github.com/MoonShining/dockernize-grape-helloworld
Clone down after run, CD to the project directory, run
docker build -t api-sample .
docker run -p 8080:8080 api-sample:latest
Copy the code
You can go to localhost:8080 and see what it looks like.
2. Specific steps
- Write an HTTP service that outputs Hello World in your preferred language
- Tell Docker how you need to build the service (Dockerfile) because there are not many Ruby programmers, I won’t go into Ruby code, just a little bit.
Run after the Clone project
Bundle install rackup -o 0.0.0.0 -p 8080Copy the code
This allows you to run the Ruby version of Hello World directly locally.
3. Dockerfile
The FROM ruby: 2.3.1 LABEL maintainer"[email protected]"
WORKDIR /app
ADD . /app
RUN bundle install
EXPOSE 8080
CMD ["rackup"."-o"."0.0.0.0"."-p"."8080"]
Copy the code
This configuration file does several things
- You must specify a Base Image, which we use ruby:2.3.1
- Maintainer information for this image
- Set a working directory in the Docker image as the running directory for the following RUN and CMD commands
- Map the current directory to /app in the container
- Install dependencies for Ruby projects
- Expose port 8080 of the container out
- Run the rackup command in the container to start the service
So, no matter what our underlying system is, as long as Docker is installed, it can be very convenient to run the program, do not care about various dependencies
reference
- ThoughtWorks’ Microservices Architecture and Practice ‘is a very general book written by someone at ThoughtWorks and not recommended reading…
- Docker References (Dockerfile of instruction meaning) https://docs.docker.com/engine/reference/builder