How to create a simple docker image with AWS CLI and Serverless installed
Tags: docker, awscli, aws, howto
Published: 2022-02-11 17:17:24

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


![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgr594x5s6xen42trdgq.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcfutfxh8glplx7qmzh4.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/misqium9p2mhh1a6e34d.png)

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

![Install docker on your machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jeof1ee2x6u6qhg0uxrw.png)

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

![Login using docker hub account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mxjl06hllwdt11on75pa.png)

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

```javascript
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
```

>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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8w2b1w3pcywswffhiqo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vos9thuwy7ejxus7oy5.png)

Check your docker hub account and your image should be there

![DONE](https://media.giphy.com/media/eKfovWZOlOpbTDEX9F/giphy.gif)