GithubHelp home page GithubHelp logo

simple-docker-aws-guide's Introduction

Deploying a Docker Container to AWS

Creating your Docker Image

Create a Docker Image using a Dockerfile. After writing up your Dockerfile, build your Docker Image by running this in the terminal. Think of a good Docker Image Name that you can use for the following shell commands.

cd <PATH_TO_DOCKERFILE>
docker built -t <DOCKER_IMAGE_NAME> .

# example
cd ~/code/docker-folder
docker build -t simple-docker-image

Verifying your Docker Image

Check Docker Built Correctly

You can verify if your Docker Image was built correctly if you run this following command.

docker images --filter reference=<DOCKER_IMAGE_NAME>

# example
docker images --filter reference=simple-docker-image

If successful, you should see your docker image listed.

Check Docker Image actually works

Run your docker image locally to make sure it works, like so:

docker run -it -p <HOST_PORT>:<DOCKER_PORT> <DOCKER_IMAGE_NAME>

# example
docker run it -p 80:80 simple-docker-image

In my example, I open up localhost:80 and I see my container is running. Once verified you can stop the docker container like so:

docker container stop <DOCKER_IMAGE_NAME>

# example
docker container stop simple-docker-image

Pushing Docker Image to AWS

Make sure your have your AWS CLI installed and configured with your access key(s).

Create Repository to Hold Your Container

You have to create a repository on AWS that will hold your Docker Image. You need to come up with a name for your repository and a region where you want the repository to reside. You can do this like so:

aws ecr create-repository --repository-name <YOUR_REPOSITORY_NAME> --region <AWS_REGION>

# example
aws ecr create-repository --repositry-name simple-docker-repo --region ap-northeast-2

You should see an output that looks like this:

{
    "repository": {
        "registryId": "<AWS_ACCOUNT_ID>",
        "repositoryName": "<YOUR_REPOSITORY_NAME",
        "repositoryArn": "arn:aws:ecr:<AWS_REGION>:<AWS_ACCOUNT_ID>:repository/<YOUR_REPOSITORY_NAME>",
        "createdAt": 1505337806.0,
        "repositoryUri": "<AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/<YOUR_REPOSITORY_NAME>"
    }
}

Keep track of the value for repository.repositoryUri from the above JSON output. You will need it for the next command.

Tag and Push your Docker Image

The next command will let Docker know where to push your Docker Image.

docker tag <DOCKER_IMAGE_NAME> <repository.repositoryUri>

Authenticate and Login with AWS

You will need the registry URI of where your ECS repositories reside on AWS. To get this uri you just need to splice off the last "/" and everything following it from <repository.repositoryUri>.

aws ecr get-login-password | docker login --username AWS --password-stdin <repository.repositoryUri>

# example
aws ecr get-login-password | docker login --username AWS --password-stdin 423432424.dkr.ecr.ap-northeast-2.amazonaws.com

Finally

Push to AWS using this command and your <repository.repositoryUri>:

docker push <repository.repositoryUri>

Deploying your Docker Image

Create a Cluster

In AWS, containers live in clusters when they are deployed. So we must first create a cluster. Think of a name for your cluster. You can do this in the command line like so:

aws ecs create-cluster --cluster-name <NAME_OF_YOUR_CLUSTER>

# sample
aws ecs create-cluster --cluster-name simple-docker-cluster

Create a task

After you create a cluster, AWS requires containers to be run in Tasks. Tasks can hold up to 10 containers. However for a task to be created, it needs a Task Definition which is a JSON file that holds metadata about your task and the containers within that task.

simple-docker-aws-guide's People

Watchers

Daniel Lee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.