Docker 101
Install Docker for Mac
https://docs.docker.com/get-docker/
Create a Container
To create a container in docker, you need to first create a folder.
Within this folder you will need 3 docker files, and the files (code) to run your app.
The docker files are:
- docker-compose.yaml
- requirements.txt
- Dockerfile
The following example here is a flask container, which uses a python script (app.py).
We have 4 files, all located in the same directory. The name of the directory doesn't matter.
app.py
Dockerfile
requirements.txt
docker-compose.yaml
Dockerfile
FROM python:3.8
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
The first thing here is the image which is used as the base image - denoted by the line "FROM python:3.8". Phyton 3.8 is a standard image containing Python. We then add to this image to install additional Python libraries before the program is executed - in this case 'pip install -r requirements.txt'.
Finally the 'Dockerfile' contains what should be executed when the container loads (python app.py).
requirements.txt
Flask==0.12
flask-restful==0.3.5
flask-mysqldb==0.2.0
mysqlclient==1.4.2.post1
The requirements.txt file simply includes the python libraries which should be installed before the program is executed. This is the same as you would run on your PC - e.g pip install Flask==0.12.
docker-compose.yaml
version: "3.7"
services:
helloworld:
build:
context: ./
ports:
- 5000:5000
The docker-compose.yaml file states how the network requirements for the application - how it should be exposed to the outside world.
We now run the command:
docker compose build
from the directory which includes the 4 files mentioned above.
This will build the container, first downloading the image for python (python:3.8), and then installing the additional python libraries mentioned in the requirements.txt file. The docker image will include all of the files in the directory from where you ran the 'docker compose build' command.
The docker image will be automatically imported to docker running locally on your PC. To view it, run:
docker images
The name of the image = the name of the folder from where you ran the 'docker compose build' command concatenated with and underscore and the name of the service from the docker-compose.yaml file. In this case, my folder name is 'test' and the service name is 'helloworld'. The image name is therefore 'test_helloworld'.
Next, you want to upload the image to docker hub. To do that you first need to setup an account (free) on dockerhub https://hub.docker.com.
In order to upload the image to docker hub you must first login with a browser and create a repository on https://hub.docker.com. Make a note of the name of your repository. You can create a public or private repository. For this example I used a public repository.
Next, you need to tag your image with your dockerhub username.
If your username is 'my-username', the repository name is 'my-repo', and the image created by 'docker compose build' is named 'testhelloworld', the tag command would be:
docker tag test_helloworld my-username/myrepo:aUniqueTagName
Note 'aUniqueTagName' is used to identify the image you are uploading and will be used in pull requests (downloads) against docker hub. The ':aUniqueTagName' is optional for the docker tag command, but I strongly recommend using it!
If you run docker images again, you will see your tagged image there.
Before uploading the image to docker hub, you must login to docker hub on the command line. To do that, run:
docker login -u your-username -p your-password docker.io
You can now upload your image to docker hub using:
docker push my-username/myrepo:aUniqueTagName
If you are using the image for a kubernetes deployment, in your deployment.yaml file, the image would be referenced as:
spec:
containers:
- name: name-of-your-container
image: docker.io/my-username/myrepo:aUniqueTagName
Other Docker commands
show running containers
docker ps
show running and stopped containers
docker ps -a
No comments:
Post a Comment