This guide shows you how to connect an AWS Cloud9 SSH development environment to a running Docker container inside of an Amazon Linux instance in Amazon EC2 or any other Linux based server. This enables you to use the AWS Cloud9 IDE to work with code and files inside of a Docker container and to run commands on that container.
This guide from AWS is available here
-
Check if Docker is installed on the instance. To do this, run the
docker
command on the instance with the--version
option.`docker --version`
If Docker is installed, the Docker version and build number are displayed. In this case, skip ahead to step 5 later in this procedure.
-
Install Docker. To do this, run the
yum
orapt
command with theinstall
action, specifying thedocker
ordocker.io
package to install.For CentOS, RedHat or Amazon Linux:
`sudo yum install -y docker`
For Ubuntu or Debian :
`sudo apt install -y docker.io`
-
Confirm that Docker is installed. To do this, run the
docker --version
command again. The Docker version and build number are displayed. -
Run Docker. To do this, run the
service
command with thedocker
service and thestart
action.`sudo service docker start`
In this step, you use a Dockerfile to build a Docker image onto the instance.
-
On the instance, create a file that contains the AWS Cloud9 SSH public key for the Docker container to use. To do this, in the same directory as the
Dockerfile
file, create a file namedauthorized_keys
, for example, by running thetouch
command.`sudo touch authorized_keys`
-
Add the AWS Cloud9 SSH public key to the
authorized_keys
file. To get the AWS Cloud9 SSH public key, do the following:a. Open the AWS Cloud9 console at https://console.aws.amazon.com/cloud9/.
b. In the AWS navigation bar, in the AWS Region selector, choose the AWS Region where you'll want to create the AWS Cloud9 development environment later in this topic.
c. If a welcome page is displayed, for New AWS Cloud9 environment, choose Create environment. Otherwise, choose Create environment.
d. On the Name environment page, for Name, type a name for the environment. (The name doesn't matter here. You'll choose a different name later.)
e. Choose Next step.
f. For Environment type, choose Connect and run in remote server (SSH).
g. Expand View public SSH key.
h. Choose Copy key to clipboard. (This is between View public SSH key and Advanced settings.)
i. Choose Cancel.
j. Paste the contents of the clipboard into the
authorized_keys
file, and then save the file. For example, you can use thevi
utility, as described earlier in this step. -
Build the image by running the
docker
command with thebuild
action, adding the tagcloud9-image:latest
to the image and specifying the path to theDockerfile
file to use.`sudo docker build -t cloud9-image:latest`
In this step, you run a Docker container on the instance. This container is based on the image you built in the previous step.
-
To run the Docker container, run the
docker
command on the instance with therun
action and the following options.`sudo docker run -d -it --expose 9090 -p 0.0.0.0:9090:22 -v {path to your workspace}:/home/ubuntu/workspace --name cloud9 cloud9-image:latest`
-
Log in to the running container. To do this, run the
docker
command with theexec
action and the following options.`sudo docker exec -it cloud9 bash`
-
Make a note of the path to the directory on the running container that contains the Node.js binary, as you'll need it for Step 3: Create the Environment. If you're not sure what this path is, run the following command on the running container to get it.
`which node`
In this step, you use AWS Cloud9 to create an AWS Cloud9 SSH development environment and connect it to the running Docker container. After AWS Cloud9 creates the environment, it displays the AWS Cloud9 IDE so that you can start working with the files and code in the container.
-
Sign in to the AWS Cloud9 console as follows:
-
If you're the only individual using your AWS account or you are an IAM user in a single AWS account, go to https://console.aws.amazon.com/cloud9/.
-
If your organization uses AWS Single Sign-On (SSO), see your AWS account administrator for sign-in instructions.
-
If you're using an AWS Educate Starter Account, see Step 2: Use an AWS Educate Starter Account to sign in to the AWS Cloud9 console in Individual Student Signup.
-
If you're a student in a classroom, see your instructor for sign-in instructions.
-
-
In the AWS navigation bar, in the AWS Region selector, choose the AWS Region where you want to create the SSH environment.
-
If a welcome page is displayed, for New AWS Cloud9 environment, choose Create environment. Otherwise, choose Create environment.
-
On the Name environment page, for Name, type a name for the environment.
-
To add a description to the environment, type it in Description.
-
Choose Next step.
-
For Environment type:, choose Connect and run in remote server (SSH).
-
For User, type
ubuntu
. -
For Host, type the public IP address of the Amazon EC2 instance, which you noted earlier.
-
For Port, type
9090
. -
Expand Advanced settings.
-
For Environment path, type the path to the directory on the running container that you want AWS Cloud9 to start from after it logs in.
-
For Node.js binary path, type the path to the directory on the running container that contains the Node.js binary, which you noted earlier.
-
Choose Next step.
-
Choose Create environment.
-
When the AWS Cloud9 Installer dialog box appears, choose Next.
-
In the list of components to be installed, clear the c9.ide.lambda.docker check box, and then choose Next. This is because AWS Cloud9 cannot run Docker inside of Docker.
-
When the AWS Cloud9 Installer dialog box displays Installation Completed, choose Next, and then choose Finish. The AWS Cloud9 IDE appears for the running container, and you can start working with the container's files and code.