DEV Community

Cover image for How to create a simple docker image with AWS CLI and Serverless installed
metacollective
metacollective

Posted on

How to create a simple docker image with AWS CLI and Serverless installed

A step by step guide on how to create a simple docker image and host it on the docker hub

Docker

A simple and easy step-by-step guide on how to create your own docker image and then host it on the docker hub. In this tutorial, we will create an image with the following software installed

  • nodejs (v14)
  • git
  • bash
  • curl
  • openssh
  • py3-pip
  • wget
  • aws cli
  • serverless

Step 1: Create an account on docker hub website by going here - https://hub.docker.com/signup

Sign up for docker hub account

Step 2: Create a new repository on your docker hub account by going here https://hub.docker.com/repository/create?namespace=[you account name].

You can mark it private or public as per your requirement. If private then only those who you want can access it, with the public, it's for everyone.

docker repo

Step 3: Install docker for desktop. You can download installables from - https://www.docker.com/products/docker-desktop

Install docker on your machine

Step 4: Login into your desktop docker using your docker hub account

Login using docker hub account

Step 5: Open your editor (e.g: visual studio code) and add a new file called Dockerfile. Add the following lines into your file

FROM node:14-alpine

# Install packages
RUN apk update && apk add --update --no-cache \
    git \
    bash \
    curl \
    openssh \
    python3 \
    py3-pip \
    py-cryptography \
    wget \
    curl

RUN apk --no-cache add --virtual builds-deps build-base python3

# Update NPM
RUN npm config set unsafe-perm true
RUN npm update -g

# Install AWSCLI
RUN pip install --upgrade pip && \
    pip install --upgrade awscli

# Install Serverless Framework
RUN npm install -g serverless
Enter fullscreen mode Exit fullscreen mode

FROM node:14-alpine: This image is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.

Install various packages you want to install in this image
like this -

RUN apk update && apk add — update — no-cache \
git \
bash \
curl \
openssh \
python3 \
py3-pip \
py-cryptography \
wget \
curl

Update NPM

RUN npm config set unsafe-perm true
RUN npm update -g

Install AWS CLI

RUN pip install — upgrade pip && \
pip install — upgrade awscli

Install Serverless framework globally. You must install the Serverless framework globally for it to work. If you install it via a project then it will not work and any of your serverless commands will fail in this image’s container

RUN npm install -g serverless

Now you are all set to push this image up to the docker hub.
Go to your terminal and run this command to build an image from this Dockerfile -

docker build -t [replace this with your username]/[replace this with the name of your repository you created in step2]

build image

Once it's built, you can find your image on your local machine by running this command -

docker images

Now, simply push your image to the docker hub

docker push [replace with your account]/[replace with your image name]

Docker push

Check your docker hub account and your image should be there

DONE

Discussion (0)