Skip to content

Instantly share code, notes, and snippets.

@andrealmar
Last active June 25, 2020 22:53
Show Gist options
  • Save andrealmar/52c876585e0d74f659464cc5f67035b3 to your computer and use it in GitHub Desktop.
Save andrealmar/52c876585e0d74f659464cc5f67035b3 to your computer and use it in GitHub Desktop.

Dockerfile NGINX

# Start from the latest golang base image
FROM nginx:1.19-alpine

# Expose port 8080 to the outside world
EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Build your NGINX image:

docker build -t simple-nginx .

Run your NGINX image:

docker run --rm -it -d -p 8080:80 simple-nginx


go build

./pokemon-api

OUTPUT: Welcome to POKEMON API from 192.168.0.15

Test the application:

curl http://localhost:8080

OUTPUT: Welcome to POKEMON API from 192.168.0.15

Test the other endpoints of our POKEMON API:

curl http://localhost:8080/pikachu

curl http://localhost:8080/bulbasaur

curl http://localhost:8080/squirtle

curl http://localhost:8080/charmander


Dockerfile

# Start from the latest golang base image
FROM golang:alpine

# Add Maintainer Info
LABEL maintainer="Andre Almar <andre@y7mail.com>"

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app
RUN go build -o pokemon-api .

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./pokemon-api"]

Build the image:

docker build -t pokemon-api-v1 .

Run the Docker Image:

docker run -d -p 8080:8080 pokemon-api-v1

See the container running:

docker container ls

Interact with the app inside the running container:

curl http://localhost:8080

OUTPUT: Welcome to POKEMON API from 172.17.0.2

Test the other endpoints of our POKEMON API running now INSIDE the container:

curl http://localhost:8080/pikachu

curl http://localhost:8080/bulbasaur

curl http://localhost:8080/squirtle

curl http://localhost:8080/charmander


MULTISTAGE BUILDING

Dockerfile Multistage

# build stage
FROM golang:alpine AS build-env
RUN apk --no-cache add build-base git mercurial gcc
ADD . /src
RUN cd /src && go build -ldflags '-w -s' -a -o pokemon-api

# final stage
FROM alpine
WORKDIR /app
COPY --from=build-env /src/pokemon-api /app/
EXPOSE 8080
ENTRYPOINT ./pokemon-api

Build the optimized Docker image:

docker build -t pokemon-api-v2 -f Dockerfile.multistage .

Check if the image is created:

docker images

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
pokemon-api-v2        latest              4ba2c8b851fe        7 seconds ago       14MB

Comparing the 2 images:

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
pokemon-api-v2        latest              4ba2c8b851fe        7 seconds ago       11MB
pokemon-api-v1        latest              8be37dd67655        10 minutes ago      385MB

Now interact with the Optimized Docker image:

curl http://localhost:8080

OUTPUT: Welcome to POKEMON API from 172.17.0.2

Test the other endpoints of our POKEMON API running now INSIDE the OPTIMIZED container:

curl http://localhost:8080/pikachu

curl http://localhost:8080/bulbasaur

curl http://localhost:8080/squirtle

curl http://localhost:8080/charmander


** PUSH IMAGE TO REGISTRY **

First we have to tag the image:

docker tag 16a72b5aa0d9 andrealmar/pokemon-api:latest

Let's push our image to the Registry:

docker push andrealmar/pokemon-api

If you see this error:

Error response from daemon: pull access denied for andrealmar, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

Don't forget to log in:

docker login --username=andrealmar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment