A Brief History of Docker Containers
In the early days of software development, developers ran their applications on their local machines during testing and deployed them to a production virtual machine.
The problem with this approach is that some dependencies which might work in the development environment might not work in production. Hence the famous line by engineers, "it is working on my local machine but not working in production!" Or worse, it could be working on the local machine of some engineers but not working for others. Docker addressed this problem.
Docker containers provide applications with all it needs to run in a containerized environment. Therefore, eliminating the need for applications to depend on environment-specific system requirements. As long as it runs in a development environment, it would certainly work in a production environment.
Containers in Docker are spun up from images. Images provide a convenient way to package applications with the preconfigured server environment. So, Docker images are the package and containers are the package in current use.
Testing Docker Container Structure
What if there is a way to make sure that our Docker images have all the required files and directory to run correctly, execute commands correctly and generally reduce the rate of deploying a problematic image to production?
There is a way! Google's container structure test is the answer. container-structure-test.
Let's say you have folders called src and test at the root of your application and a Dockerfile for your Node js application with these commands:
FROM node:14-alpine
# Create app directory
WORKDIR /usr/node-app
ENV PORT=3000
COPY . .
RUN npm install
EXPOSE 3000
ENTRYPOINT ["npm", "run", "start" ]
Build the Docker image with this command:
docker build -t my-container-name .
Follow these commands to install Container Structure Tests on MacOs or Linux:
OS X
Install via brew:
$ brew install container-structure-test
curl -LO https://storage.googleapis.com/container-structure-test/latest/container-structure-test-darwin-amd64 && chmod +x container-structure-test-darwin-amd64 && sudo mv container-structure-test-darwin-amd64 /usr/local/bin/container-structure-test
Linux
curl -LO https://storage.googleapis.com/container-structure-test/latest/container-structure-test-linux-amd64 && chmod +x container-structure-test-linux-amd64 && sudo mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test
If you want to avoid using sudo:
curl -LO https://storage.googleapis.com/container-structure-test/latest/container-structure-test-linux-amd64 && chmod +x container-structure-test-linux-amd64 && mkdir -p $HOME/bin && export PATH=$PATH:$HOME/bin && mv container-structure-test-linux-amd64 $HOME/bin/container-structure-test
Create a file called config.yaml at the root of your project. Write the following in that file:
schemaVersion: 2.0.0
metadataTest:
env:
- key: PORT
value: 3000
exposedPorts: ["3000"]
# volumes: ["/test"]
entrypoint: ["npm", "run", "start" ]
# cmd: ["/bin/bash"]
workdir: "/usr/node-app"
# user: "luke"
fileExistenceTests:
- name: 'src directory exists'
path: '/usr/node-app/src'
shouldExist: true
- name: 'data directory exists'
path: '/usr/node-app/test'
shouldExist: true
Save the file and run this command:
container-structure-test test --image my-container-name \
--config config.yaml
This should give a successful test output.
Congratulations! You just tested your first docker container structure! You can research further ways to add it to a deployment pipeline or automate the process of testing. Thanks for your time.
Top comments (3)
Good working test structure.
Thank you 🙏🏾.
Sure no problem well deserved.