Skip to main content

Docker - Image Creation & Pushing Images


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;

 

ENTRYPOINT sleep

#set the startup command as sleep

 

docker run image 10

#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

Repo 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/imagename:tag