Build and Deploy Docker image to Amazon EC2

Docker installation on Windows machines isn't hard. Make sure your CPU supports virtualization technology and virtualization support is enabled in BIOS. Then go Download ToolBox and simply install docker.

Some special commands you should use and know:

Build Dockerfile:

docker build -t user/repo:tag . Dot in the end is the relative path for your Dockerfile.

Show docker images:

docker image

If you want to delete image from VM, your should stop and delete all the containers with this image. So:

Delete container:

docker rm "container id"

Delete image:

docker rmi "image id"

Login to DockerHub

It's is very convenient to have already built and ready-to-use image in docker hub.

In Windows there some troubles for the first time login to Docker Hub. You should register and type in console:

docker login --username=username --password=pass [email protected] https://index.docker.io/v1/

Then open C:\Users\Username\.docker\config.json and change mail\hash for both the same i.e.

{
    "auths": {
        "https://index.docker.io/v1/": {
            "auth": "c2thenppOk5vb0IyMjAyAAA=",
            "email": "[email protected]"
        },
        "https://registry-win-tp3.docker.io/v1/": {
            "auth": "c2thenppOk5vb0IyMjAyAAA=",
            "email": "[email protected]"
        }
    }
}

When you built your image, let's save it to hub for future easy use.

Commit changes to Docker image:

docker commit #containerid user/repo:tag

Push image to Docker Hub

docker push user/repo:tag

EC2 quick setup and docker image use routine

Connecting to Your Linux Instance from Windows Using PuTTY.

In the Host Name box, enter user_name@public_dns_name. Be sure to specify the appropriate user name for your AMI. 
For example:

1. For an Amazon Linux AMI, the user name is ec2-user
2. Under Connection type, select SSH.
3. Ensure that Port is 22.
4. In the Category pane, expand Connection, expand SSH, and then select Auth. Select the .ppk file that you generated for your key pair

In this guide i use Linux Amazon Instance (p.1)

[ec2-user ~]$ sudo yum update -y

[ec2-user ~]$ sudo yum install -y docker

[ec2-user ~]$ sudo service docker start

[ec2-user ~]$ sudo usermod -a -G docker ec2-user - to use docker command without sudo

Log out and log back in again to pick up the new docker group permissions.

Let's install git. I think it's the easiest way to get your project files to Amazon instance, so:

[ec2-user ~]$ sudo yum install -y git

Should I explain?:

git clone https://[email protected]

Now you can login and get your docker image:

docker login docker pull user/repo:tag

If you want to dynamically mount volume from host to docker container use -v key, -i for interactive console + this helps to save container UP (instead after finishing command container goes off) -t for choosing image by name\tag, i.e.:

docker run -v /home/ec2-user/host/directory:/container/directory -i -t user/repo:tag

Connect to running Docker container

If you start a long-term operation and log-out from console, docker container will continue his work. You can connect to running container anytime:

docker attach 'container id'

If you want to get some data back to your local machine, first you should get it from docker container to host:

Show all containers, running and stopped:

docker ps -a get id there

Copy data from Docker container:

docker cp <containerId>:/file/path/within/container /host/path/to/target

And for Amazon EC2 it should be: /home/ec2-user/path/to/target

Now you can get data from EC2 host to local machine with tour favourite ftp client or putty =)

results matching ""

    No results matching ""