Bootstrap FreeKB - Docker - Build an image using a Dockerfile
Docker - Build an image using a Dockerfile

Updated:   |  Docker articles

This assumes you have installed Docker on Linux and Docker is running.

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage and memory (containers are usually megabytes in size).

 

There are various ways to pull down an image:

  • Using the docker pull command (as-is image as-is is pulled down)
  • Using the docker run command (as-is image is pulled down and container is created/started)
  • Using the docker build command (image can be customized)
  • Using the docker stack deploy command (Docker Compose)

 

Let's say the /usr/local/my/app directory on your Linux server contains the various files needed for your application, perhaps something like this.

├── main.py
├── my-project (directory)
│   ├── __init__.py
│   ├── views.py
│   ├── templates (directory)
│   │   ├── base.html
│   │   ├── home.html
│   │   ├── foo.html
│   │   ├── bar.html

 

You create a docker image that contains the various files needed for your application, there will need to be a file named Dockerfile in the base directory for your application (/usr/local/my/app in this example).

├── Dockerfile
├── requirements.txt
├── main.py
├── my-project (directory)
│   ├── __init__.py
│   ├── views.py
│   ├── templates (directory)
│   │   ├── base.html
│   │   ├── home.html
│   │   ├── foo.html
│   │   ├── bar.html

 

Let's say Dockerfile contains the following.

FROM tiangolo/uwsgi-nginx-flask:python3.11
RUN apt-get update -y
RUN pip install --upgrade pip
COPY requirements.txt /usr/requirements.txt
RUN pip install -r /usr/requirements.txt


COPY main.py:/app/main.py
COPY my-project:/app/my-project

 

You can move into the directory that contains the Dockerfile and then use the docker build command to create an image using the Dockerfile. 

cd /usr/local/my/app
docker build --file Dockerfile --tag my-org/my-image:0.0.1 .

 

Or you can specify the full path to the directory that contains the Dockerfile.

docker build --file /usr/local/my/app/Dockerfile --tag my-org/my-image:0.0.1 /usr/local/my/app

 

If all goes well, something like this should be displayed.

Sending build context to Docker daemon  147.6MB
Step 1/7 : FROM tiangolo/uwsgi-nginx-flask:python3.11
 ---> a975551e90f3
Step 2/7 : RUN apt-get update -y

 ---> 7e5160ca0c0d
Step 3/7 : RUN pip install --upgrade pip
 ---> 9b37e2b74f3d
Step 4/7 : COPY requirements.txt /usr/requirements.txt
 ---> adef77bf589e
Step 5/5 : RUN pip install -r /usr/requirements.txt
 ---> Running in ca075505c990
Step 6/6 : COPY main.py /app/main.py
 ---> c6389f4ff2ae
Step 7/7 : COPY my-project /app/my-project
 ---> 620ae5439453
Successfully built 620ae5439453
Successfully tagged my-org/my-image:0.0.1

 

The docker images command can be used to display the images.

docker images

 

Which should return something like this.

REPOSITORY                 TAG     IMAGE ID      CREATED             SIZE
my-org/my-image:0.0.1      0.0.1   fb60d5e3e7c8  About a minute ago  5.6MB

 

Then the docker run command can be used to create a container using the image.

sudo docker run --detach --restart unless-stopped --name my-container --publish 10.11.12.13:12345:80 my-org/my-image:0.0.1

 

And there should now be a container that is using the image.

~]$ sudo docker container ls -a
CONTAINER ID   IMAGE                   COMMAND                  CREATED            STATUS              PORTS                   NAMES
d6d0e6467c6c   my-org/my-image:0.0.1   "/entrypoint.sh /sta…"   39 seconds ago     Up 35 seconds       10.11.12.13:12345->80/tcp   my-container

 

Finally, the container can be stopped and removed.

sudo docker stop my-container
sudo docker rm my-container

 

And the docker rmi command can be used to remove the image.

docker rmi my-org/my-image:0.0.1

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c2f1e3 in the box below so that we can be sure you are a human.