deploy docker container on AWS EC2 instance withou

2019-07-07 19:48发布

问题:

I'm a beginner with microservices and have spent hours on the most tiny painful things of AWS today, would appreciate any expert advice as I suspect the next step is very small but could take me hours to work it out otherwise.

So I deployed a nano instance then ssh into it. Had to actually redo it to fix the security group but anyway it worked eventually. Used scp to put my docker image up there per the instructions here, in summary docker save to make a .tar out of the image locally and docker load to put it into the system remotely after waiting 15 minutes for scp to upload. Then typed docker run at the command prompt.

Had resorted to these (linux) terminal measures as over the last 3 days had twice tried and failed to do it from the AWS console, as in it uploaded but wouldn't run.

Now it runs fantastically when I type docker run my_image and I can see it in there with both the commands docker images and docker ps -a !

But the command prompt on my AWS instance is busy while it runs.. if I close the terminal window it will surely die. Now that I know it works there, how can I 'deploy' it, ie let it run and continue to run for a month or until further notice? I think it might need some kind of json file called 'task definition' but don't really know at all what to do next. Can this task definition and all remaining tasks be done from within a terminal logged into the instance?

回答1:

I see a few things wrong:

  • You should use a Docker registry service instead of SCPing an image. On AWS there is EC2 Container Registry or you can also use Docker Hub as well. This will make it much easier to get your images onto your instances.

  • I'm not sure why you weren't able to start your container using the console. I assume you are using AWS ECS? You might try the troubleshooting guide to help you figure out why your task wasn't running.

  • It sounds like you are starting a docker container in the foreground (attached to your shell) rather than in the background. Try adding the -d flag to your docker run command to run the container in the background so you can close your SSH session. Note that if the application process inside your container crashes the container will still stop. This is one reason for using an orchestrator such as AWS ECS to define a service that will attempt to always run a certain number of your tasks. ECS also helps with getting the docker container onto the instance and starting it for you in the background automatically.