Pushing image from DockerHub to EC2 through EC2 Connect

0

Enter image description here

Enter image description here Enter image description here

Enter image description here

Enter image description here

Enter image description here

Not sure if I am moving in the right direction and now need to provide some commands on EC2 Connect in order to push the image from DockerHub to EC2 instance. If indeed so, could please anyone guide with the commands for the purpose.

profile picture
asked 23 days ago103 views
3 Answers
3
Accepted Answer

Hi Rajeev Bagra

1.Docker Installation: Update the package index:

  ```
  sudo apt update

 ```

Install prerequisites:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add the Docker GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the Docker repository:

 sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update the package index again:

sudo apt update

Install Docker:

 sudo apt install docker-ce
``
Verify that Docker is installed:

sudo docker --version

2.Pushing Images to Docker Registry:

Log in to your Docker registry:

 ```
 docker login <registry-url>
 ```
Tag your Docker image with the registry URL:

 ```
 docker tag <image-name>:<tag> <registry-url>/<image-name>:<tag>
 ```
Push the tagged image to the Docker registry:

docker push <registry-url>/<image-name>:<tag>
answered 23 days ago
profile picture
EXPERT
reviewed 21 days ago
  • Thanks! It will help now to know how to set the IP address for the web application image pulled through Docker.

    ubuntu@ip-172-31-12-29:~$ docker images
    REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
    digitalsplendid/my-flask-app   latest    da18e5aba86b   2 days ago      181MB
    hello-world                    latest    d2c94e258dcb   12 months ago   13.3kB
    ubuntu@ip-172-31-12-29:~$ 
    ubuntu@ip-172-31-12-29:~$ docker run digitalsplendid/my-flask-app:latest
    * Serving Flask app 'app'
     * Debug mode: on
    INFO: WARNING: This is a development server. Do not use it in a production 
    deployment. Use a production WSGI server instead.
     * Running on http://127.0.0.1:5000
    

    However when pasting http://127.0.0.1:5000 on the browser:

    This site can’t be reached So how to set or make this image live?

  • Hi Rajeev Bagra

    1.Specify Port Binding: When running the Docker container, use the -p flag to specify port binding. For example, if your Flask app is running on port 5000 inside the container and you want to access it on port 5000 of the host machine, you would run: Command: docker run -p 5000:5000 digitalsplendid/my-flask-app:latest 2.Use 0.0.0.0 as Host Address: Ensure that your Flask app is configured to listen on 0.0.0.0 instead of 127.0.0.1 when running inside the Docker container. This allows the app to accept connections from any IP address, not just localhost. Modify your Flask app to run with:

      if _ _name__ == '__main__':
            app.run(host='0.0.0.0')
    

    After making these changes, your Flask app should be accessible at http://<host_machine_ip>:5000. Replace <host_machine_ip> with the IP address of your host machine. You can find the IP address using commands like ifconfig (on Linux)

  • Currently, app.py file has this: if name == "main": app.run(debug=True)

    So need to modify: if _ name_ == 'main': app.run(host='0.0.0.0')

    The above needs to be changed within app.py file itself. So this means going to the app.py file on VS Code, adding the above code, and then pushing the image to DockerHub. And then DockerHub to EC2 instance. Is there any other way?

    Thanks in advance!

  • Started with a new instance after adding to the app.py file: if _ name_ == 'main': app.run(host='0.0.0.0'). The issue yet not resolved. https://www.canva.com/design/DAGEiUuJrio/aAFWZXEEMz6atIMVleKYag/edit?utm_content=DAGEiUuJrio&utm_campaign=designshare&utm_medium=link2&utm_source=sharebutton

3

hello, i am sharing some commands and steps to Push image from Docker Hub to EC2. first connect to your ec2- server then run following commands

$ sudo yum install docker
$ sudo yum install docker
$ sudo service docker start
$ sudo docker login # Enter your DockerHub username and password 
$ sudo docker pull digitalsplendid/my-flask-app
$ docker tag digitalsplendid/my-flask-app <your_ec2_instance_public_dns>/my-flask-app
 #this command i used to tag that image it looks already taged to your Docker Hub so you can skip it now.
$ docker push <your_ec2_instance_public_dns>/my-flask-app #this command to push it to your Docker Hub 

these are the commands you can use to resolve this issue and this commands for AWS Linux server if you're using ubuntu try to use apt instead of yum you can find that commands online. i hope this will be helpful to you.

profile picture
answered 23 days ago
profile pictureAWS
EXPERT
reviewed 23 days ago
1

SSH into your EC2 instance using EC2 Connect: EC2 Connect allows you to securely connect to your EC2 instances using SSH without needing to manage SSH keys directly. You'll use EC2 Connect to access your EC2 instance.

Install Docker on the EC2 instance (if not already installed): If Docker is not already installed on your EC2 instance, you need to install it. You can use package managers like yum (for Amazon Linux) or apt-get (for Ubuntu) to install Docker.

Pull the Docker image from DockerHub: Once you're logged into your EC2 instance, use the docker pull command to pull the Docker image from DockerHub. For example:

``docker pull <your-image-name> `

Run the Docker container: After pulling the Docker image, you can run the container using the docker run command. Make sure to specify any necessary options, such as port mappings or environment variables. For example:


docker run -d -p 80:8080 <your-image-name>

Verify that the container is running: You can use the docker ps command to verify that the container is running successfully:


docker ps

Now, let's go through these steps using EC2 Connect commands:

Install Docker on the EC2 instance (if not already installed):You can install Docker on your EC2 instance using package managers like yum or apt-get, depending on the Linux distribution. For example, on Amazon Linux:


sudo yum install docker -y

docker pull <your-image-name>

docker run -d -p 80:8080 <your-image-name>

docker ps

answered 23 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions