Docker: Difference between revisions
Line 93: | Line 93: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Docker Compose= | =Docker Compose= | ||
==Creating A Container With Composer== | |||
* Create a requirements.txt document with what is required for the container | |||
* Create a Docker File definition | |||
* Create a composer file | |||
==Django Container== | ==Django Container== | ||
===Requirements=== | ===Requirements=== | ||
Line 241: | Line 245: | ||
django_demo: | django_demo: | ||
external: true | external: true | ||
</syntaxhighlight> | |||
=Networking with Containers= | |||
==General== | |||
In order to connect on the network container to container I ended up setting up my own network. | |||
<syntaxhighlight lang="bash"> | |||
docker network create --attachable django_demo | |||
</syntaxhighlight> | |||
Once we have the network we can assign the containers to the network by setting the network name and the network in this case django_demo | |||
<syntaxhighlight lang="yaml"> | |||
version: '3.8' | |||
services: | |||
django: | |||
container_name: djangoapp | |||
build: | |||
context: . | |||
dockerfile: Dockerfile | |||
command: 'python manage.py runserver 0.0.0.0:8000' | |||
ports: | |||
- 8000:8000 | |||
volumes: | |||
- .:/app | |||
depends_on: | |||
- db | |||
networks: | |||
django_demo: | |||
... | |||
networks: | |||
django_demo: | |||
external: true | |||
</syntaxhighlight> | |||
We need to specify the container_name because the default is to use the service name an, underscore and a number which in this case would have been django_1. This creates an invalid host as the underscore is not allowed. | |||
==Commands== | |||
We can see the network with | |||
<syntaxhighlight lang="bash"> | |||
docker network inspect django_demo | |||
</syntaxhighlight> | |||
We can delete network with | |||
<syntaxhighlight lang="bash"> | |||
docker network prune | |||
</syntaxhighlight> | |||
And we can see the containers with | |||
<syntaxhighlight lang="bash"> | |||
docker ps | |||
</syntaxhighlight> | |||
==Django== | |||
To ensure no errors we need to specify which hosts django allows. This is done in the settings.py using | |||
<syntaxhighlight lang="python"> | |||
... | |||
ALLOWED_HOSTS = ['127.0.0.1','djangoapp'] | |||
... | |||
</syntaxhighlight> | |||
We need to also need to account for the hostname when using the container_name in the docker-compose yml | |||
<syntaxhighlight lang="python"> | |||
DATABASES = { | |||
'default': { | |||
'ENGINE': 'django.db.backends.mysql', | |||
'NAME': 'dont', | |||
'USER': 'copy', | |||
'PASSWORD': 'this', | |||
'HOST': 'djangodb', | |||
'PORT': '3306' | |||
} | |||
} | |||
</syntaxhighlight> | |||
==Flask== | |||
===Database=== | |||
In this case I created a flask app so the database is specified in the main.py in the SQLALCHEMY_DATABASE_URI. Here we use the hostname for URI. | |||
<syntaxhighlight lang="python"> | |||
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@flaskdb/main' | |||
</syntaxhighlight> | |||
===Container to Container=== | |||
To call the django service we do this by using the hostname of the django service. In our case. | |||
<syntaxhighlight lang="python"> | |||
... | |||
req = requests.get('http://djangoapp:8000/api/user') | |||
return jsonify(req.content.decode('utf-8')) | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 14:16, 10 January 2021
Common commands
Show the current docker image running
sudo docker ps
Show the images available
sudo docker images
Stop a container id
sudo docker stop <container id>
Start a container id
sudo docker stop <container id>
Show ports exposed
sudo docker port <container id>
List current containers
sudo docker container ls -s
Remove a container
sudo docker container rm <container id>
Pull a container
sudo docker pull <image name>
This will remove all images without at least one container associated to them
docker image prune -a
This will remove all stopped containers
docker container prune
Build Container
RUN command
RUN su-exec volpara git clone https://github.com/bibble235/aports
Copying from on container
Given the container
FROM ubuntu:18.04 AS opencv_build
Copying can be done using
COPY --from=opencv_build /usr/local/opencv/ /usr/local/
Creating a docker postgres image
This exposes the default port 5432 for postgres
docker run --name postgresq-spring -e POSTGRES_USER=iain -e POSTGRES_PASSWORD=password -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres:alpine
The -d is to run the container in the background
The postgres:alpine is the image name
The -p is the port to expose which is the default for postgress
The -v is where the data will be stored
Volumes
List volumes
docker volume ls
Create a volumes
docker volume create hello
Build MediaWiki Container
Build your own Docker Image
Get the definition from github and build the container.
https://github.com/wikimedia/mediawiki-docker/blob/38d4e7f77f8b5b4efda91f969ccba2475fc2c597/1.35/apache/Dockerfile
docker build .
Use Prebuilt
We can install the prebuilt one with
docker pull mediawiki
We can find the container with the command
docker ps --format "table {{.ID}}\t{{.Status}}\t{{.Names}}"
b0ff6611123e Up 2 minutes some-mediawiki
898c8f40818d Up 6 hours k8s_operator_openshift-web...
b02230ae7074 Up 6 hours k8s_kube-dns_kube-dns-qhbvf_....
ea19641bc9a1 Up 6 hours k8s_webconsole_webconsole-...
0dc6ee563a9b Up 6 hours k8s_registry_docker-registry-...
0b6ea1064152 Up 6 hours k8s_operator_openshift-servic...
...
From there we can get the ip address with
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' b0ff6611123e
172.17.0.9
Delete Container
We can stop the contain and delete it
docker stop b0ff6611123e
docker container rm b0ff6611123e
Docker Compose
Creating A Container With Composer
- Create a requirements.txt document with what is required for the container
- Create a Docker File definition
- Create a composer file
Django Container
Requirements
Here are the specific requirements for the Django cotainer.
Django==3.1.5
djangorestframework==3.12.2
mysqlclient==2.0.3
django-mysql==3.10.0
django-cors-headers==3.6.0
pika==1.1.0
DockerFile
This command runs the django app at start up
FROM python:3.9
ENV PYTHONNUMBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app
Yaml
We can build a container using docker-compose. Create a docker file with
version: '3.8'
services:
django:
container_name: djangoapp
build:
context: .
dockerfile: Dockerfile
command: 'python3 manage.py runserver 0.0.0.0:8000'
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- db
networks:
django_demo:
queue:
environment:
- PYTHONUNBUFFERED=1
build:
context: .
dockerfile: Dockerfile
container_name: djangoqueue
command: 'python3 consumer.py'
depends_on:
- db
networks:
django_demo:
db:
image: mysql:5.7.32
container_name: djangodb
restart: always
environment:
MYSQL_DATABASE: admin
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306
networks:
django_demo:
networks:
django_demo:
external: true
Flask Container
Requirements
Here are the specific requirements for the Flask container.
Flask==1.1.2
Flask-SQLAlchemy==2.4.4
SQLAlchemy==1.3.20
Flask-Migrate==2.5.3
Flask-Script==2.0.6
Flask-Cors==3.0.9
requests==2.25.0
mysqlclient==2.0.3
pika==1.1.0
DockerFile
This command runs the flask app at start up
FROM python:3.9
ENV PYTHONNUMBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app
Yaml
We can build a container using docker-compose. Create a docker file with
version: '3.8'
services:
flask:
build:
context: .
dockerfile: Dockerfile
container_name: flaskapp
command: python main.py
ports:
- 8001:5000
volumes:
- .:/app
depends_on:
- db
networks:
django_demo:
queue:
build:
context: .
dockerfile: Dockerfile
container_name: flaskqueue
command: 'python consumer.py'
depends_on:
- db
networks:
django_demo:
db:
image: mysql:5.7.32
container_name: flaskdb
restart: always
environment:
MYSQL_DATABASE: main
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/nysql
ports:
- 33067:3306
networks:
django_demo:
networks:
django_demo:
external: true
Networking with Containers
General
In order to connect on the network container to container I ended up setting up my own network.
docker network create --attachable django_demo
Once we have the network we can assign the containers to the network by setting the network name and the network in this case django_demo
version: '3.8'
services:
django:
container_name: djangoapp
build:
context: .
dockerfile: Dockerfile
command: 'python manage.py runserver 0.0.0.0:8000'
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- db
networks:
django_demo:
...
networks:
django_demo:
external: true
We need to specify the container_name because the default is to use the service name an, underscore and a number which in this case would have been django_1. This creates an invalid host as the underscore is not allowed.
Commands
We can see the network with
docker network inspect django_demo
We can delete network with
docker network prune
And we can see the containers with
docker ps
Django
To ensure no errors we need to specify which hosts django allows. This is done in the settings.py using
...
ALLOWED_HOSTS = ['127.0.0.1','djangoapp']
...
We need to also need to account for the hostname when using the container_name in the docker-compose yml
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dont',
'USER': 'copy',
'PASSWORD': 'this',
'HOST': 'djangodb',
'PORT': '3306'
}
}
Flask
Database
In this case I created a flask app so the database is specified in the main.py in the SQLALCHEMY_DATABASE_URI. Here we use the hostname for URI.
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@flaskdb/main'
Container to Container
To call the django service we do this by using the hostname of the django service. In our case.
...
req = requests.get('http://djangoapp:8000/api/user')
return jsonify(req.content.decode('utf-8'))