Last active
April 25, 2019 02:25
-
-
Save zoe606/9174c92399d5e1b943545005c1be7871 to your computer and use it in GitHub Desktop.
Docker Setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Melihat Running Containter | |
Docker ps | |
## Ssh ke dalam container | |
docker exec -it {containter_code} bash | |
## Memulai docker | |
docker-compose up -d | |
## Mematikan docker | |
docker-compose down | |
## TBC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo apt-get update | |
$ sudo apt-get install \ | |
apt-transport-https \ | |
ca-certificates \ | |
curl \ | |
gnupg2 \ | |
software-properties-common | |
$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - | |
$ sudo apt-key fingerprint 0EBFCD88 | |
$ sudo add-apt-repository \ | |
"deb [arch=amd64] https://download.docker.com/linux/debian \ | |
$(lsb_release -cs) \ | |
stable" | |
$ sudo apt-get update | |
$ sudo apt-get install docker-ce | |
Verify that Docker installed | |
$ sudo docker run hello-world | |
$ sudo docker -v | |
------------------------------------------------------------------------------- | |
2. Build Apps with Dockerfile | |
------------------------------------------------------------------------------- | |
buat sebuah temporary directory yang didalamnya terdiri dari | |
-Dockerfile (sebuah file berisi perintah-perintah untuk menjalankan apps) | |
-app.py (ini contoh aplikasi webbased menggunakan python) | |
-requirements.txt (ini bagian dari apps) | |
Dockerfile | |
# Use an official Python runtime as a parent image | |
FROM python:2.7-slim | |
# Set the working directory to /app | |
WORKDIR /app | |
# Copy the current directory contents into the container at /app | |
ADD . /app | |
# Install any needed packages specified in requirements.txt | |
RUN pip install --trusted-host pypi.python.org -r requirements.txt | |
# Make port 80 available to the world outside this container | |
EXPOSE 80 | |
# Define environment variable | |
ENV NAME World | |
# Run app.py when the container launches | |
CMD ["python", "app.py"] | |
requirements.txt | |
Flask | |
Redis | |
app.py | |
from flask import Flask | |
from redis import Redis, RedisError | |
import os | |
import socket | |
# Connect to Redis | |
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
try: | |
visits = redis.incr("counter") | |
except RedisError: | |
visits = "<i>cannot connect to Redis, counter disabled</i>" | |
html = "<h3>Hello {name}!</h3>" \ | |
"<b>Hostname:</b> {hostname}<br/>" \ | |
"<b>Visits:</b> {visits}" | |
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=80) | |
Build Apps tersebut dengan perintah: | |
$ docker build -t friendlyhello . | |
Cek apakah sudah terbentuk image nya atau belum dengan cara : | |
$ docker images | |
RUN Apps dengan perintah : | |
$ docker run -p 4000:80 friendlyhello | |
Cek di browser | |
http://ippublik:4000 | |
Cara stop Apps | |
$ docker container ls | |
$ docker container stop $CONTAINER_ID | |
Docker Registry | |
Pastikan signup akun di hub.docker.com | |
$ docker login | |
TAG image pada docker | |
Tag berfungsi untuk mengaitkan repository lokal ke registry dengan username/repository:tag | |
$ docker tag image username/repository:tag | |
Misalnya | |
$ docker tag friendlyhello ariretiarno/firstapp:v1 | |
Cek daftar image yang telah di tag dengan perintah | |
$ docker images | |
Publish image/share image ke dockerhub | |
$ docker push username/repository:tag | |
Cek hasil push di hub.docker.com | |
Pull and run the image from the remote repository | |
Untuk menjalankan aplikasi yang telah kita buat di env lain misalnya.. nah kita bisa ketikan perintah: | |
$ docker run -p 4000:80 username/repository:tag | |
Selesai... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3. Build apps with nginx | |
-------------------------------------------------------------------------------------------------------- | |
apps.conf | |
server { | |
listen 80; | |
server_name localhost; | |
#charset koi8-r; | |
#access_log /var/log/nginx/host.access.log main; | |
location / { | |
root /var/www/apps; | |
index index.html index.htm; | |
} | |
#error_page 404 /404.html; | |
# redirect server error pages to the static page /50x.html | |
# | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html { | |
root /var/www/apps; | |
} | |
} | |
index.html | |
ini apps saya pakai nginx | |
Dockerfile | |
#source utama image | |
FROM nginx:latest | |
MAINTAINER Muhamad Ari Retiarno <ari.retiarno@docotel.com> | |
#VHOST | |
RUN mkdir -p /var/www/apps | |
RUN rm /etc/nginx/conf.d/default.conf | |
ADD apps.conf /etc/nginx/conf.d | |
ADD index.html /var/www/apps | |
RUN chown www-data:www-data -R /var/www | |
RUN chown root:root /etc/nginx/conf.d/apps.conf | |
#Running into port 80 | |
EXPOSE 80 | |
CMD nginx -g "daemon off;" | |
Build Apps. | |
docker build -t aplikasinginx:v1 . | |
Selesai... | |
----------------------------------------------------------------------------- | |
----------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment