Skip to content

Instantly share code, notes, and snippets.

@ejlp12
Last active June 18, 2024 22:08
Show Gist options
  • Save ejlp12/bf7fa3967f715de241f2dd6b6587372c to your computer and use it in GitHub Desktop.
Save ejlp12/bf7fa3967f715de241f2dd6b6587372c to your computer and use it in GitHub Desktop.
Flatten or squashing or stripped docker image

OPTION-1

docker image build --squash -t image-name:tag .

OPTION-2

Run the image then export the running image and save to the image repository using import

docker run ... image-name:tag
docker ps 
docker export <CONTAINER_ID> | docker import - new-image-name:new-tag

docker export `docker ps -q -n=1` | docker import - new-image-name:new-tag

Create new file Dockerfile.flatten

FROM new-image-name:new-tag

EXPOSE <PORT>

CMD ["command_here"]

Build the new container image

docker build -f Dockerfile.flatten -t image-name:flatten-tag .

Test the new container image (make sure the service is running as expected)

docker run ... image-name:flatten-tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment