AWS User data script Docker-Run not working - amazon-web-services

I have installed docker and httpd on an instance and when I run
sudo docker run -d -p 8600:8080 pengbai/docker-supermario
in SSH, The command works but when I Restart the Instance with this user data(docker and httpd already installed)
#!/bin/bash
sudo yum update -y
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl enable docker
sudo systemctl start docker
sudo docker run -d -p 8600:8080 pengbai/docker-supermario
It does not work
I have tried it with sudo, without sudo and even added a sleep command between some of the commands but still couldn't get it to work.

Related

docker not installed through yum in user data

this is how my user data looks like, but docker is not installed, when connecting to my ec2 machine:
sudo yum -y install docker
sudo service docker start
sudo docker pull nginx
sudo docker run -d -p 80:80 nginx
what can I do?
When using user-data script you can debug what is happening by ssh connecting to the instance and check the output in cloud-init-output.log.
sudo cat /var/log/cloud-init-output.log
When doing this you'll find an strange error containing:
Jan 29 11:58:25 cloud-init[2970]: __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'sudo yum -y install dock...'
Which means that the default interpreter seems to be python and it's neccesary to start the user-data with #!/bin/bash. (See this other StackOverflow answer)
When changing the user-data to:
#!/bin/bash
sudo yum -y install docker
sudo service docker start
sudo docker pull nginx
sudo docker run -d -p 80:80 nginx
it will be executed as expected and you will find nginx running on your ec2.

GCP VM Startup script to determine VMs serving traffic

I have a VM startup script which is supposed to show me which instance behind the load balancer is serving the traffic. Unfortionately the variable is not working correctly. Below is the simple script:
#! /bin/bash
sudo apt-get update
sudo apt-get install apache2 -y
sudo a2ensite default-ssl
sudo a2enmod SSL
sudo vm_hostname="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/hostname)"
sudo echo "Page served from: $vm_hostname" | \
tee /var/www/html/index.html
The result I see is simply the following in the browser window:
Page served from:
Does anyone see a possible issue with the line calling the metadata?
You need to remove sudo from the six steps it will be like the script below.
vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/hostname)"
Your final script will be:
#! /bin/bash
sudo apt-get update
sudo apt-get install apache2 -y
sudo a2ensite default-ssl
sudo a2enmod SSL
vm_hostname="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/hostname)"
sudo echo "Page served from: $vm_hostname" | \
tee /var/www/html/index.html

Restart django on linux

Ive deployed a django app on linux using nginx, supervisor and gunicorn.
I have made updates (using GIT and manually in settings.py). The files changed but the application has not changed (i.e. it still functions the same as before the changes were added).
I've tried lots of commands but none of them work e.g.
//reload/restart nginx
sudo service nginx restart
sudo systemctl reload nginx
sudo systemctl restart nginx
//reload supervisor
sudo supervisorctl reload
//restart gunicorn (supervisor)
sudo supervisorctl restart gunicorn
//reboot ubuntu using systemctl
sudo systemctl reboot
//reboot ubuntu
sudo reboot now
//force reboot ubuntu
sudo reboot -f
Any help is greatly appreciated.

Chained docker image start httpd and php

I've got 2 Docker containers: httpd-container and php-container.
httpd-container dockerfile:
FROM centos:latest
RUN yum -y install httpd
RUN sed -i 's/AllowOverride None/AllowOverride all/g' /etc/httpd/conf/httpd.conf
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
php-container dockerfile
FROM httpd-container:latest
RUN yum -y install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json
RUN mkdir /run/php-fpm
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Currently I have to run /usr/sbin/php-fpm in php-container after it starts.
I`ve have tried putting both commands in one script and setting that as entry point, but it does not find it.
I've tried running supervisord and got errors as well.
Any advice is deeply appreciated!
Change php-container dockerfile to
FROM httpd-container:latest
RUN yum -y install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json
RUN mkdir /run/php-fpm
COPY ./start_services.sh /
CMD ["/start_services.sh"]
create start_services.sh with
#!/bin/sh
/usr/sbin/php-fpm
/usr/sbin/httpd -D FOREGROUND

How to run Django Daphne service on Google Kubernetes Engine and Google Container Registry

Dockerfile
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install build-essential -y
WORKDIR /app
COPY . /app/
# Python
RUN apt-get install python3-pip -y
RUN python3 -m pip install virtualenv
RUN python3 -m virtualenv /env36
ENV VIRTUAL_ENV /env36
ENV PATH /env36/bin:$PATH
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# Start Daphne [8443]
ENV DJANGO_SETTINGS_MODULE=settings
CMD daphne -e ssl:8443:privateKey=/ssl-cert/privkey.pem:certKey=/ssl-cert/fullchain.pem asgi:application
# Open port 8443
EXPOSE 8443
Enable Google IP Alias in order that we may connect to Google Memorystore/Redis
Build & Push
$ docker build -t [GCR_NAME] -f path/to/Dockerfile .
$ docker tag [GCR_NAME] gcr.io/[GOOGLE_PROJECT_ID]/[GCR_NAME]:[TAG]
$ docker push gcr.io/[GOOGLE_PROJECT_ID]/[GCR_NAME]:[TAG]
Deploy to GKE
$ envsubst < k8s.yml > patched_k8s.yml
$ kubectl apply -f patched_k8s.yml
$ kubectl rollout status deployment/[GKE_WORKLOAD_NAME]
I configured Daphne on GKE/GCR. If you guys have other solutions, please give me your advice.
system is not included in the Ubuntu:18.04 docker image.
Add an ENTRYPOINT to your Dockerfile with commands in ExecStart property of project-daphne.service.