Docker Cheat-Sheet
Docker Cheat-Sheet
Question: Create an Ubuntu container, install Java, and run hello world program
Make an image of this setup
Push this image into a private Docker Hub registry
Run the image from the private registry by pulling it back
Docker Networking
Docker Networking
Check available network docker network ls
create a custom network of This command used to create a docker network create --
type bridge bridge network driver=bridge my_network
Inspect the bridge network This command is used to docker network inspect bridge
retrieve the information of the
bridge network driver
we are creating a new docker run -dt --name=nettry2
container of ubuntu image ubuntu
without specifying any
network details
check the network details docker inspect network
container_name
Connect the network docker network connect
network_name nettry2
disconnect a container from This command is used to docker network disconnect bridge
any network disconnect the network container_name
Docker network with a docker network create
custom subnet network_name --
subnet=10.11.0.0/16
Run a container and connect it docker run -it --
to a custom network with a name=container_name --
net=network_name --ip=10.11.0.10
static IP address using the
ubuntu
ubuntu image
try to create a container of docker run -dt httpd (apache)
apache httpd image without
specifying its name.
Docker Compose
app = Flask(__name__)
cache = redis.Redis(host='redis',
port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except
redis.exceptions.ConnectionError
as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have
been seen {} times.\
n'.format(count)
Create another file called redis
requirements.txt in your flask
project directory
Create a docker file FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc
musl-dev linux-headers
COPY requirements.txt
requirements.txt
RUN pip install -r
requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
Create a file called services:
compose.yaml in your project web:
directory build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
Build and run your app with docker compose up
compose
Go to browser Enter http://localhost:8000/
Refresh the page Output:
Hello World! I have been seen 2
times.
Switch to another terminal docker image ls
window, Listing images at this
point should return redis and
web
Stop the application, from docker compose down
within your project directory
in the second terminal,
If you want to run your docker compose ps
services in the background, docker compose up -d
you can pass the -d flag (for
"detached" mode)