Image Creation, Pushing Images, and Registry
Building a Docker File
A docker file is essentially a list of instructions that are issued to Docker to build an image. You will more than likely want to build your Docker File somewhere that you can access all the files required for the application you're dockerising.
To create a docker image, you first need to a list of every step required for installing/configuring the application on bare metal. As a basic example, I have an application which requires the following;
Ubuntu 22.04
apt update
apt install -y python python-pip
application code in /opt/app.py
command to run app
Docker File for this app;
FROM ubuntu #Define base image to use for the container
RUN apt-get update
RUN apt-get install -y python python-pip #Install required packages, use -y flag to accept install without manual intervention
RUN pip install flask #Install Flask module for web app
COPY app.py /opt/app.py #Copy the app source code from current directory file 'app.py' to /opt/app.py on the container
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0 #Command to run the application
Once we've created our Docker File, we then use the Docker Build command to build a locally stored version of the image.
#In docker file dir
docker build . -t dockeraccount/imagename
Image Tagging
When running docker build, it's best practice to apply a tag to the image so that it can be distinguished between previous/future versions.
docker build . -t dockeraccount/imagename:tag
You can also retroactively apply tags to an existing image:
docker tag imagename accountname/imagename:tag
Docker File Options
A list of options available within DockerFIle
| FROM | Specify source image |
| RUN | Issue a command to run on the container upon startup |
| COPY | Copy files from local machine to image specification |
| ENTRYPOINT |
Define a command to be run, that can have additional parameters passed when starting the container. example;
#set the startup command as sleep
#we can then pass a parameter to this command when starting the container
Differs from the RUN option since that cannot have parameters passed to it - what is defined in the docker file is what will run. |
Pushing an image to a Docker repository/registry
Registry: This is a service or system where Docker images are stored and distributed. Docker Hub is a public registry, but you can also have private registries. A registry can host many repositories.
Repository: A repository is a collection of related Docker images, usually representing different versions of the same application or component. For example, nginx is a repository on Docker Hub that contains different image tags like nginx:latest, nginx:1.21, etc. A repository is hosted in a registry.
Private Registry
Lets say you want to create your own private registry to store your images. Docker offers a registry image that can be used to achieve this;
docker run -d -p 127.0.0.1:5000:5000 --restart=always --name registry \
-v /path/to/your/registry/data:/var/lib/registry \
registry:2
To make this accessible via WAN, you'll want to configure a domain DNS record to point to the server where the registry container is, and then setup something like an nginx proxy to forward traffic to it.
Authentication
In order to push an image to a repository, you'll first need to authenticate with your chosen repository.
Docker Hub
docker login
You'll then be prompted to enter your credentials:
$ docker login
Username: yourdockerhubusername
Password: yourpassword
GitLab
Use your GitLab username and a personal access token instead of your password.
Once you’re logged in, Docker stores your credentials locally (in ~/.docker/config.json), allowing you to push images without re-authenticating each time.
Image Push
Once you've authenticated, you can then push the image using the docker push command.
docker push accountname[or_registryname]/imagename:tag
If I was pushing to a private registry:
docker push dockerio.b4sed.xyz/my-nginx
If I was pushing to my docker account:
docker push dcropp/my-nginx
No Comments