What is Dockerfile
-
docker image is created from docker file which runs inside container.
Dockerfile Instructions
| Name | Meaning | ||
|---|---|---|---|
| FROM |
This is the first instruction in every Dockerfile. It specifies the
base Docker image
|
||
| WORKDIR |
sets the working directory inside the container
|
||
| COPY src dst |
Copy files or directories from your local file system (the host) to
the Docker container's file system.
|
||
| RUN | Execute a command in the Docker image at build time. | ||
| CMD |
Command which is run at startup with DEFAULT arguments
CMD can be easily overridden by adding arguments at the end of docker run
|
||
| ENTRYPOINT |
This command specifies the default command that will run when the
container starts. Mainly used to set the main executable for the
container. Can be overridden using --entrypoint flag
Most Often Use case: Using ENTRYPOINT, CMD together
|
||
| EXPOSE (only Documentation) |
The EXPOSE command is used as documentation to inform users and
other tools that the container will listen on specific network ports
at runtime. It DOES NOT actually publish or open the port to the host machine or the outside world. PUBLISH (-p): There is not publish command in Dockerfile, but if you want to open a port then you can specify using -p after docker run
|
Dockerfile Example
-
We will use the code from this repository
https://github.com/meobilivang/newsreader-cli
| Step | Description |
|---|---|
| Step-1: Clone the code |
|
| Step-2: Create run_test.py file in root of cloned repo |
|
| Step-3. Dockerfile content. Create Dockerfile in root of cloned repo |
|
Create Docker image
- Create docker image using docker file created in above step
> docker build --platform linux/amd64 -t newsreader-cli-test .
[+] Building 33.0s (15/15) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.2s
=> => transferring dockerfile: 886B 0.1s
=> [internal] load metadata for docker.io/library/python:3-slim 1.8s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [ 1/11] FROM docker.io/library/python:3-slim@sha256:c24c34b502635f1f7c4e99dc09a2cbd85d480b7dcfd077198c6b5af138906390 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 5.16kB 0.1s
=> CACHED [ 2/11] WORKDIR /app 0.0s
=> CACHED [ 3/11] COPY requirements.txt ./ 0.0s
=> CACHED [ 4/11] RUN pip install --no-cache-dir -r requirements.txt 0.0s
=> [ 5/11] COPY . /app/ 12.9s
=> [ 6/11] RUN pip3 install --no-cache-dir pytest 4.1s
=> [ 7/11] RUN pip3 install --no-cache-dir py==1.11.0 3.1s
=> [ 8/11] RUN python -m pytest /app/tests/ --maxfail=1 --disable-warnings 2.4s
=> [ 9/11] COPY run_tests.sh ./ 0.1s
=> [10/11] RUN chmod +x /run_tests.sh 0.6s
=> exporting to image 6.9s
=> => exporting layers 6.6s
=> => writing image sha256:5f77f59a7ebf7e5c4357e0c1532cac0cd417e1f2a2700c7d8bf757a89a21f9cd
Check Docker Image
-
Now we have created docker image, its time to go inside it and check the
contents.
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newsreader-cli-test latest 5f77f59a7ebf 7 minutes ago 734MB
// Check Architecture
> docker inspect --format='{{.Os}}/{{.Architecture}}' 5f77f59a7ebf
'linux/amd64'
// Run tests inside docker container
// Check run command on Commands page
> docker run --platform linux/amd64 -i newsreader-cli-test < pytest
============================= test session starts ==============================
platform linux -- Python 3.12.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /app
collected 3 items
tests/test_article.py . [ 33%]
tests/test_web_scraper.py . [ 66%]
tests/test_data/test_newsreadercli.py . [100%]
=============================== warnings summary ===============================
../usr/local/lib/python3.12/site-packages/certifi/core.py:36
/usr/local/lib/python3.12/site-packages/certifi/core.py:36: DeprecationWarning: path is deprecated. Use files() instead. Refer to https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy for migration advice.
_CACERT_CTX = get_path("certifi", "cacert.pem")
-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================= 3 passed, 1 warning in 0.65s =========================
// Enter inside docker container
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newsreader-cli-test latest 2376cae71f9e 7 minutes ago 734MB
> docker run -it --entrypoint /bin/bash newsreader-cli-test
// Check contents of /app folder
root@0e8f62096f71:/# ls /app
Dockerfile LICENSE README.md docker-image newsreadercli requirements.txt run_tests.sh tests
// Check location of run_tests.sh
root@0e8f62096f71:/# ls run_tests.sh
run_tests.sh
root@0e8f62096f71:/# pwd
/
root@0e8f62096f71:/#